The difference between drop truncate delete

1. drop (delete table): delete content and definitions to free up space. Simply put, remove the entire table. It is impossible to add data in the future, unless a table is added.

The drop statement will delete the constraint and trigger index on which the structure of the table is dependent; the stored procedure/function that depends on the table will be retained, but its status will become invalid.

2. Truncate (empty the data in the table): delete content, release space but not delete the definition (retain the data structure of the table). The difference with drop is that it just clears the table data.

Note: truncate cannot delete row data. To delete, empty the table.

3. Delete (delete data in the table): The delete statement is used to delete rows in the table. The delete statement deletes a row from the table each time, and at the same time the delete operation of the row is recorded in the log as a transaction

In order to perform a rollback operation.

Truncate and delete without where: only delete data, not the structure of the table (definition)

4. 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 the new row identification is reset to the seed of the column. If you want to keep the mark count value, use delete instead.

If you want to delete the table definition and its data, use the drop table statement.
5. For the table referenced by the foreign key constraint, truncate table cannot be used, but the delete statement without the where clause should be used. Since truncate table is recorded in the log, it cannot activate triggers.

6. Execution speed, generally speaking: drop> truncate> delete.

7. The delete statement is a database operating 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 when it is executed.
Special note: truncate and drop are database definition languages ​​(ddl). 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.

Guess you like

Origin blog.csdn.net/qq_36073688/article/details/112393968