Compare Sql in truncate, delete and drop the

Same point:

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

 

  2.drop, truncate is DDL statements (Data Definition Language), it will automatically be submitted after execution.

 

difference:

  1. truncate data and delete only delete without deleting the table structure (as defined)
  drop table delete statement structure is dependent constraints (constrain), the trigger (trigger), an index (index); stored procedure relies on the table / function will remain, but become invalid state.

 

  2. delete statement is a database manipulation language (dml), this operation will put rollback segement, the only take effect after the transaction commits; if there is a corresponding trigger, when the execution will be triggered.
  truncate, drop a database definition language (ddl), operating with immediate effect, the original data is not put rollback segment can not be rolled back, the operator does not trigger the trigger.

 

  3.delete statements do not affect the table occupied by the extent, high waterline (high watermark) to maintain the original position does not move
  drop statements will release all the space occupied by the table.
  See truncate statement released to the default space minextents a extent, unless reuse storage; truncate will reset the high water line (back to the beginning).

 

  4. Speed, general: drop> truncate> delete

 

  5. Security: Be careful using the drop and truncate, in particular when there is no backup or too late to cry.
  Use on the part of the data you want to delete the row with delete, to bring attention to the where clause rollback segments should be large enough..
  Want to delete the table, Of course, with a drop
  you want to keep all the data tables will be deleted, and if a transaction has nothing to do with truncate to. If and matters relating to, or want to trigger the trigger, or a delete.
  If it is inside finishing table fragmentation, you can truncate keep reuse stroage, reintroduced into / insert the data.


  6.delete DML statement is not automatically submitted. drop / truncate is DDL statements, it will automatically be submitted after execution.

 

  7, TRUNCATE TABLE and DELETE statement with no WHERE clause is functionally the same: both remove all rows in the table. But TRUNCATE TABLE is faster than DELETE speed, and fewer system and transaction log resources used. DELETE statement deletes each row, and each row is a record deleted in the transaction log. TRUNCATE TABLE to delete data stored by the release of the data page table data used, and only release page recorded in the transaction log. 

 

  8, TRUNCATE TABLE to delete all the rows in the table, but the table structure and its columns, constraints, indexes and so on remain unchanged. New line count value reseeded identification used for that column. If you want to keep the identity count value, use DELETE. If you want to remove table definition and its data, use the DROP TABLE statement.  
    
  9, for a table referenced by FOREIGN KEY constraint can not be used TRUNCATE TABLE, but should not use the DELETE statement WHERE clause. Because TRUNCATE TABLE is not logged, it can not activate the trigger.    
 

  10, TRUNCATE TABLE not for participating in a table indexed view

 

Drop

drop can be used to delete a database (drop Database database name), delete the data table (use database name drop table data of Table 1 names, the data in Table 2), or delete data table field (use database name alter table table name drop column field names ( column name)).

Notably: drop statement removes the structure of the table is dependent constraints (constrain), the trigger (Trigger), an index (index); dependent table stored in the procedure / function will be retained, but becomes invalid state.

 

Truncate

Truncate data for deleting the data in the table, only the data in the table data, without deleting the table (truncate table table name).

 

Delete

Delete: delete the data rows in the table, may delete a row, all rows can be deleted without deleting data table (delete a row: Delete from table names where column name = value).

 

Difference between the three

 

To distinguish from the delete speed:

In terms of deleting data rate, generally from fast to slow: drop> truncate> delete

 

Distinguished from the language type:

delete statement is a database manipulation language (dml), ​​this operation will put rollback segement, the only take effect after the transaction commits; if there is a corresponding trigger, when the execution will be triggered.

truncate, drop a database definition language (ddl), operating with immediate effect, the original data is not put rollback segment can not be rolled back, the operator does not trigger the trigger.

(Core SQL has four parts: data definition language DDL, data manipulation language DML, Data Control Language DCL, embedded SQL language)

 

Remove content from:

truncate and delete only delete data without deleting the table structure (defined)

drop delete content and definition, and free up space. Execute the drop statement, will allow the structure of this table is deleted together.

 

truncate和delete:

Process delete delete statement is deleted from the table each row, and at the same row as the delete transaction records stored in a log, for rollback operation.

TRUNCATE TABLE to remove a data page is stored by releasing the data table data and records only the page released in the transaction log, the data can not be rolled back.

After truncate statement is executed, id identity column or arranged in order to maintain continuous;

After the delete statement is executed, ID identity column discontinuous

 

 

To learn more:

alter table change table name of the new primary column Column name Type; - changes to the table column Name

alter table modify table column name Type; - class type changes to the table

alter table table name drop column names; - delete a column in a table

alter table add column name table type; - Add a column

alter table table name rename the new table name; - modify the table name
---------------------

Guess you like

Origin www.cnblogs.com/klb561/p/10963473.html