SQL DELETE删除查询

SQL DELETE查询用来从表中删除现有的记录。

您可以使用WHERE子句限定DELETE查询删除所选行,否则,所有的记录将被删除。

语法

DELETE查询使用WHERE子句的基本语法如下:

DELETE FROM table_name
WHERE [condition];

您可以使用AND或OR运算符组合N多个条件。

例子:

考虑CUSTOMERS表具有以下记录:

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

下面是一个例子,它会删除一个ID是6的客户:

SQL> DELETE FROM CUSTOMERS
WHERE ID = 6;

现在,CUSTOMERS表中有以下记录(ID=6的记录已经被删除了):

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

如果你希望CUSTOMERS表中所有的记录删除,你不需要使用WHERE子句,完整的DELETE查询如下:

SQL> DELETE FROM CUSTOMERS;

现在,CUSTOMERS表将不会有任何记录。


链接: https://fly63.com/course/32_1546