📅  最后修改于: 2020-12-29 04:34:34             🧑  作者: Mango
SQL DELETE查询用于从表中删除现有记录。
您可以将WHERE子句与DELETE查询一起使用,以删除选定的行,否则将删除所有记录。
带WHERE子句的DELETE查询的基本语法如下-
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 | 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表将没有任何记录。