The difference between drop, truncate and delete

The difference between drop, truncate and delete

Original November 22, 2015 19:30:21

The difference between drop , truncate and delete

(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 indexes will be restored to the original size,

   DELETE operations do not reduce the space occupied by tables or indexes.

   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, DROP deletes the entire table (structure and data). 

( 6 ) truncate and delete  without where : only delete the data, not the structure of the table (definition) The drop statement will delete the constraint ( constrain), trigger ( trigger) index ( index); dependent on the structure of the table Stored procedures / functions on this table will be preserved, but their status will change to: 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, it 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 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 release of pages in the transaction log. 

( 11 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. 

( 12 ) 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.

One, delete

1. delete is DML . When the delete operation is performed , a row is deleted from the table each time, and the delete operation of the row is recorded in the redo and undo tablespaces for rollback and redo operations. Note that the table space must be large enough to require manual commit ( commit ) operation to take effect, and the operation can be undone through rollback .

2. delete can delete the data that meets the conditions in the table according to the conditions. If the where clause is not specified, then delete all the records in the table.

3. The delete statement does not affect the extent occupied by the table, and the high watermark remains unchanged.

2. truncate

1. truncate is DDL and will be implicitly submitted, so it cannot be rolled back and triggers will not be triggered.

2. truncate will delete all records in the table, and reset the high watermark and all indexes. By default, the space will be released to minextents extents , unless reuse storage is used . No log is recorded, so the execution speed is very fast, but the operation cannot be undone through rollback (if a table is accidentally truncated , it can be recovered, but it cannot be recovered through rollback ).

3. For tables referenced by  foreign key constraints  , truncate table cannot be used, but a delete  statement without  where  clause  should be used.

4. truncatetable cannot be used for tables that participate in indexed views.

3. drop

1. Drop is DDL and will be implicitly submitted, so it cannot be rolled back and triggers will not be triggered.

2. The drop statement deletes the table structure and all data, and releases all the space occupied by the table.

3. The drop statement will delete the constraints, triggers, and indexes that the structure of the table depends on, and the stored procedures / functions that depend on the table will remain , but become invalid .

 

Summarize:

1. In terms of speed, in general, drop > truncate > delete .

2. Be careful when using drop and truncate . Although it can be restored, you should be cautious in order to reduce trouble.

3. If you want to delete some data, use delete , pay attention to bring the where clause, and 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.

Guess you like

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