当前位置:网站首页>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 .
边栏推荐
- Keyword inline (inline function) usage analysis [C language]
- Cannot change version of project facet Dynamic Web Module to 2.3.
- open-mmlab labelImg mmdetection
- . elf . map . list . Hex file
- 基于Redis的分布式ID生成器
- [Red Treasure Book Notes simplified version] Chapter 12 BOM
- Working principle of genius telephone watch Z3
- A possible cause and solution of "stuck" main thread of RT thread
- ESP8266通过arduino IED连接巴法云(TCP创客云)
- Bubble sort [C language]
猜你喜欢
Générateur d'identification distribué basé sur redis
Mysql database interview questions
CUDA C programming authoritative guide Grossman Chapter 4 global memory
C语言回调函数【C语言】
MySQL时间、时区、自动填充0的问题
高通&MTK&麒麟 手机平台USB3.0方案对比
Kaggle competition two Sigma connect: rental listing inquiries
AMBA、AHB、APB、AXI的理解
I2C bus timing explanation
level16
随机推荐
Arduino JSON data information parsing
arduino获取数组的长度
JS variable types and common type conversions
JS变量类型以及常用类型转换
arduino获取随机数
2022.2.12 resumption
Time slice polling scheduling of RT thread threads
Mysqldump error1066 error solution
Pytorch four commonly used optimizer tests
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
锂电池基础知识
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
高通&MTK&麒麟 手机平台USB3.0方案对比
Unit test - unittest framework
Symbolic representation of functions in deep learning papers
CUDA C programming authoritative guide Grossman Chapter 4 global memory
(一)R语言入门指南——数据分析的第一步
Basic operations of databases and tables ----- classification of data