The difference between drop, delete and truncate

Drop directly deletes the table truncate to delete the data in the table. When inserting again, the self-increasing id starts from 1 to delete the data in the table. You can add the where clause.

 

(1) The process of deleting a DELETE statement is to delete a row from the table at a time, and at the same time save the delete operation of the row as a transaction record in the log for rollback operation.

TRUNCATE TABLE deletes all data from the table at one time and does not record the individual delete operation records in the log for preservation. Deleted rows cannot be recovered.

And the delete trigger related to the table will not be activated during the delete process. Fast execution.

 

(2) The space occupied by tables and indexes. When the table is TRUNCATE, the space occupied by the table and index will be restored to the original size, and the DELETE operation will not reduce the space occupied by the table or index.

The drop statement releases all the space occupied by the table.

 

(3) In general, drop > truncate > delete

 

(4) Scope of application. TRUNCATE can only be used for TABLE; DELETE can be used for table and view

 

(5) TRUNCATE and DELETE only delete data, while DROP deletes the entire table (structure and data).

 

(6) truncate and delete without where: only delete data, but not delete the structure (definition) of the table, the drop statement will delete the constraints on which the structure of the table is dependent (constrain),

Trigger (trigger) index (index); Stored procedures/functions that depend on this table will be preserved, but their status will become: invalid.

 

(7) The delete statement is DML (data maintain language), this operation will be placed in the rollback segment, and it will take effect after the transaction is committed. If there is a corresponding tigger,

will be triggered when executed.

 

(8) truncate and drop are DLL (data define language), the operation takes effect immediately, the original data is not placed in the rollback segment and cannot be rolled back

 

(9) In the absence of backup, use drop and truncate with caution. To delete some data rows, use delete and pay attention to combining with where to constrain the scope of influence. The rollback segment should be large enough.

To delete the table, use drop; if you want to keep the table and delete the data in the table, if it has nothing to do with the transaction, you can use truncate. If it is related to affairs, or the teacher wants to trigger the trigger, use delete.

 

(10) Truncate table table name is fast and efficient because:

truncate table is functionally identical to the DELETE statement without the WHERE clause: both delete all rows in the table. But TRUNCATE TABLE is faster than DELETE,

And use less system and transaction log resources.

The DELETE statement deletes one row at a time and records an entry in the transaction log for each row deleted.

TRUNCATE TABLE deletes data by freeing the data pages used to store the table data, and only records the freeing of pages in the transaction log.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326606216&siteId=291194637