扫码一下
查看教程更方便
SQL DELETE 语句用于从表中删除现有记录。
可以在 DELETE 语句中使用 WHERE 子句来删除选定的行,否则所有记录都将被删除。
带有 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 表没有任何记录。