Compare truncate and delete

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 it 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 releasing data pages used to store table data, and only records page releases in the transaction log.

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.

For tables referenced by FOREIGN KEY constraints, TRUNCATE TABLE cannot be used, but DELETE statements without WHERE clauses should be used. Since TRUNCATE TABLE is not recorded in the log, it cannot activate triggers.

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

Guess you like

Origin blog.csdn.net/guorui_java/article/details/109647185