SQL DELETE

SQL DELETE 语句

DELETE 语句用于删除表中的行。

SQL DELETE 语法

DELETE FROM table_name
WHERE some_column=some_value;

请注意 SQL DELETE 语句中的 WHERE 子句!
WHERE 子句规定哪条记录或者哪些记录需要删除。如果您省略了 WHERE 子句,所有的记录都将被删除!

演示数据库

在本教程中,我们将使用 Web3 样本数据库。

下面是选自 "Websites" 表的数据:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 芝麻教程      | http://www.web3.xin/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
|  6 | 百度         | https://www.baidu.com/    |     4 | CN      |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

SQL DELETE 实例

假设我们要从 "Websites" 表中删除网站名为 "百度" 且国家为 CN 的网站 。

我们使用下面的 SQL 语句:

实例

DELETE FROM Websites
WHERE name='百度' AND country='CN'; 
执行以上 SQL,再读取 "Websites" 表,数据如下所示:
mysql> delete from websites where name= '百度'AND country = 'CN';
Query OK, 1 row affected (0.00 sec)

mysql> select * from websites;
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    | 1     | USA     |
|  2 | 淘宝          | https://www.taobao.com/   | 13    | CN      |
|  3 | 芝麻教程      | http://www.web3.xin/      | 5000  | USA     |
|  4 | 微博          | http://weibo.com/         | 20    | CN      |
|  5 | Facebook      | https://www.facebook.com/ | 3     | USA     |
|  9 | stackoverflow | http://stackoverflow.com/ | NULL  | IND     |
+----+---------------+---------------------------+-------+---------+
6 rows in set (0.00 sec)

删除所有数据

您可以在不删除表的情况下,删除表中所有的行。这意味着表结构、属性、索引将保持不变:

DELETE FROM table_name;

or

DELETE * FROM table_name;

注释在删除记录时要格外小心!因为您不能重来!

联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2024

备案号:京ICP备15003423号-3