当前位置:网站首页>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? ?

边栏推荐
- DAP事实表加工汇总功能应用说明
- Analysis of the read data source code of spark shuffle
- Recommend 7 super easy-to-use terminal tools - ssh+ftp
- 来厦门了!线上交流限额免费报名中
- 【招聘】[北京中关村/远程][TensorBase][开源数据仓库]等一群人,做一件事
- Test for API
- Basic application of scala for
- Simple integration of client go gin -update
- WPF achieves star effect
- Blazor University (30)表单 —— 从 InputBase 派生
猜你喜欢

Partage de l'architecture du système de paiement du Groupe letv pour traiter 100 000 commandes simultanées élevées par seconde

Kibana+elk cluster log processing

##Kibana+ELK集群日志处理

你管这破玩意儿叫高可用?

Description of new features and changes in ABP Framework version 5.3.0

来厦门了!线上交流限额免费报名中

Quickly master asp Net authentication framework identity - user registration

High availability ResourceManager

Mybaits: interface proxy implementation Dao

mysql5.7.27安装之windows8.1 64
随机推荐
clickhouse 21.x 集群四分片一副本部署
Spark on data skew
试用了多款报表工具,终于找到了基于.Net 6开发的一个了
Content recommendation process
面试突击58:truncate、delete和drop的6大区别!
Examples of MySQL account addition, deletion, modification, data import and export commands
[mysql] data synchronization prompt: specified key was too long; max key length is 767 bytes
The MySQL of docker restarts, prompting error response from daemon: driver failed programming external connectivity on**
团队管理|如何提高技术 Leader 的思考技巧?
[recruitment] [Beijing Zhongguancun / remote] [tensorbase][open source data warehouse] and other people do one thing
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
Seeing the big from the small: a simple example of domain modeling, understanding "domain driven".
Xshell 7 (SSH Remote Terminal tool) v7.0.0109 official Chinese Version (with file + installation tutorial)
股票在哪家的平台买比较安全呢?
Blazor University (30)表单 —— 从 InputBase 派生
Apache ShardingSphere 一文读懂
WPF achieves star effect
What is a flush? Is online account opening safe?
Mybaits: interface proxy implementation Dao
Social responsibility: GAC Honda advocates children's road traffic safety in "dream children's travel"



