Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PostgreSQL DELETE Statement

PostgreSQL DELETE Statement

You can use the DELETE statement to delete data from a PostgreSQL table.

Syntax

The following is the general syntax of the DELETE statement for deleting data:

DELETE FROM table_name WHERE [condition];

If no WHERE clause is specified, all records in the PostgreSQL table will be deleted.

Usually we need to specify conditions in the WHERE clause to delete the corresponding records; the conditional statement can use AND or OR operators to specify one or more.

Example

Create the COMPANY table (Download the COMPANY SQL file ), with the following data:

runoobdb# select * from COMPANY;

 id | name  | age | address   | salary

----+-------+-----+-----------+--------

  1 | Paul  |  32 | California|  20000

  2 | Allen |  25 | Texas     |  15000

  3 | Teddy |  23 | Norway    |  20000

  4 | Mark  |  25 | Rich-Mond |  65000

  5 | David |  27 | Texas     |  85000

  6 | Kim   |  22 | South-Hall|  45000

  7 | James |  24 | Houston   |  10000

(7 rows)

The following SQL statement will delete the data with ID 2:

runoobdb=# DELETE FROM COMPANY WHERE ID = 2;

The result is as follows:

 id | name  | age | address     | salary

----+-------+-----+-------------+--------

  1 | Paul  |  32 | California  |  20000

  3 | Teddy |  23 | Norway      |  20000

  4 | Mark  |  25 | Rich-Mond   |  65000

  5 | David |  27 | Texas       |  85000

  6 | Kim   |  22 | South-Hall  |  45000

  7 | James |  24 | Houston     |  10000

(6 rows)

As can be seen from the above result, the data with id 2 has been deleted.

The following statement will delete the entire COMPANY table:

DELETE FROM COMPANY;