当前位置:网站首页>What is the difference between delete, drop and truncate in MySQL
What is the difference between delete, drop and truncate in MySQL
2022-06-09 13:49:00 【1024 Q】
Preface :
1.delete
1.1 delete Realization principle
1.2 About the auto increment column
2.truncate
2.1 truncate Realization principle
2.2 Reset auto increment column
3.drop
4. Differences among the three
summary
Preface :stay MySQL in , The total number of deleted methods is 3 Kind of :delete、truncate、drop, The usage and usage scenarios of the three are completely different , Let's look at it in detail .
1.deletedetele Can be used to delete some or all of the data in a table , Its usage syntax is as follows :
delete from table_name [where...] [order by...] [limit...]PS:[] The commands in are optional , Can be omitted .
If we want to delete the top math scores in the student list 3 Students , You can use the following SQL:
delete from student order by math desc limit 3;1.1 delete Realization principle stay InnoDB In the engine ,delete The operation does not really delete the data , Instead, the data is marked for deletion , Marked as deleted , We can do this by MySQL Set to non auto submit mode , To test and verify .
Settings for non auto submit mode SQL as follows :
set autocommit=0;After that, a data delete Delete the , And then use rollback Rollback operation , Finally, verify whether the data we deleted before still exists , If the data still exists, it means delete The data is not really deleted , It just identifies the data as deleted , verification SQL And execution results are shown in the figure below :

stay InnoDB In the engine , Used delete After deleting all the data , The auto increment column is not reset to the initial value , We can use the following command to verify :

truncate Execution effect and delete similar , It is also used to delete all row data in the table , Its usage syntax is as follows :
truncate [table] table_nametruncate In use and delete The big difference ,delete You can use conditional expressions to delete some data , and truncate Cannot add conditional expression , So it can only delete all row data , For example, the following truncate Added where An error will be reported after the command :

truncate It seems that only row data has been deleted , But it is DDL sentence , That is to say Data Definition Language Data definition language , It is used to maintain the structure instructions of stored data , So this is also the same as delete Commands are different ,delete The statement belongs to DML,Data Manipulation Language Data manipulation language , Used to manipulate data . Why? truncate Only the row data is deleted , Column data was not deleted ( Data such as fields and indexes ) nevertheless DDL Language ? This is because truncate In essence, a new table structure is created , Delete the original table , So it belongs to DDL Language , Instead of DML Language .
2.2 Reset auto increment columntruncate stay InnoDB The auto incrementing column will be reset in the engine , The following command shows :

drop Unlike the previous two commands, which only delete the row data of the table ,drop The row data of the entire table and the table structure will be deleted , Its syntax is as follows :
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [,tbl_name]among TEMPORARY It means temporary table , In general, this command will be ignored .
drop An example is as follows :

Data recovery :delete Can recover deleted data , and truncate and drop Cannot recover deleted data .
In terms of execution speed :drop > truncate > delete.
Delete data :drop Delete the entire table , Contains row data and fields 、 Index and other data , and truncate and drop Only row data is deleted .
Add conditional aspect :delete have access to where Expression to add query criteria , and truncate and drop Can not add where Query criteria .
Reset auto incrementing aspect : stay InnoDB In the engine ,truncate Auto incrementing columns can be reset , and delete Auto incrementing columns cannot be reset .
summarydelete、truncate Can be used to delete row data in a table , and drop Is to delete the whole table , The deleted data contains all row data and fields 、 Index and other data , among delete Deleted data can be recovered , and truncate and drop Is irrecoverable , But in terms of execution efficiency , The latter two deletion methods have great advantages , Therefore, select the corresponding delete command according to the actual scene , Of course truncate and drop You should also be careful when using the deletion method of these unrecoverable data .
This is about mysql Medium delete,drop and truncate That's all for the difference , More about mysql delete drop truncate Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- "Forget to learn again" shell Basics - 28. Description of conditional expressions in awk
- 射频同轴连接器和电缆指南--【转自微信公众号射频课堂】
- Explain asynchronous tasks in detail: the task of function calculation triggers de duplication
- 15 Uncaught TypeError: Cannot set properties of null (setting ‘onclick‘)
- 面试题 05.08. 绘制直线
- 【C语言练习——合并两个有序序列】
- Lossy transmission instance
- 使用nodejs导出md/Markdown文档当中的图片到本地并替换原始图片链接为本地图片链接
- 云呐|数据库监控一般监控什么
- jstat 详解
猜你喜欢
随机推荐
"Forget to learn again" shell Basics - 28. Description of conditional expressions in awk
Prototype chain? New misconceptions
对某快捷酒店一次内网测试
记忆化搜索+状态压缩leetcode.464
详解mysql数据去重的三种方式
Database day-3
[C language practice - merging two ordered sequences]
Yunna RFID asset management, advantages of RFID asset management system
面试题 08.01. 三步问题
云呐|公司实物资产如何管理
6000 字+,帮你搞懂互联网架构演变历程!
云呐|智能运维管理系统平台,可视化运维系统管理
Jstat details
软件测试工程师手把手教你如何制定测试计划
【C语言练习——打印菱形及其变形】
5G发牌三周年 云网融合加速 如何解决企业网络之忧?
在腾讯连拿六个五星(下)
C语言 结构体 | 链表
The curl post request carries the request header and passes the command to receive parameter data
Database installation --mysql







