SQL Delete

创建于 2024-12-02 / 33
字体: [默认] [大] [更大]

DELETE 语句

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

DELETE 语法

DELETE FROM table_name WHERE condition;

注释: 请注意 SQL 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


DELETE 实例

以下 SQL 语句从 客户(customer) 表中删除客户 "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; 亲自试一试 »

学习训练

练习题:

Customers 表中删除 Country 值为 'Norway' 的所有记录。 p>

 Customers
 Country = 'Norway';

开始练习



0 人点赞过