C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Databases »

Delete VS Truncate ...


Posted Date: 24 Dec 2006    Resource Type: Articles    Category: Databases
Author: Vadivel MohanakrishnanMember Level: Diamond    
Rating: 1 out of 5Points: 10



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.



Responses

Author: Thamil Selvan J.    17 Jan 2007Member Level: Silver   Points : 0
Good article. Its very useful to all.


Author: David Portas    09 Mar 2008Member 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 2008Member 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 2008Member 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 2009Member 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.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Avoiding SQL Injection Attacks !!!
Previous Resource: Rolling back a truncate operation!
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use