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
# Installation daemon configuration for Redis on ...
Recently I saw the article Build your own React o...
This article shares the specific code of js to ac...
8 optimization methods for MySQL database design,...
Table of contents Overview 1. Separation of front...
Jenkins is an open source software project. It is...
In the previous article, we introduced how to use...
1. Float: The main purpose is to achieve the effe...
1) Introduction to cache mechanism In the Linux s...
React Hooks is a new feature introduced in React ...
Table of contents 1. Introduction 2. Installation...
Table of contents Common payment methods in proje...
Preface In front-end programming, we often use th...
Set the table's style="table-layout:fixed...
Table of contents Question: 1. First attempt 2. R...