📜  PostgreSQL-删除查询

📅  最后修改于: 2020-11-26 06:22:25             🧑  作者: Mango


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表中的所有记录,则不需要对WHERE子句使用DELETE查询,如下所示-

testdb=# DELETE FROM COMPANY;

现在,COMPANY表没有任何记录,因为所有记录已被DELETE语句删除。