当前位置:网站首页>Detailed explanation of truncate usage

Detailed explanation of truncate usage

2022-07-06 12:18:00 wx5caecf2ed0645


Preface : 


When we want to empty a table , Often use truncate sentence . Most of the time we just care about meeting the needs , Instead of thinking about the use of such statements and considerations . This article mainly introduces truncate How to use sentences and what to pay attention to .


1.truncate Use the syntax


truncate It is used to empty or truncate a table , It only works on the table .truncate The grammar of is very simple , Follow the table name directly , for example :​​truncate table tbl_name​

  perhaps  ​​truncate tbl_name​

 .



perform truncate Statement needs to have a table of drop jurisdiction , Logically speaking ,truncate table Be similar to delete Delete all line statements or drop table And then again create table The combination of sentences . For high performance , It bypasses the deletion of data DML Method , therefore , It cannot be rolled back . Even though truncate table And delete be similar , But it's classified as DDL Statement instead of DML sentence .


2.truncate And drop,delete Comparison of


It said truncate And delete,drop Very similar , In fact, these three are quite different from each other , The following is a simple comparison of the similarities and differences of the three .



  • truncate And drop yes DDL sentence , Cannot roll back after execution ;delete yes DML sentence , Roll back .
  • truncate It only works on the table ;delete,drop It can act on the table 、 View etc. .
  • truncate All rows in the table will be cleared , But table structure and its constraints 、 Index and so on remain unchanged ;drop It will delete the structure of the table and the constraints it depends on 、 Index, etc. .
  • truncate Will reset the table's self increment ;delete Can't .
  • truncate Table related drop triggers are not activated ;delete Sure .
  • truncate After that, the space occupied by the table and index will be restored to the original size ;delete The operation does not reduce the space occupied by the table or index ,drop Statement to free up all the space occupied by the table .


3.truncate Usage scenarios and precautions


Through the introduction above , It's easy for us to come to the conclusion that truncate Usage scenarios of statements , That is, when the data in the table is not needed at all truncate. If you want to delete some data, use delete, Pay attention to take where Clause ; If you want to delete a table , Of course with drop; If you want to keep the table and delete all data regardless of the transaction , use truncate that will do ; If it's about business , Or want to trigger trigger, Or use it delete; If it's defragmenting the table , It can be used truncate And then re insert the data .


No matter what ,truncate Watch is a high-risk operation , Especially in the production environment, be more careful , Here are a few notes , I hope you can use it for reference .



  • truncate Unable to get binlog Roll back .
  • truncate All data will be cleared and the execution speed will be very fast .
  • truncate Cannot use... On a table that has a foreign key constraint reference .
  • perform truncate need drop jurisdiction , It is not recommended to give an account number drop jurisdiction .
  • perform truncate Be sure to check again and again before , It's better to back up the data in the following table in advance .


原网站

版权声明
本文为[wx5caecf2ed0645]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131547076947.html