当前位置:网站首页>Interview shock 58: Six differences among truncate, delete and drop!
Interview shock 58: Six differences among truncate, delete and drop!
2022-06-22 17:22:00 【JAVA Chinese community】

author | Lei brother
source | Java Analysis of the real interview questions (ID:aimianshi666)
Please contact authorization for reprint ( WeChat ID:GG_Stone)
stay MySQL in , Use truncate、delete and drop Can be deleted , But they 3 The usage scenarios and execution effects of are completely different , Next, let's take an inventory .
truncate、delete、drop Overview of differences
they 3 The differences between the two are shown in the following table :
| Difference point | drop | truncate | delete |
|---|---|---|---|
| Execution speed | fast | Faster | slow |
| Order classification | DDL( Data definition language ) | DDL( Data definition language ) | DML( Data operation language ) |
| Delete object | Delete the entire table and table structure , And the index of the table 、 Constraints and triggers . | Delete only table data , The structure of the table 、 Indexes 、 Constraints, etc. are preserved . | Delete only all or part of the table data , Table structure 、 Indexes 、 Constraints, etc. are preserved . |
| Delete the condition (where) | Out-of-service | Out-of-service | You can use |
| Roll back | Cannot roll back | Cannot roll back | Roll back |
| Self increasing initial value | - | Reset | No reset |
Next, we use cases to demonstrate the difference between them .
preparation
Before the official start , Let's create a user table and user test data , It is convenient for subsequent demonstration :
CREATE TABLE `userinfo` (
`id` int(11) NOT NULL AUTO_INCREMENT comment ' Number ',
`name` varchar(250) NOT NULL comment ' full name ' unique,
`balance` decimal(10,2) NOT NULL DEFAULT '0.00' comment ' The account balance ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
insert into userinfo values(1,' Zhang San ',1000),(2,' Li Si ',500),(3,' Wang Wu ',2000),(4,' Li Liu ',500); The table structure and data created are shown in the following figure :
1. Deleting objects is different
delete and truncate Delete only table data , Do not delete table structure , among delete The result after deletion is as follows :
Let's restore the table to its original state , Reuse truncate Delete , The execution result is shown in the figure below :
Restore the table to its original state , perform drop Delete statements , The execution result is shown in the figure below :
As can be seen from the above results ,delete and truncate Delete only table data , and drop Delete the table structure and table data .
2. Deletion conditions support different
truncate and drop Adding... Is not supported where Conditions , and delete Support where Conditions , As shown in the figure below :
3. Command categories are different
truncate、delete and drop Belongs to SQL Different categories ,SQL Divided into the following 3 class :
DDL【Data Definition Language】 Data definition language , The structure used to maintain stored data represents instructions : create、drop、alter、truncate.
DML【Data Manipulation Language】 Data manipulation language , It is used to operate on data :insert,delete,update,DML There is another one in the DQL, Data query language , Delegate instruction is select.
DCL【Data Control Language】 Data control language , Mainly responsible for authority management and affairs representative instructions :grant,revoke,commit.
among delete Belong to DML, and truncate and drop Belong to DDL.
PS:truncate Copy a new table structure first , Delete the old table structure and data together , So it belongs to the data definition language DDL, Not a data manipulation language DML.
4. Rollback support is different
delete Belong to DML Support transaction rollback , and truncate and drop Belong to DDL, It will take effect immediately after implementation , And the data is unrecoverable , Now let's verify . First of all MySQL Automatic transaction commit close for , The default value for automatic transaction commit is “ON” That is, automatic submission is enabled , As shown in the figure below :
We use the following command to automatically submit ( Business ) Turn off the :
set autocommit=off; Query the settings of automatic transaction submission again. The results are as follows :
Now let's demonstrate delete Rollback operation for , As shown in the figure below :
As can be seen from the above results delete Then you can restore ( Roll back ) Of , and truncate and drop After that, it cannot be rolled back , You old iron can use the same method to test the latter two SQL Implementation .
5. Autoincrement initialization is different
delete The initial value of the auto increment field will not be reset , As shown in the figure below :
and truncate Will reset the initial value of the auto increment field , As shown in the figure below :
6. Different execution speeds
delete It's line by line , And the operation log will be recorded during execution , For future rollback , therefore delete The execution speed of is relatively slow ; and truncate First, copy a new table structure , Delete the original table as a whole , So its execution speed is in the middle , and drop The fastest execution speed .
summary
truncate、drop and delete The main differences are as follows 6 spot :
Execution speed :drop > truncate > detele.
delete and truncate Delete only table data , and drop The table data, table structure and table index will be deleted 、 Constraints and triggers .
delete You can add where The condition enables partial data deletion , and truncate and drop Cannot add where The condition is to delete as a whole .
truncate and drop It's immediate execution , And it can't be recovered ; and delete I can walk away from business , Can be withdrawn and restored .
truncate Will reset the auto increment column to 1, and delete Autoincrement columns are not reset .
truncate and drop yes DDL sentence , and delete yes DML sentence .
It's up to you to judge right and wrong , Disdain is to listen to people , Gain or loss is more important than number .
official account :Java Analysis of the real interview questions
Interview collection :https://gitee.com/mydb/interview

Previous recommendation Java Weekly summary of interview questions ( common 57 piece )| The latest version
Interview shock 57: Cluster index = Primary key index ?
Interview shock 56: What is the difference between clustered index and non clustered index ?
Interview shock 55:delete、drop、truncate What's the difference? ?

边栏推荐
- AD20/Altium designer——过孔盖油
- STM32 ADC acquisition via DMA (HAL Library)
- MySQL master-slave connection prompt of docker: communications link failure
- 使用 POST 向 ASP.NET Core 传递数据时的长度限制与解决方案
- Xshell 7 (SSH Remote Terminal tool) v7.0.0109 official Chinese Version (with file + installation tutorial)
- Analysis of the read data source code of spark shuffle
- Spark streaming receiver startup and data receiving
- Hello playwright: (7) simulate keyboard and mouse
- Why buy increased life insurance? Is increased life insurance safe and reliable?
- Parallel通过XCM与Moonbeam集成,将PARA以及DeFi用例带入Moonbeam生态
猜你喜欢

Xftp 7 (ftp/sftp client) v7.0.0107 official Chinese free official version (with file + installation tutorial)

System throughput, TPS (QPS), user concurrency, performance test concepts and formulas

clickhouse 21. X cluster four piece one copy deployment

Hello Playwright:(7)模拟键盘和鼠标

Mybaits:接口代理方式实现Dao

内容推荐流程

Hello playwright: (7) simulate keyboard and mouse

AD20/Altium designer——过孔盖油

关于#数据库#的问题,如何解决?

UI自动化定位利器-xpath实战
随机推荐
[mysql] data synchronization prompt: specified key was too long; max key length is 767 bytes
WPF effect chapter 190: playing listbox again
网传学习通1.7亿密码泄露!有什么补救措施?
Redis实现延迟队列的正确姿势
[Alibaba cloud server - install MySQL version 5.6 and reinstall]
High availability ResourceManager
系统吞吐量、TPS(QPS)、用户并发量、性能测试概念和公式
. NETCORE enables image scaling and cropping - based on imagesharp
Docker之MySQL主从连接提示:Communications link failure
Partage de l'architecture du système de paiement du Groupe letv pour traiter 100 000 commandes simultanées élevées par seconde
启牛学堂给的中信建投证券账户是不是真的?开户安全吗
.NET 发布和支持计划介绍
Test for API
Vs2017 solution to not displaying qstring value in debugging status
快速掌握 ASP.NET 身份认证框架 Identity - 登录与登出
0 basic how to get started software testing, can you succeed in changing careers?
mysql服务器启动后自动停止
[step 1 of advanced automated testing] 1 minute to introduce you to automated testing
JMeter use case
clickhouse 21.x 集群四分片一副本部署



