Postgresql Delete
# PostgreSQL DELETE Statement
You can use the DELETE statement to delete data from a PostgreSQL table.
### Syntax
Here is the general syntax for deleting data using the DELETE statement:
DELETE FROM table_name WHERE ;
If the WHERE clause is not specified, all records in the PostgreSQL table will be deleted.
Generally, we need to specify conditions in the WHERE clause to delete specific records. The condition can use AND or OR operators to specify one or more conditions.
### Example
Create the COMPANY table ((https://static.jyshare.com/download/company.sql)), with the following data:
tutorialdb# 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 record with ID 2:
tutorialdb=# 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)
From the result above, we can see that the record with id 2 has been deleted.
The following statement will delete the entire COMPANY table:
DELETE FROM COMPANY;
YouTip