CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Framework Design, Agile Coach, President Oklahoma City Developers Group, Microsoft MVP C#, TDD, Continuous Integration, Patterns and Practices, Domain Driven Design, Speaker, VB.Net, C# and Sql Server

The difference in TRUNCATE and DELETE in Sql Server

I’ve answered this question many times, and answered it again this weekend.  What is the difference when doing a DELETE TableA instead of TRUNCATE TableA?  A common misconception is that they do the same thing.  Not so.  In fact, there are many differences between the two.

DELETE is a logged operation on a per row basis.  This means that the deletion of each row gets logged and physically deleted.

You can DELETE any row that will not violate a constraint, while leaving the foreign key or any other contraint in place.

TRUNCATE is also a logged operation, but in a different way.  TRUNCATE logs the deallocation of the data pages in which the data exists.  The deallocation of data pages means that your data rows still actually exist in the data pages, but the extents have been marked as empty for reuse.  This is what makes TRUNCATE a faster operation to perform over DELETE.

You cannot TRUNCATE a table that has any foreign key constraints.  You will have to remove the contraints, TRUNCATE the table, and reapply the contraints.

TRUNCATE will reset any identity columns to the default seed value.  This means if you have a table with an identity column and you have 264 rows with a seed value of 1, your last record will have the value 264 (assuming you started with value 1) in its identity columns.  After TRUNCATEing your table, when you insert a new record into the empty table, the identity column will have a value of 1.  DELETE will not do this.  In the same scenario, if you DELETEd your rows, when inserting a new row into the empty table, the identity column will have a value of 265.



Comments

Sahil Malik said:

Simple concept + profound implications = good post.
# May 9, 2005 7:34 AM

Brendan Tompkins said:

Add this to the pile of stuff I didn't know before I met you. Thanks!
# May 10, 2005 6:20 AM

Jeffrey Vanneste said:

# May 10, 2005 4:48 PM

Thomas said:

nice explanation, I was asked this in an interview and couldn’t really explain the difference. thanx….
# May 30, 2005 12:09 AM

Greg Low said:

And the other big difference is: permissions.

The fact that a user can DELETE rows does not mean they can TRUNCATE the table. By default, they need at least DDL_ADMIN to do that.
# May 30, 2005 3:18 AM

Armando Prato said:


One other difference to note... delete triggers are not fired when using TRUNCATE.

Excellent post, btw
# July 8, 2005 7:04 AM

Alan said:

excellent post !!!
# August 19, 2005 2:04 PM

Rajiv Bhati said:

hi I am Rajiv Bhati, i am a S/w Test professional(QA). the biggest problem that i face while performing testing,is this one i.e basic difference between delete table and truncate table. from your material i got some more information.
thanks & regards
Rajiv Bhati rajivbhati12@gmail.com
# March 22, 2006 5:41 AM

shishir said:

Gud one!
will help me for my interview :-)
# May 30, 2006 10:18 AM

hemant said:

Thanks for a great info. It was asked two time in interviw and iwas having no answer
thank for clearing the concept
# June 1, 2006 1:24 AM

Srikanth HN said:

Thanks !! It helped ..
Short but still easy to understand !
# June 17, 2006 4:20 AM

prasadrmarathe said:

Thanks,
But what abt RollBack
Is RollBack Possible in both case Delete And Truncate in Sql Server2000
# June 29, 2006 4:17 AM

Shakeeb Ahmed said:

simple and nice explaination!!
# July 27, 2006 6:36 PM

raza ur rahman said:

verry good
# July 31, 2006 2:02 AM

raza ur rahman said:

nice explaination
# July 31, 2006 2:04 AM

Nitin Jagga said:

Q: Is RollBack Possible in both case Delete And Truncate in Sql Server2000 ?

Sol: Yes, in case of delete you can roll back, but in case of truncate, there's a scenario where rollback is possible. Normally not, you can take truncate as delete + commit as truncate is a DDL Command.

Scenario:

BEGIN TRAN

//DO

TRUNCATE

IF ERROR (OR ANY CONDITION)
ROLLBACK

COMMIT TRAN

in this case, rollback will work even after truncate if it meets your condition.

Other wise, truncate as i mentioned is a DDL Command and will work with auto commit status set as true.

Regards
# August 8, 2006 7:57 AM

san said:

Its possible to use where condition in truncate table command

# September 5, 2006 6:25 AM

Mohammad Iqbal said:

Thanks for a nice answer !!

# September 6, 2006 3:07 AM

Ramesh Galla said:

Thanks...!

# September 28, 2006 7:18 AM

shaik afreen said:

Thanks for this information

which maks me to understand what exactly happens internally.

# October 11, 2006 2:09 AM

Arun Mahendrakar said:

Thanks for the info.. very simple words, very concise statements, very lucid explanation

# October 17, 2006 3:18 PM

samir said:

while inserting data in a table through strored proc and the size of the field is

less than  any valued  ,how we avoid this situation without changing the size of the field

plz mail to

thanks..

# November 6, 2006 10:05 PM

samir said:

while inserting data in a table through strored proc and the size of the field is

less than  any valued  ,how we avoid this situation without changing the size of the field

plz mail to

thanks..

# November 6, 2006 10:14 PM

samir said:

what is the use of temporary table ?how we use it?

how to pass dynamic parametyers in stored proc?

while inserting data in a table through strored proc and the size of the field is

less than  any valued  ,how we avoid this situation without changing the size of the field  

plz mail to

thanks..

# November 6, 2006 10:30 PM

samir said:

sorry,

i am unable to retive my answers.

# November 6, 2006 10:39 PM

samir said:

sorry,

i am unable to get my answers

# November 6, 2006 10:42 PM

samir said:

plz.

  inform me in my id

thanks....

# November 6, 2006 10:45 PM

Raj said:

Excellent

# December 12, 2006 4:49 AM

Maqsud said:

Hi,

very simple words, excellent post.

# January 11, 2007 2:30 AM

sandeep bhardwaj said:

Thanks for clearing the concept, before geting this answer i thought that we can not roll back in any how.

thanks again.

# February 2, 2007 2:20 AM

sandeep bhardwaj said:

Thanks for clearing the concept, before geting this answer i thought that in truncate we can not roll back in any how.

thanks again.

# February 2, 2007 2:21 AM

Sunil said:

Hi it is really very good artical which is showing the difference between truncate and delete

# February 7, 2007 3:04 AM

Zubair Masoodi said:

This Post made me to feel better to answer any one who will ask me the difference

# February 15, 2007 7:45 AM

AK said:

Great Post

Helped me a lot

# April 11, 2007 4:28 AM

Risham said:

What about the table space concept, will it be freed in both the cases, delete n truncate???

# June 1, 2007 6:29 AM

karl said:

Somewhat off topic, but...in a MySQL NDBCluster (in memory table-type), a DELETE won't free up the memory used by the table - it'll only make it available for subsequent inserts into it. That is, until the data nodes are re-started.

A truncate will release memory used by a table without the need to restart a data node.

# June 1, 2007 7:52 AM

sukhveer said:

great job..

# June 18, 2007 12:15 PM

sateesh said:

Excellent

# June 28, 2007 7:13 AM

Suraj said:

nice description

# July 7, 2007 7:32 AM

kate said:

thanks a lot easy to understand.

# August 2, 2007 11:55 PM

SRIKANTH said:

Very nice and cool explanation.

# August 15, 2007 6:03 AM

sathya said:

The concepts is good.  The other difference is Truncate will be available for the whole table.  Delete can be done by row level.

# August 30, 2007 5:04 AM

Ruchi Kandwal said:

it is good to understand.

# December 20, 2007 8:27 AM

Hardik said:

This is very nice explanation ,,,

# January 8, 2008 3:57 AM

jimmy said:

shows u do know what the shit is going on actually inside.... good one. thanks.
# February 20, 2008 2:07 AM

Biaoge said:

It seems that delete( usually follows a where clause) does not release table size even you delete a whole table.

# March 19, 2008 10:51 PM

vikram said:

thanks for the post ,can we delete a table that has constraints??

# March 24, 2008 10:52 PM

vikram said:

Hi, thanks for this article , its very easy to understand, i have question, if  we use truncate the data will still present in the pages but they marked as empty for reuse,so what about the space,it wont release any space right?

# March 25, 2008 3:28 PM

Rajesh said:

Given insight to the concepts. simply nice....

# May 20, 2008 2:56 AM

Brijesh Nishad said:

Wat a gud explation u have...!!!

It's amazing!!!

really help full for us...

# June 10, 2008 11:54 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Raymond Lewallen

Working primarily in the public sector during his career, Raymond has designed and built several high profile enterprise level applications for all levels of the government. Raymond now works as a solutions architect for EMC. Raymond is an agile coach, Microsoft MVP C# and also president of the Oklahoma City Developers Group and Oklahoma Agile Developers Group. Raymond spends a lot of his time learning and teaching such things as Test Driven Development, Domain Driven Design, Design Patterns and Extreme Programming practices and principles, to name a few. Raymond is also an advocate of Alt.Net. Raymond is primarily a framework guy, so don't ask him anything about UI :) Check out Devlicio.us!