当前位置:网站首页>MySQL数据库:drop、truncate、delete的区别
MySQL数据库:drop、truncate、delete的区别
2022-06-29 13:05:00 【全栈程序员站长】
1、删除速度:drop>truncate>delete;
2、删除方式:
- (1)执行delete操作时,每次从表中删除一行,并且同时将该行的的删除操作记录在日志文件中。delete可根据where条件删除表中指定的数据,如果不指定where子句,则跟truncate一样,可以删除表中所有记录。
- (2)truncate通过释放存储表数据所用的页来删除数据,一次性删除表中所有记录,不会将删除的数据记录在日志文件中,只在日志文件中记录页的释放,使用的系统和事务日志资源少,所以执行速度比delete快。对于外键约束引用的表,不能使用 truncate table,而应使用不带 where 子句的 delete 语句。
- (3)drop则删除整个表的结构和数据,但是被依赖的约束、触发器、索引、依赖于该表的存储过程、函数将被保留,但其状态会变为invalid
3、删除后,表和索引所占空间:
- (1)delete操作不会减少表或索引所占用的空间;
- (2)当表被truncate后,这个表和索引所占用的空间会恢复到初始大小,但表结构及其列、约束、索引的定义等保持不变。
- (3)drop语句将表所占用的空间全释放掉。
4、提交方式:delete是DML,需要手动提交操作才能生效,可以回滚,可以触发触发器;truncate和drop是DDL,会隐式提交,不能回滚,不会触发触发器。
5、使用场景:
- (1)如果想删除表,当然用drop;
- (2)如果想保留表而将所有数据删除,而且和事务无关,用truncate即可;
- (3)如果和事务有关,或者想触发trigger,还是用delete;
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/100076.html原文链接:https://javaforall.cn
边栏推荐
- 如何让 Dapper 支持 DateOnly 类型
- mysql函数和约束
- urllib urllib2
- Uncover the practice of Baidu intelligent test in the field of automatic test execution
- Go unit testing introductory practice
- Normalization layer of pytorch learning (batchnorm, layernorm, instancenorm, groupnorm) [easy to understand]
- “死掉”的诺基亚,一年躺赚1500亿
- 揭秘百度智能测试在测试自动执行领域实践
- goby如何导出扫描结果
- 微信小程序:修复采集接口版云开发表情包
猜你喜欢
随机推荐
weserver發布地圖服務
Equivalence class partition method for test case design method
【 网络带宽 】MBps & Mbps
win11怎么看cpu几核几线程? win11查看cpu是几核几线程的教程
Problems in replacing RESNET convolution of mmdet with ghost convolution group
mysql函数和约束
Why is the integration of storage and computing a new direction of core making Collision school x Zhicun Technology
“死掉”的诺基亚,一年躺赚1500亿
College girls wear cheongsam to defend! Netizen: the tutor said it would be nice if the paper were as beautiful as the cheongsam
MySQL数据库:使用show profile命令分析性能
Openssl证书工具使用手册
打造一个 API 快速开发平台,牛逼!
I talked about exception handling for more than half an hour during the interview yesterday
STM32 watchdog study
Yyds dry inventory solution sword finger offer: find the nearest common ancestor of two nodes in the binary tree
微信小程序:图片秒加水印制作生成
MySQL数据库:读写分离
Interview high concurrent, cool!! (high energy in the whole process, collection recommended)
在线文本过滤小于指定长度工具
Game development of contract quantitative trading system (ready-made case analysis)









