当前位置:网站首页>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
边栏推荐
- 电脑时间校对后不自动更新要如何解决
- 超 Nice 的表格响应式布局小技巧
- PHP FPM startup parameters and important configuration details
- System.currentTimeMillis() 和 System.nanoTime() 哪个更快?大部分人都会答错!
- Getting started with SQLite3
- 微信小程序:装B神器P图修改微信流量主小程序源码下载趣味恶搞图制作免服务器域名
- [graduation season · advanced technology Er] 10.76 million graduates, the most difficult employment season in history? I can't roll it up again. I lie down again and again. Where is the road?
- Cloud native (31) | kubernetes chapter kubernetes platform basic pre installed resources
- win11怎么看cpu几核几线程? win11查看cpu是几核几线程的教程
- systemd调试
猜你喜欢

Openssl证书工具使用手册

Application of ansvc reactive power compensation device in a shopping mall in Hebei

Wechat applet: repair collection interface version cloud development expression package
![[document translation] camouflaged object detection](/img/30/73a927c05173a95cc5a5d51e182e3b.png)
[document translation] camouflaged object detection

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

微信小程序:(更新)云开发微群人脉

Wechat applet: new and exclusive cloud development wechat group contacts

I talked about exception handling for more than half an hour during the interview yesterday

WinDbg debugging tool introduction

Equivalence class partition method for test case design method
随机推荐
丢弃 Tkinter!简单配置快速生成超酷炫 GUI!
Deecamp2022 officially opened! Likaifu and zhangyaqin personally teach master courses 𞓜 innovation
What if the excel table exported from the repair record is too large?
喜迎市科协“十大”•致敬科技工作者 | 卢毅:守护电网传输安全的探索者
weserver发布地图服务
STM32 watchdog study
[cloud resident co creation] break through the performance bottleneck of image recognition through rust language computing acceleration technology
台式机主板上保护cpu的盖子安装和拆卸
纳人才,谋发展 | 人大金仓喜获“最佳雇主校招案例奖”
Why is the integration of storage and computing a new direction of core making Collision school x Zhicun Technology
如何让 Dapper 支持 DateOnly 类型
Leetcode question brushing: String 07 (repeated substring)
Industry analysis - quick intercom, building intercom
MySQL数据库:使用show profile命令分析性能
systemd调试
投资人跌下神坛:半年0出手,转行送外卖
I talked about exception handling for more than half an hour during the interview yesterday
How to install MySQL 8.0 on rocky Linux and almalinux
Grep exact match
Intuition and Implementation: batch normalization