Simply understand the differences in the principles of common SQL delete statements

Simply understand the differences in the principles of common SQL delete statements

This article mainly introduces the differences between the SQL delete statements DROP, TRUNCATE, and DELETE to help you better understand and learn SQL statements. Interested friends can learn more about it.

DROP:

DROP TABLE test;

Delete the test table and free up space, deleting all data in the test table.

TRUNCATE:

TRUNCATE test;

Delete the contents of the test table and free up space, but do not delete the table definition. The table structure remains.

DELETE:

1. Delete specified data

Delete the data whose age is 30 and whose country is US in the test table

DELETE FROM test WHERE age=30 AND country='US';

2. Delete the entire table

Delete all contents of the test table, keep the table definition, and do not release space.

DELETE FROM test or TRUNCATE test;

The truncate table command will quickly delete all records in a table but retain the table structure. This quick deletion is different from the delete from table command which deletes all table records. The data deleted by the delete command will be stored in the system rollback segment. When necessary, the data can be rolled back and restored, while the data deleted by the truncate command cannot be restored.

Similarities

Truncate, delete without a where clause, and drop will delete data in the table.

Differences:

1. Truncate and delete only delete data but not the table structure (definition). The drop statement will delete the constraints, triggers, and indexes that the table structure depends on; the stored procedures/functions that depend on the table will be retained but become invalid.

2. The delete statement is a DML statement. This operation will be placed in the rollback segment and will take effect only after the transaction is committed. If there is a corresponding trigger, it will be triggered when it is executed. Truncate and drop are DDL operations. The operation takes effect immediately. The original data is not placed in the rollback segment and cannot be rolled back. The operation does not trigger the trigger.

3. The delete statement does not affect the extent occupied by the table, and the high watermark remains in its original position. Obviously, the drop statement releases all the space occupied by the table. By default, the truncate statement releases space to minextents extents, unless reuse storage is used; truncate will reset the high watermark (back to the beginning).

4. Speed: Generally speaking: drop > truncate > delete.

5. Safety: Use drop and truncate with caution, especially when there is no backup. Otherwise it will be too late to cry.
at last

In usage, if you want to delete some data rows, use delete, and be sure to include the where clause. The rollback segment must be large enough.

If you want to delete a table, of course use drop.

You want to keep the table but delete all the data. If it is not related to the transaction, just use truncate. If it is related to a transaction, or if you want to trigger a trigger, use delete.

If you need to defragment the table, you can use truncate followed by reuse storage, and then re-import/insert the data.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Mysql some complex sql statements (query and delete duplicate rows)
  • Use SQL batch replace statements to modify, add, and delete field contents
  • SQL statement to delete duplicate records and keep only one
  • Must-know SQL statements (IV) Data deletion and update
  • Must-know SQL statements (Part 2) Create table, modify table structure, delete table
  • Example code for creating and deleting constraints using SQL statements
  • Oracle uses SQL statements to add fields (SQL delete field statements)
  • SQL statement to batch delete the specified prefix table in MySQL
  • How to delete and add foreign keys and primary keys using SQL statements
  • Comparison of MySQL Delete and Truncate statements

<<:  Vue3 AST parser-source code analysis

>>:  Solution to slow response of Tomcat server

Recommend

MySQL backup and recovery design ideas

background First, let me explain the background. ...

Detailed explanation of Vue's sync modifier

Table of contents 1. Instructions 2. Modifiers 3....

JS implements sliding up and down on the mobile terminal one screen at a time

This article shares with you the specific code of...

How to migrate local mysql to server database

We can use the scp command of Linux (scp cannot b...

A simple way to implement all functions of shopping cart in Vue

The main functions are as follows: Add product in...

Specific use of the wx.getUserProfile interface in the applet

Recently, WeChat Mini Program has proposed adjust...

Double loading issue when the page contains img src

<br />When the page contains <img src=&qu...

Example of how to implement MySQL cascading replication

The so-called cascading replication is that the m...

The table merges cells and the img image to fill the entire td HTML

Source code (some classes deleted): Copy code The ...

Sample code for implementing two-way authentication with Nginx+SSL

First create a directory cd /etc/nginx mkdir ssl ...

MySQL slow query and query reconstruction method record

Preface What is a slow query and how to optimize ...