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

Explain aggregate functions?

0 Answers  


Define self join?

0 Answers  


Difference between connected and disconnected database in .net with sql server?

0 Answers   Infosys,


If there is failure during updation of certain rows, what will be the state?

2 Answers  


Why is replication required on the sql server?

0 Answers  






What is difference between aggregate and analytic function?

0 Answers  


What are the different type of replication in sql server?

0 Answers  


Explain some stored procedure creating best practices or guidelines?

0 Answers  


What are the different Topologies in which Replication can be configured?

0 Answers  


What is a cursor, index in sql?

0 Answers  


Explain the difference between clustered and non-clustered index?

0 Answers  


Please give me queries for the following 1. To get the count of duplicate records. 2. A query to delete the duplicate records.

3 Answers   247Customer,


Categories