Difference between Oracle DELETE and TRUNCATE

Syntax
delete from aa
truncate table aa

Difference
1. You can write conditions after delete from, but not truncate.


2. The delete from record is deleted one by one, and each deleted row of records will be entered into the log, while truncate deletes the entire page at one time, so only the page release is recorded in the day to day. In short, delete from update log, truncate is basically No, less transaction log space is used.


3. After delete from the empty table, an empty page will be retained, and truncate will not leave any pages in the table.


4. When a DELETE statement is executed with row locks, the rows in the table are locked for deletion. truncate always locks tables and pages, not individual rows.

5. If there is an auto-incrementing id column generated by identity, it will still increase from the last number after delete from, that is, the seed will remain unchanged, and after truncate, the seed will be restored to the original.


6. truncate will not trigger the delete trigger, because the truncate operation does not record the deletion of each row.


Summary
1. truncate and delete only delete data but not delete 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 (constrain), trigger (trigger), index (index); Stored procedures that depend on the table The /function will remain, but become invalid.


2. The delete statement is dml, this operation will be placed in the rollback segement, and it will take effect after the transaction is committed; if there is a corresponding trigger, it will be triggered when executed
    truncate, drop is 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.


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. The   
   truncate statement releases the space to minextents extents by default, unless Use reuse storage; truncate will reset 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.


6. 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   
to keep the table and delete all the data. If it has nothing to do with the transaction, use truncate That 's it. If it is related to a transaction, or if you want to trigger a trigger, you can still use delete.
If it is to sort out the fragments inside the table, you can use truncate to keep up with reuse stroage, and then re-import/insert the data. Explain the difference between Oracle DELETE and TRUNCATE

Guess you like

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