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
Arrange by functionNN : Indicates which earlier ve...
Preface I have always wanted to know how a SQL st...
In Docker Start all container commands docker sta...
There is a medicine for regret in the world, as l...
Please see the following screenshot I took from G...
Querying the database SELECT * FROM `student` Que...
Multi-table query Use a single select statement t...
Solution to mysql not closing: Right-click on the...
Table of contents Typical Cases Appendix: Common ...
Preface: During the project development, we encou...
After installing the latest Windows 10 update, I ...
Table of contents Technology Stack Backend build ...
This article shares the specific code of vue elem...
Table of contents 1. Introduction 2. What is func...