![d81a8ac6412c5e8c3b1694582f78e921.png](https://img-blog.csdnimg.cn/img_convert/d81a8ac6412c5e8c3b1694582f78e921.png)
本文使用 Zhihu On VSCode 创作并发布
之前我们已经介绍过,数据库中的表不再使用时,可以使用 drop 语句删除表,语法如下:
drop table <表名>;
但如果只是想删除表中满足特定条件的数据,保留其他数据时,drop 语句就派不上用场了,这个时候可以使用 delete 语句。
delete 语句的使用语法如下:
delete from <表名>
where <条件>;
不指定条件的删除
delete 语句使用时可以不指定 where 条件,比如删除 product 表中的所有数据,delete from product 即可。
delete from product 和 drop table product 的区别在于: delete 操作并不会删除表,表仍然在,只是表中的数据为空,而 drop 操作会删除表,执行后表就不存在了。
指定条件的删除
比如删除 product 表中商品种类 product_type 为办公用品的
delete from product where product_type = '办公用品'
执行前 product 表的数据如下:
Image 执行后 product 表的数据如下:
Image
需要注意的是,与 select 语句不同,delete 语句中不能使用 group by、having 和 order by 三类子句,而只能使用 where 子句,因为删除表时这些操作都起不到什么作用。