轻松上手,快乐学习!

SQL DELETE 语句


SQL DELETE 语句

DELETE语句用于删除表中的现有记录。

删除语法

DELETE FROM table_name WHERE condition;
注意:删除表中的记录时要小心!请注意DELETE语句中的WHERE子句.WHERE子句指定应删除哪些记录。如果省略WHERE子句,表中的所有记录都将被删除!

演示数据库

以下是Northwind示例数据库中“Customers”表的选择:
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constituciуn 2222 Mйxico D.F. 05021 Mexico
3 Antonio Moreno Taquerнa Antonio Moreno Mataderos 2312 Mйxico D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbkцp Christina Berglund Berguvsvдgen 8 Luleе S-958 22 Sweden

SQL DELETE 实例

以下SQL语句从“Customers”表中删除客户“Alfreds Futterkiste”:

实例

DELETE FROM CustomersWHERE CustomerName='Alfreds Futterkiste';
运行实例»
“Customers”表结果如下:
CustomerID CustomerName ContactName Address City PostalCode Country
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constituciуn 2222 Mйxico D.F. 05021 Mexico
3 Antonio Moreno Taquerнa Antonio Moreno Mataderos 2312 Mйxico D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbkцp Christina Berglund Berguvsvдgen 8 Luleе S-958 22 Sweden

删除所有记录

可以删除表中的所有行而不删除表。这意味着表结构,属性和索引将保持不变:
DELETE FROM table_name;
以下SQL语句删除“Customers”表中的所有行,而不删除表:

实例

DELETE FROM Customers;
运行实例»