当前位置:网站首页>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
边栏推荐
- WinDbg debugging tool introduction
- urllib urllib2
- Distributed cache for memcached
- 超 Nice 的表格响应式布局小技巧
- 逆向调试入门-PE文件节表与区块03/07
- System. Currenttimemillis() and system Nanotime() which is faster? Most people get the wrong answer!
- Equivalence class partition method for test case design method
- College girls wear cheongsam to defend! Netizen: the tutor said it would be nice if the paper were as beautiful as the cheongsam
- c语言入门教程–-6循环语句
- What if the excel table exported from the repair record is too large?
猜你喜欢

关于MongoDB报错:connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

【毕业季·进击的技术er】1076万毕业生,史上最难就业季?卷又卷不过,躺又躺不平,敢问路在何方?

节点数据采集和标签信息的远程洪泛传输

Mondo rescue creates an image file (which is conducive to image damage recovery)

Stable currency risk profile: are usdt and usdc safe?

动荡的中介生意,不安的租房人

如何优雅的写 Controller 层代码?

中康控股开启招股:拟募资净额3.95亿港元,预计7月12日上市

记一次 .NET 某物管后台服务 卡死分析

Korean AI team plagiarizes the shock academic world! One tutor with 51 students, or plagiarism recidivist
随机推荐
June training (day 29) - divide and rule
win10安装Monggodb的基本使用教程
[untitled] error in installation dependency: refusing to install package with name "* * *" under a package
测试用例设计方法之等价类划分方法
koa2+better-sqlite3实现增删改查
WinDbg common commands
sqlite3入门
How to make dapper support dateonly type
【云驻共创】通过Rust语言计算加速技术突破图片识别性能瓶颈
如何让 Dapper 支持 DateOnly 类型
WinDbg debugging tool introduction
Matlab fmincon precision, fmincon and quadprog error
打造一个 API 快速开发平台,牛逼!
ES6 array method
[graduation season] it's worth it all the way over the past four years -- advice from the senior students
golang6 反射
Why is the integration of storage and computing a new direction of core making Collision school x Zhicun Technology
leetcode:226. 翻转二叉树
Mondo rescue creates an image file (which is conducive to image damage recovery)
微信小程序:图片秒加水印制作生成