what is the difference between Delete and Truncate

Answers were Sorted based on User's Feedback



what is the difference between Delete and Truncate..

Answer / soorai ganesh

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.

I believe its enough to you...............

Is This Answer Correct ?    4 Yes 0 No

what is the difference between Delete and Truncate..

Answer / bhaskar

What is difference between DELETE & TRUNCATE commands?
Delete command removes the rows from a table based on the
condition that we provide with a WHERE clause. Truncate
will actually remove all the rows from a table and there
will be no data in the table after we run the truncate
command.
TRUNCATE
TRUNCATE is faster and uses fewer system and transaction
log resources than DELETE.
TRUNCATE removes the data by deallocating the data pages
used to store the table’s data, and only the page
deallocations are recorded in the transaction log.
TRUNCATE removes all rows from a table, but the table
structure and its columns, constraints, indexes and so on
remain. The counter used by an identity for new rows is
reset to the seed for the column.
You cannot use TRUNCATE TABLE on a table referenced by a
FOREIGN KEY constraint.
Because TRUNCATE TABLE is not logged, it cannot activate a
trigger.
TRUNCATE can not be Rolled back using logs.
TRUNCATE is DDL Command.
TRUNCATE Resets identity of the table.



DELETE
DELETE removes rows one at a time and records an entry in
the transaction log for each deleted row.
If you want to retain the identity counter, use DELETE
instead. If you want to remove table definition and its
data, use the DROP TABLE statement.
DELETE Can be used with or without a WHERE clause
DELETE Activates Triggers.
DELETE Can be Rolled back using logs.
DELETE is DML Command.
DELETE does not reset identity of the table.

Is This Answer Correct ?    1 Yes 0 No

what is the difference between Delete and Truncate..

Answer / anil sharma

Delete command execute row by row and it can't reset any
identity column's value.
But using Truncate command it reset all table and Identity
column also.So truncate command is fast then delete command.

Is This Answer Correct ?    0 Yes 0 No

what is the difference between Delete and Truncate..

Answer / manoj pandey

Check following blog post for Difference Delete vs Truncate: http://sqlwithmanoj.wordpress.com/2009/02/22/difference-between-truncate-delete-and-drop-commands/

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL Server Interview Questions

How would you Update the rows which are divisible by 10, given a set of numbers in column?

3 Answers  


What is exclusive locks?

0 Answers  


How to fetch records from a One to Many relationship table. eg: wanna get details of all orders for a specific customer. (do not want repeatation of master table records for child table records)

2 Answers  


Why you need indexing? Where that is stored and what you mean by schema object? For what purpose we are using view?

0 Answers  


Tell me what are the advantages of using stored procedures?

0 Answers  






What is log shipping?

0 Answers  


Explain the difference between function and stored procedure?

0 Answers  


diffrence between Cluster Index and non Cluster Index

3 Answers   Wipro,


How to optimize stored procedure optimization?

0 Answers  


How do you measure the performance of a stored procedure?

3 Answers   Infosys,


How to create an inline table-valued function?

0 Answers  


Why I have to use stored procedures?

0 Answers   Cognizant,


Categories