views:

54

answers:

5

Hey everyone,

Could someone give me a quick overview of the pros and cons of using the following two statements:

TRUNCATE TABLE dbo.MyTable

vs

DELETE FROM dbo.MyTable

It seems like they both do the same thing when all is said and done; but are there must be differences between the two.

Thanks again!

+1  A: 

TRUNCATE doesn't generate any rollback data, which makes it lightning fast. It just deallocates the data pages used by the table.

However, if you are in a transaction and want the ability to "undo" this delete, you need to use DELETE FROM, which gives the ability to rollback.

dcp
`TRUNCATE` may also break consistency (=doesn't check for foreign keys, and doesn't fire triggers)
Yossarian
@Yossarian - According to MSDN: "You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint; instead, use DELETE statement without a WHERE clause". http://msdn.microsoft.com/en-us/library/aa260621%28SQL.80%29.aspx
dcp
A: 

truncate doesnt do any logging, delete does, so if you have a ton of records, your trans log is huge

ScaleOvenStove
+1  A: 

Google is your friend:

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=truncate+vs+delete

You don't even need to go very far - the first result gives you a good comparison:

http://www.mssqltips.com/tip.asp?tip=1080

Charles Boyung
A: 

TRUNCATE TABLE doesn't log the transaction. That means it is lightning fast for large tables. The downside is that you can't undo the operation.

DELETE FROM logs each row that is being deleted in the transaction logs so the operation takes a while and causes your transaction logs to grow dramatically. The upside is that you can undo the operation if need be.

Justin Niessner
A: 

Another key point not mentioned in the other answers is that TRUNCATE TABLE will reset you identity to the initial seed, whereas DELETE FROM will carry on incrementing from where it left off.

Ben Robinson

related questions