SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Truncate and Delete


Truncate and Delete

Author
Message
Stuart Davies
Stuart Davies
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5798 Visits: 4740
Comments posted to this topic are about the item Truncate and Delete

-------------------------------Posting Data Etiquette - Jeff Moden Smart way to ask a questionThere are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan I would never join a club that would allow me as a member - Groucho Marx
M&M
M&M
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4141 Visits: 3909
Good question for testing the basics.

M&M
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10980 Visits: 7326
Good "back to basics" question

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Nils Gustav Stråbø
Nils Gustav Stråbø
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2703 Visits: 3575
Thanks for the question :-)

It will be interesting to see how many still think that truncate is a non-logged operation.
Kwex
Kwex
SSC-Addicted
SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)SSC-Addicted (401 reputation)

Group: General Forum Members
Points: 401 Visits: 240
I missed it because I also selected an option that said "TRUNCATE cannot be logged". Baffled to know this is otherwise :-)

Made 3 selections and thought QoTd would validate my selection since only 2 was required. That validation did not happen ... and thus, I saw the dreaded red X saying "Sorry, you were wrong!"

Kwex.
Arjun SreeVastsva
Arjun SreeVastsva
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2806 Visits: 1659
Truncate
Delete


TRUNCATE is a DDL command

DELETE is a DML command




TRUNCATE TABLE always locks the table and page but not each row

DELETE statement is executed using a row lock, each row in the table is locked for deletion



Cannot use Where Condition

We can specify filters in where clause




It Removes all the data

It deletes specified data if where condition exists.



TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.

Delete activates a trigger because the operation are logged individually.




Faster in performance wise, because it doesn’t keep any logs

Slower than truncate because, it keeps logs



Rollback is not possible

Rollback is possible




Drop all object’s statistics and marks like High Water Mark free extents and leave the object really empty with the first extent. zero pages are left in the table

keeps object’s statistics and all allocated space. After a DELETE statement is executed, the table can still contain empty pages.



TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction lo

The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row



If the table contains an identity column, the counter for that column is reset to the seed value that is defined for the column

DELETE retain the identity



Restrictions on using Truncate Statement
1. Are referenced by a FOREIGN KEY constraint.
2. Participate in an indexed view.
3. Are published by using transactional replication or merge replication.





Delete works at row level, thus row level constrains
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10980 Visits: 7326
Kwex (8/17/2011)
I missed it because I also selected an option that said "TRUNCATE cannot be logged". Baffled to know this is otherwise :-)

While the details of the truncate (i.e. the removal of individual records) is not logged, the truncate itself is logged.
use this as an example
CREATE SCHEMA test
go
CREATE TABLE test.TestTruncate (id INT IDENTITY(1,1), [description] NVARCHAR(100), value NCHAR(6))
Go
INSERT test.TestTruncate(description, value)
VALUES ('this is test line 1','test1'),('this is test line 2','test2'),('this is test line 3','test3'),('this is test line 4','test4'),
('this is test line 5','test5'),('this is test line 6','test6'),('this is test line 7','test7'),('this is test line 8','test8'),
('this is test line 9','test9'),('this is test line 10','test10'),('this is test line 11','test11'),('this is test line 12','test12'),
('this is test line 13','test13'),('this is test line 14','test14'),('this is test line 15','test15'),('this is test line 16','test16'),
('this is test line 17','test17'),('this is test line 18','test18'),('this is test line 18','test18'),('this is test line 20','test20')
SELECT * FROM test.TestTruncate
BEGIN TRANSACTION
TRUNCATE TABLE test.TestTruncate
SELECT * FROM test.TestTruncate
ROLLBACK TRANSACTION
SELECT * FROM test.TestTruncate
DROP TABLE test.TestTruncate
Go
DROP SCHEMA test
GO



____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Tom Thomson
Tom Thomson
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17460 Visits: 12331
Nils Gustav Stråbø (8/17/2011)
Thanks for the question :-)

Yes, it's a good question.
It will be interesting to see how many still think that truncate is a non-logged operation.

Interesting and somewhat horrifying - up to now, 43% said delete was not logged, which is amazing considering how much discussion of that there has been on SQLServerCentral.com; and more tha 50% got it wrong that way or some other way. 6% chosing "delete resets ID, truncate does not" is crazy, I hope these guys never use identity columns because if they believe that nonsense they are going to make some really horrible mistakes, like thinking that if their table behaves like a stack IDs will always be contiguous, which can cause lots of lovely bugs.

Tom

rfr.ferrari
rfr.ferrari
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1661 Visits: 13630
easy question!! and good question!


rfr.ferrari
DBA - SQL Server 2008
MCITP | MCTS

remember is live or suffer twice!
the period you fastest growing is the most difficult period of your life!

Stuart Davies
Stuart Davies
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5798 Visits: 4740
rfr.ferrari (8/17/2011)
easy question!!


I would liked to have thought so - but sadly (and alarmingly) not so going by the right / wrong answers given - roughly 50/50 split

-------------------------------Posting Data Etiquette - Jeff Moden Smart way to ask a questionThere are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan I would never join a club that would allow me as a member - Groucho Marx
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search