0% found this document useful (0 votes)
119 views2 pages

Delete Vs Truncate

Truncate table removes all rows from a table faster than delete by deallocating the data pages rather than deleting rows individually. Truncate cannot be rolled back, resets the identity counter, and does not activate triggers, while delete can be rolled back, does not reset the identity, and does activate triggers. Truncate also cannot be used if the table has foreign key constraints or is part of an indexed view, while delete can be used in those cases.

Uploaded by

sandeep_nayak_17
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views2 pages

Delete Vs Truncate

Truncate table removes all rows from a table faster than delete by deallocating the data pages rather than deleting rows individually. Truncate cannot be rolled back, resets the identity counter, and does not activate triggers, while delete can be rolled back, does not reset the identity, and does activate triggers. Truncate also cannot be used if the table has foreign key constraints or is part of an indexed view, while delete can be used in those cases.

Uploaded by

sandeep_nayak_17
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Delete table is a logged operation, so the deletion of each row gets logged in the transaction log, which makes

it slow. Truncate table also deletes all the rows in a table, but it wont log the deletion of each row, instead it logs the de-allocation of the data pages of the table, which makes it faster. Of course, truncate table cannot be rolled back. Truncate table is functionally identical to delete statement with no where clause both remove all rows in the table. But truncate table is faster and uses fewer system and transaction log resources than delete. Truncate table removes all rows from a table, but the table structure and its columns, constraints, indexes etc., remains as it is. In truncate table the counter used by an identity column for new rows is reset to the seed for the column. If you want to retain the identity counter, use delete statement instead. If you want to remove table definition and its data, use the drop table statement. You cannot use truncate table on a table referenced by a foreign key constraint; instead, use delete statement without a where clause. Because truncate table is not logged, it cannot activate a trigger. Truncate table may not be used on tables participating in an indexed view.

What is difference between DELETE & TRUNCATE commands? Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command. TRUNCATE TRUNCATE is faster and uses fewer system and transaction log resources than DELETE. TRUNCATE removes the data by deallocating the data pages used to store the tables data, and only the page deallocations are recorded in the transaction log. TRUNCATE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column. You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint. Because TRUNCATE TABLE is not logged, it cannot activate a trigger. TRUNCATE cannot be Rolled back using logs. TRUNCATE is DDL Command. TRUNCATE Resets identity of the table. DELETE DELETE removes rows one at a time and records an entry in the transaction log for each deleted row.

If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement. DELETE Can be used with or without a WHERE clause DELETE Activates Triggers. DELETE Can be Rolled back using logs. DELETE is DML Command. DELETE does not reset identity of the table.

You might also like