Delete VS Truncate


This article explains the difference between "Delete" and "Truncate" statements in Microsoft SQL Server.

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.


Comments

Author: Prabhukumar J17 Jan 2007 Member Level: Silver   Points : 0

Good article. Its very useful to all.

Author: David09 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 Nair15 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: ashokkuamr01 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: sangeetha12 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.

Author: Vadivel Mohanakrishnan06 Aug 2011 Member Level: Gold   Points : 1

True Truncate is also a logged operation, but in a different way. It logs the deallocation of the data pages in which the data exists.

I forgot to update the article here whereas in 2006 itself I wrote a blog post on that in my blog. Seems hyperlinks can't be posted here :)

Author: Ultimaterengan11 Aug 2011 Member Level: Gold   Points : 1

Hi Editors,
This is copied resource

The original resource is below link


http://www.dotnetspark.com/qa/3052-database-what-is-recorded-as-transaction.aspx

http://dba.fyicenter.com/Interview-Questions/SQL-Server-DBA/What_s_the_difference_between_DELETE_TABLE_and_T_54.html

Author: Jivani13 Sep 2013 Member Level: Gold   Points : 4

Truncate

1)It is DDL(Data Definition Language) Statement.

2)It Delete all rows from table.

3)You can not Rollback all raw.

4)It release all raw including Memory space, Just Structure remains there.

5)Example : Truncate Table Emp

Delete

1)It is DML(Data Manipulation Language) Statement.

2)You Can Delete All rows as well as specified rows using Delete.

3)Using Rollback you can retrieve all rows from the point of last commit.

4)It delete all raw but, not Memory space.

5)Example : Delete From Emp Where Empno=7369

Author: anbu18 Nov 2013 Member Level: Gold   Points : 0

very useful and understanding article

Many Thanks

Author: Umesh Bhosale21 Apr 2014 Member Level: Silver   Points : 0

very nice article

Thanks
Umesh



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: