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

37445102526597cac24657d2c2928030.png

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 droptruncatedelete
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 :935f7de4d0fb512572dda7919864e9d8.png

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 :5173583009b808f58261c53287b78040.png Let's restore the table to its original state , Reuse truncate Delete , The execution result is shown in the figure below :cc3052306c7b7d8d9d63e67c169671ec.png Restore the table to its original state , perform drop Delete statements , The execution result is shown in the figure below :8d7d90ada2a54067c5583321704ca5a3.png 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 :5e353dd5ae4056f52970a8b9f17580e8.png

3. Command categories are different

truncate、delete and drop Belongs to SQL Different categories ,SQL Divided into the following 3 class :

  1. DDL【Data Definition Language】 Data definition language , The structure used to maintain stored data represents instructions : create、drop、alter、truncate.

  2. 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.

  3. 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 :1fbcf67f8930d52c4d71cc4774843697.png 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 :e6ac3cc99ac06f4017d4943b8507a1d1.png Now let's demonstrate delete Rollback operation for , As shown in the figure below :5372ab71f65d2951e29374227ab4b3a6.png 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 :ec04713f7849e865a15703f565e6cfe4.png and truncate Will reset the initial value of the auto increment field , As shown in the figure below :5919f2beac752535b81c066c52230fad.png

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 :

  1. Execution speed :drop > truncate > detele.

  2. delete and truncate Delete only table data , and drop The table data, table structure and table index will be deleted 、 Constraints and triggers .

  3. 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 .

  4. 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 .

  5. truncate Will reset the auto increment column to 1, and delete Autoincrement columns are not reset .

  6. 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

df90de5735fa868d330a10dcdfeb59bb.gif

 Previous recommendation 

c885610e87c19002a1360a616cf0e2c5.png

Java Weekly summary of interview questions ( common 57 piece )| The latest version


539817738a29eff9b4835b434ae02d26.png

Interview shock 57: Cluster index = Primary key index ?


2e24b45f5d2b15031ec77796315459f8.png

Interview shock 56: What is the difference between clustered index and non clustered index ?


e73074ccbfa41461760e505e4dc8471b.png

Interview shock 55:delete、drop、truncate What's the difference? ?


6a79e931de7048302683a2ea826248a2.gif

原网站

版权声明
本文为[JAVA Chinese community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221542205020.html