PostgreSQL DELETE 查询
PostgreSQL DELETE 查询用于从表中删除现有记录。您可以使用WHERE子句与DELETE查询一起使用以删除所选的行。否则,将删除所有记录。
语法
带有WHERE子句的DELETE查询的基本语法如下:
DELETE FROM table_name
WHERE [condition];
您可以使用AND或OR运算符组合N个条件。
示例
考虑表COMPANY,记录如下-
# 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)
以下是一个示例,将删除ID为7的客户−
testdb=# DELETE FROM COMPANY WHERE ID = 2;
现在,COMPANY 表将会有以下记录:
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)
如果你想从 COMPANY 表中删除所有记录,你不需要在 DELETE 查询中使用 WHERE 子句,如下所示:
testdb=# DELETE FROM COMPANY;
现在,公司表中没有任何记录,因为所有记录都被删除了 DELETE 语句。