Introduction In this article let us try and understand the difference between "Delete" and "Truncate" statement in Microsoft SQL Server.
Differences ...
• 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 won’t 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.
Summary Hope this gives some clarity on this subject.
|
| Author: Thamil Selvan J. 17 Jan 2007 | Member Level: Silver Points : 0 |
Good article. Its very useful to all.
|
| Author: David Portas 09 Mar 2008 | Member Level: Bronze Points : 0 |
Hi, You a wrong about TRUNCATE when you say it cannot be rolled back. TRUNCATE can be rolled back just like other DML operations.
|
| Author: Nishanth Nair 15 Apr 2008 | Member Level: Silver Points : 0 |
I agree with David. Truncate can be rolled back . Please try to execute the following statement and see.
-- Create atable called testtable b4 running this script. and add some --row data too :) begin transaction T1
truncate table testtable
if @@error = 0 Rollback transaction T1
else commit transaction T1
|
| Author: ashokkuamr 01 Sep 2008 | Member Level: Bronze Points : 2 |
Hi Truncate and Delete are used to delete the records. My Concern is which one is Rollbacked..?? Both the statements can be rollbacked ...?? Yes ! both the statements can be rollbacked..
Run a below script if u have any concern on this..
Example1: ======== create table a ( a int ) insert into a select 4 -- Inserted one record
BEGIN TRANSACTION; delete from a rollback TRANSACTION
Example2: ======== create table a ( a int ) insert into a select 4 -- Inserted one record
BEGIN TRANSACTION; Truncate table a rollback TRANSACTION
Both works fine in SQL Server 2005...
Any clarification pl let me know in this ....
|
| Author: sangeetha 12 May 2009 | Member Level: Gold Points : 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 won't log the deletion of each row, instead it logs the deallocation of the data pages of the table, which makes it faster. Of course, TRUNCATE TABLE can be rolled back.
|