Preface:When we want to clear a table, we often use the truncate statement. Most of the time we only care about whether the needs can be met, without thinking about the usage scenarios and precautions of such statements. This article mainly introduces the usage and precautions of the truncate statement. 1.Truncate usage syntax The function of truncate is to clear the table or truncate the table, and it can only be used on the table. The syntax of truncate is very simple, just follow it with the table name, for example: Executing the truncate statement requires the drop permission for the table. Logically, truncate table is similar to a delete statement that deletes all rows or a combination of a drop table and then a create table statement. To achieve high performance, it bypasses the DML method of deleting data, so it cannot be rolled back. Although truncate table is similar to delete, it is classified as a DDL statement rather than a DML statement. 2. Comparison between truncate, drop, and deleteAs mentioned above, truncate is very similar to delete and drop. In fact, there are still significant differences between these three. The following is a brief comparison of the similarities and differences between the three.
3. Truncate usage scenarios and precautionsThrough the previous introduction, we can easily derive the usage scenario of the truncate statement, that is, truncate can be used when the table data is completely unnecessary. If you want to delete some data, use delete, and remember to include a where clause; if you want to delete a table, of course use drop; if you want to keep the table and delete all data and it has nothing to do with transactions, use truncate; if it is related to transactions, or you want to trigger a trigger, still use delete; if you want to sort out the fragments inside the table, you can use truncate and then reinsert the data. In any case, truncate table is a high-risk operation, especially in production environment, you need to be more careful. Here are a few points to note, I hope you can use them as a reference when using.
The above is a detailed explanation of the usage of Truncate in MySQL introduced by the editor. I hope it will be helpful to everyone. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Detailed explanation of Web front-end performance optimization: resource merging and compression
>>: Solution to the conflict between two tabs navigation in HTML
I wonder if you have ever thought about this ques...
Preface Since MySQL 5.1.6, a very unique feature ...
Table of contents The basic principles of Vue'...
MySQL transaction support is not bound to the MyS...
This article uses examples to illustrate the erro...
Project Documentation Directory Div+CSS Naming Sta...
Table of contents Preface 1. Introduction to Axio...
How to install PHP7 on Linux? 1. Install dependen...
Table of contents topic analyze Objects of use So...
Preface In order to reflect the difference betwee...
Preface The simple understanding of MySQL permiss...
Since enabling https access for the entire site, ...
Find the problem I recently encountered a problem...
This article mainly introduces the example analys...
Table of contents Preface 1. What is selenium? 2....