当前位置:网站首页>Delete in MySQL: the difference between delete, drop and truncate
Delete in MySQL: the difference between delete, drop and truncate
2022-07-27 10:50:00 【Golden% sunset】
MySQL in delete、drop、Truncate The difference between
The same thing : Both have deletion function .
The three difference :
| delete | truncate | drop | |
|---|---|---|---|
| type | Belong to DML | Belong to DDL | Belong to DDL |
| Roll back | Roll back | Cannot roll back | Cannot roll back |
| Delete content | Delete all or part of a table's data rows | Delete all data in the table | Delete tables from the database |
| Delete speed | Slow to delete | Delete fast | Delete the fastest |
Three diagrams :

Detailed explanation of the three
delete
Delete content , Do not delete table structure , Don't free up space .
usage :
-- Delete the whole table data
DELETE FROM table_name
-- Delete some data
DELETE FROM table_name
WHERE condition
1、DELETE Belongs to the database DML Operating language , Delete only the data, not the structure of the table , I can walk away from business , It will trigger trigger;
2、 stay InnoDB in ,DELETE It doesn't really delete the data ,MySQL In fact, it just marks the deleted data as deleted , therefore delete When deleting data from a table , Table files will not take up less space on disk , Storage will not be released , Just make the deleted data rows invisible . Although no disk space has been released , But the next time you insert data , You can still reuse this space ( reusing → Cover ).
3、DELETE Execution time , It will cache the deleted data to rollback segement in , Business commit Effective after ;
4、delete from table_name Delete all data from the table , about MyISAM Will immediately free up disk space ,InnoDB It won't free up disk space ;
5、 about delete from table_name where xxx Conditional deletion , Whether it's InnoDB still MyISAM Will not free up disk space ;
6、delete Use after operation optimize table table_name Will immediately free up disk space . Whether it's InnoDB still MyISAM . So in order to free up disk space ,delete Later optimize table operation .
drop
drop Statement to delete the table structure and all data , And release all the space occupied by the table .
usage :
-- Delete the whole table data
DROP TABLE table_name
1、drop: Belongs to the database DDL Define language , Same as Truncate; Take effect immediately , Unable to retrieve
2、drop table table_name Free up disk space immediately , Whether it's InnoDB and MyISAM;
3、drop Statement will delete the constraint that the structure of the table is dependent on (constrain)、 trigger (trigger)、 Indexes (index); Stored procedures that depend on the table / The function will keep , But it became invalid state .
Truncate
Delete the contents of the table , Do not delete table structure , Release space .
usage :
-- Delete the whole table data
TRUNCATE TABLE table_name
1、truncate Belongs to the database DDL Define language , No business , The original data will not be put into rollback segment in , Operation does not trigger trigger, Take effect immediately , Unable to retrieve .
2、truncate table table_name Free up disk space immediately , Whether it's InnoDB and MyISAM .truncate table It's kind of like drop table then create, It's just this create table The process has been optimized , For example, the table structure file has already existed before, and so on . So the speed should be close to drop table The speed of .
3、truncate Can quickly clear a table . And reset auto_increment Value . But for different types of storage engines, what you need to pay attention to is : about MyISAM,truncate Reset auto_increment( Autogenous sequence ) The value of is 1. and delete The back table remains auto_increment. about InnoDB,truncate Reset auto_increment The value of is 1.delete The back table remains auto_increment. But doing delete Restart after the entire table MySQL Words , After the restart auto_increment Will be set to 1. in other words ,InnoDB The table itself cannot be persisted auto_increment.delete After the table auto_increment Still in memory , But it was lost after the restart , Only from 1 Start . Essentially after the restart auto_increment From SELECT 1+MAX(ai_col) FROM t Start .
Execution speed
drop> truncate > delete
Security
Careful use drop and truncate, Especially when there is no backup .
On use , Want to delete some data lines delete, Pay attention to take where Clause . The rollback segment should be large enough .
Use advice
Delete table completely [drop]
Want to keep the table and delete all the data . If it's not about business [truncate]
If it's about business , Or want to trigger trigger[delete]
边栏推荐
- Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
- WEB服务如何平滑的上下线
- Oracle 11g manual memory management
- Li Kou brush question 02 (sum of three numbers + sum of maximum subsequence + nearest common ancestor of binary tree)
- phpstudy中Apache无法启动
- Loop traversal of foreach and some of ES6
- Share machine learning notes (PDF version) + practical projects (dataset + code)
- Customized modification based on jira7.9.2
- 让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
- Webrtc realizes simple audio and video call function
猜你喜欢

MySQL数据表的高级操作

Overview of user space lock on mobile platform

Codeforces Round #807 (Div 2.) AB

让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...

Customized modification based on jira7.9.2

全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL

PHP generates text and image watermarks

Basic statement of database operation

想要一键加速ViT模型?试试这个开源工具!

jvm--字节码浅析
随机推荐
Solved syntaxerror: (Unicode error) 'Unicode scape' codec can't decode bytes in position 2-3: truncated
分享机器学习笔记(PDF版)+实战项目(数据集+代码)
Shardingproxy sub database and table actual combat and comparison of similar products
[brother hero June training] day 28: dynamic planning
Program translation and execution, from editing, preprocessing, compilation, assembly, linking to execution
TensorFlow笔记——基本函数及概念
华硕无双,这可能是屏幕最好的平价高刷轻薄笔记本
MySQL 索引、事务与存储引擎
【Liunx】安装Redis
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
Family Trivia
TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
Tdengine business ecosystem partner recruitment starts
Oracle resizing data files
Samba server
分布式块设备复制:客户端
阿里邮箱web端登录转圈的处理
It is thought-provoking: is syntax really important? Qiu Xipeng group proposed a powerful baseline for aspect based emotional analysis
Redis数据结构分析(二)