当前位置:网站首页>MySQL中的删除:delete、drop、Truncate三者的区别
MySQL中的删除:delete、drop、Truncate三者的区别
2022-07-17 04:08:00 【金色%夕阳】
相同点:都具有删除功能。
三者区别:
| delete | truncate | drop | |
|---|---|---|---|
| 类型 | 属于DML | 属于DDL | 属于DDL |
| 回滚 | 可回滚 | 不可回滚 | 不可回滚 |
| 删除内容 | 删除表的全部或者一部分数据行 | 删除表中的所有数据 | 从数据库中删除表 |
| 删除速度 | 删除速度慢 | 删除速度快 | 删除速度最快 |
三者图解:

三者详解
delete
删除内容,不删除表结构,不释放空间。
用法:
-- 删除整表数据
DELETE FROM table_name
-- 删除部分数据
DELETE FROM table_name
WHERE condition
1、DELETE属于数据库DML操作语言,只删除数据不删除表的结构,会走事务,执行时会触发trigger;
2、在 InnoDB 中,DELETE其实并不会真的把数据删除,MySQL实际上只是给删除的数据打了个标记为已删除,因此 delete 删除表中的数据时,表文件在磁盘上所占空间不会变小,存储空间不会被释放,只是把删除的数据行设置为不可见。虽然未释放磁盘空间,但是下次插入数据的时候,仍然可以重用这部分空间(重用 → 覆盖)。
3、DELETE执行时,会先将所删除数据缓存到rollback segement中,事务commit之后生效;
4、delete from table_name删除表的全部数据,对于MyISAM 会立刻释放磁盘空间,InnoDB 不会释放磁盘空间;
5、对于delete from table_name where xxx 带条件的删除, 不管是InnoDB还是MyISAM都不会释放磁盘空间;
6、delete操作以后使用 optimize table table_name 会立刻释放磁盘空间。不管是InnoDB还是MyISAM 。所以要想达到释放磁盘空间的目的,delete以后执行optimize table 操作。
drop
drop语句删除表结构及所有数据,并将表所占用的空间全部释放。
用法:
-- 删除整表数据
DROP TABLE table_name
1、drop:属于数据库DDL定义语言,同Truncate;执行后立即生效,无法找回
2、drop table table_name 立刻释放磁盘空间 ,不管是 InnoDB 和 MyISAM;
3、drop 语句将删除表的结构被依赖的约束(constrain)、触发器(trigger)、索引(index); 依赖于该表的存储过程/函数将保留,但是变为 invalid 状态。
Truncate
删除表中的内容,不删除表结构,释放空间。
用法:
-- 删除整表数据
TRUNCATE TABLE table_name
1、truncate属于数据库DDL定义语言,不走事务,原数据不放到 rollback segment 中,操作不触发 trigger,执行后立即生效,无法找回。
2、truncate table table_name 立刻释放磁盘空间 ,不管是 InnoDB和MyISAM 。truncate table其实有点类似于drop table 然后create,只不过这个create table 的过程做了优化,比如表结构文件之前已经有了等等。所以速度上应该是接近drop table的速度。
3、truncate能够快速清空一个表。并且重置auto_increment的值。但对于不同的类型存储引擎需要注意的地方是:对于MyISAM,truncate会重置auto_increment(自增序列)的值为1。而delete后表仍然保持auto_increment。对于InnoDB,truncate会重置auto_increment的值为1。delete后表仍然保持auto_increment。但是在做delete整个表之后重启MySQL的话,则重启后的auto_increment会被置为1。也就是说,InnoDB的表本身是无法持久保存auto_increment。delete表之后auto_increment仍然保存在内存,但是重启后就丢失了,只能从1开始。实质上重启后的auto_increment会从 SELECT 1+MAX(ai_col) FROM t 开始。
执行速度
drop> truncate > delete
安全性
小心使用drop 和truncate,尤其没有备份的时候。
使用上,想删除部分数据行用delete,注意带上where子句. 回滚段要足够大。
使用建议
完全删除表[drop]
想保留表而将所有数据删除. 如果和事务无关[truncate]
如果和事务有关,或者想触发trigger[delete]
边栏推荐
- Wechat e-book reading applet graduation project (6) opening defense ppt
- By voting for the destruction of STI by Dao, seektiger is truly community driven
- Nearly 90% of servers can be saved, but the anti fraud efficiency has increased significantly. Why is PayPal's plan to break the "Ai memory wall" so cost-effective?
- Technical writing guide for programmers to leave work early
- C# 使用this关键字串联构造函数调用方法
- [database] must know and be able at the end of the term ----- Chapter 1 database overview
- C # explain out output parameters in detail
- minimum spanning tree
- Small program completion work wechat online education video on demand learning small program graduation design (2) small program function
- Cocos creator 3.0 foundation - event system
猜你喜欢

leetcode7-dfs+动态规划+双指针

小程序毕设作品之微信电子书阅读小程序毕业设计(2)小程序功能

Wechat e-book reading of applet completion works (7) Interim inspection report
![[Huang ah code] Introduction to MySQL - 5. Database tips: a single column group by will, and multiple columns?](/img/4d/552c4251779705a6dfd4ceb59c4261.png)
[Huang ah code] Introduction to MySQL - 5. Database tips: a single column group by will, and multiple columns?

OSPF anti ring

Wechat e-book reading applet graduation design of applet completion works (2) applet function

PAC Decade: witness HPC from CPU era to XPU Era

CAD视频课程推荐 b站

2022/7/16 周赛

Wechat Online Education video on Demand Learning of applet Graduation Design (3) Background Function
随机推荐
ospf防环
Thesis research NLP
Distributed notes (02) - redis of distributed cache (brief description of uses, features, high availability solutions redis cluster, tweetproxy, CODIS)
Simple explanation of C constructors
WPF cannot find resource file problem
SQL interface switching cannot obtain focus
牛客2021训练联盟热身训练赛Interstellar Love(并查集)
AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
Intel + Lenovo jointly launched open source cloud solutions
Optimization and configuration of OSPF
微信附近的人小程序怎么开(开通附近小程序的方法)
C# 构造函数(Constructors)简单讲解
分布式笔记(02)— 分布式缓存之 Redis(用途、特点、高可用方案 Redis Cluster、Twemproxy、Codis 简要说明)
Heartless sword Chinese English bilingual poem 005 Lyric
The adaptation of go language under windows10:vscode
若依框架包名修改器
MAUI 框架入门学习05 MVVM数据模型理解
机器学习11:代价敏感学习
Small program completion work wechat online education video on demand learning small program graduation design (2) small program function
T+0变T+1!快赎金额降至1万!又有银行调整现金理财产品申赎规则