MySQL DELETE 语句
MySQL DELETE 语句
MySQL 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 Customers WHERE 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;