1.语法
DELETE FROM table_name WHERE [condition];
如果没有指定WHERE子句,表中所有的记录将被删除。
一般我们需要在WHERE子句中指定条件来删除对应的记录,条件语句可以使用AND或OR来指定一个或多个。
2.示例
runoobdb=# select * from company;
id | name | age | address | salary
----+-------+-----+------------------------------------------------------+--------
2 | Allen | 25 | haha | 1
4 | Mark | 25 | haha | 1
6 | Kim | 22 | haha | 1
7 | James | 24 | haha | 1
3 | Teddy | 23 | haha | 1
1 | Paul | 32 | 北京 | 100000
5 | David | 27 | 北京 | 100000
(7 行记录)
--删除ID为2的数据
runoobdb=# DELETE FROM COMPANY WHERE ID = 2;
DELETE 1
runoobdb=# select * from company;
id | name | age | address | salary
----+-------+-----+------------------------------------------------------+--------
4 | Mark | 25 | haha | 1
6 | Kim | 22 | haha | 1
7 | James | 24 | haha | 1
3 | Teddy | 23 | haha | 1
1 | Paul | 32 | 北京 | 100000
5 | David | 27 | 北京 | 100000
(6 行记录)
--清空整个表
runoobdb=# DELETE FROM COMPANY;
DELETE 6
--查看发现company这个表还在,只是表被清空了
runoobdb=# \d
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+---------+--------+----------
public | company | 数据表 | postgres
(1 行记录)
runoobdb=# select * from company;
id | name | age | address | salary
----+------+-----+---------+--------
(0 行记录)