MySQL statement: usage of drop, truncate, delete

table of Contents

1 drop, truncate, delete syntax

2 difference between drop, truncate, delete

2.1 Similarities

2.2 Differences

2.3 In actual application

3 Remarks: DML and DDL

3.1 The meaning of DML and DDL:

3.2 The difference between DML and DDL:

1 drop, truncate, delete syntax

(1) drop table table name;                                    eg: drop table dbo.Sys_Test
(2) truncate table table name;                              eg: truncate table dbo.Sys_Test                  
(3) delete from table name;         
         delete from table name where column name=value;       eg: delete from dbo.Sys_Test where test='test'

2 difference between drop, truncate, delete

2.1 Similarities

Drop, truncate, and delete without a where clause will clear the data in the table.

[Note: The delete mentioned here refers to the delete statement without the where clause.

[Delete with a where clause can delete each row of records one by one according to the filter conditions, and at the same time, the deletion of the row is stored as a transaction record in the log for redo or rollback operations.

2.2 Differences

(1) truncate and delete, only delete all data in the table, do not delete the definition of the table (that is, do not delete the structure of the table). Drop, not only deletes all the data in the table, but also deletes the definition of the table, the index of the table, etc., and all the space occupied by the table is released.

(2) Delete is a DML operation, this operation will be placed in the transaction, only after the transaction is submitted to take effect, support the rollback of the transaction. Truncate and drop are DDL operations, the operation will take effect immediately, the operation is not put in the transaction, and cannot be rolled back.

(3) Execution speed: Generally speaking: drop> truncate> delete.

2.3 In actual application

In practical applications, the difference between the three is clear: the table is discarded, and when you no longer need the table, use drop . When you still want to keep the table, but want to delete all records in the table, use truncate . When you want to delete some records, use delete with the where clause.

3 Remarks: DML and DDL

3.1 The meaning of DML and DDL:

1. DML (Data Manipulation Language): Data manipulation language. For the basic operations of the database, operations such as data processing in SQL are collectively referred to as data manipulation language. In short, it has realized the basic "addition, deletion, modification, and check" operation. The included keywords are: select, update, delete, insert, etc.

2. DDL (Data Definition Language): Data Definition Language, a language used to define and manage all objects in the SQL database, and manage certain objects in the database (such as database, table). The included keywords are: create, alter, drop, truncate, etc.

3.2 The difference between DML and DDL:

DML operations can manually control the opening, commit and rollback of transactions. DDL operations are submitted implicitly and cannot be rolled back.

 

Guess you like

Origin blog.csdn.net/cmm0401/article/details/109862598