当前位置:网站首页>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 .
边栏推荐
- [esp32 learning-2] esp32 address mapping
- OSPF message details - LSA overview
- Basic operations of databases and tables ----- creating data tables
- JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
- Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
- Mysql database interview questions
- Embedded startup process
- 高通&MTK&麒麟 手机平台USB3.0方案对比
- [golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
- 1081 rational sum (20 points) points add up to total points
猜你喜欢
Symbolic representation of functions in deep learning papers
Amba, ahb, APB, Axi Understanding
Basic operations of databases and tables ----- classification of data
Basic knowledge of lithium battery
Detailed explanation of Union [C language]
基于Redis的分布式锁 以及 超详细的改进思路
Characteristics, task status and startup of UCOS III
ES6 grammar summary -- Part 2 (advanced part es6~es11)
【ESP32学习-1】Arduino ESP32开发环境搭建
E-commerce data analysis -- salary prediction (linear regression)
随机推荐
inline详细讲解【C语言】
imgcat使用心得
[esp32 learning-2] esp32 address mapping
MySQL時間、時區、自動填充0的問題
Bubble sort [C language]
I2C总线时序详解
冒泡排序【C语言】
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
选择法排序与冒泡法排序【C语言】
Kconfig Kbuild
Générateur d'identification distribué basé sur redis
ES6 grammar summary -- Part 2 (advanced part es6~es11)
Characteristics, task status and startup of UCOS III
基于Redis的分布式锁 以及 超详细的改进思路
Cannot change version of project facet Dynamic Web Module to 2.3.
【ESP32学习-1】Arduino ESP32开发环境搭建
Togglebutton realizes the effect of switching lights
Raspberry pie tap switch button to use
Understanding of AMBA, AHB, APB and Axi
关于Gateway中使用@Controller的问题