The difference between truncate, delete and drop

Same point:

1. truncate and delete without where clause, and drop will delete the data in the table.

 

2. Both drop and truncate are DDL statements (Data Definition Language), which will be automatically submitted after execution.

 

difference:

1. truncate and delete only delete data without deleting the structure of the table (definition)
The drop statement will delete the constraints, triggers, and indexes on which the structure of the table is dependent; the stored procedures/functions that depend on the table will remain, but become invalid.

 

2. The delete statement is a database operation language (dml). This operation will be placed in the rollback segment and will take effect after the transaction is committed; if there is a corresponding trigger, it will be triggered during execution.
truncate and drop are database definition language (ddl), the operation takes effect immediately, the original data is not placed in the rollback segment, cannot be rolled back, and the operation does not trigger trigger.

 

3. The delete statement does not affect the extent occupied by the table, and the high watermark remains in its original position.
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 resets the high watermark (back to the beginning).

 

4. Speed, in general: drop > truncate > delete

 

5. Security: Use drop and truncate carefully, especially when there is no backup. Otherwise, it will be too late to cry
In use, if you want to delete some data rows, use delete, and pay attention to the where clause. The rollback segment should be large enough.
If you want to delete the table, of course, use drop
If you want to keep the table and delete all the data, if it has nothing to do with the transaction, just use truncate. If it is related to a transaction, or if you want to trigger a trigger, use delete.
If you are sorting out the fragments inside the table, you can use truncate to keep up with reuse stroage, and then re-import/insert data.


6.delete is a DML statement and will not be automatically submitted. Drop/truncate are all DDL statements, which will be automatically submitted after execution.

 

7. TRUNCATE TABLE is functionally the same as the DELETE statement without the WHERE clause: both delete all rows in the table. But TRUNCATE TABLE is faster than DELETE and uses 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.

 

8. TRUNCATE TABLE deletes all rows in the table, but the table structure and its columns, constraints, indexes, etc. remain unchanged. The count value used for new row identification is reset to the seed for this column. If you want to preserve the identity count value, use DELETE instead. If you want to drop the table definition and its data, use the DROP TABLE statement.  
    
9. For tables referenced by FOREIGN KEY constraints, TRUNCATE TABLE cannot be used, but a DELETE statement without a WHERE clause should be used. Since TRUNCATE TABLE is not logged, it cannot activate triggers.    
 

10. TRUNCATE TABLE cannot be used for tables that participate in indexed views.  



[b]Note: The above content comes from the Internet[/b]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326370675&siteId=291194637