what is the difference between Delete and Truncate
Answers were Sorted based on User's Feedback
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 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 |
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 |
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 |
How would you Update the rows which are divisible by 10, given a set of numbers in column?
What is exclusive locks?
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)
Why you need indexing? Where that is stored and what you mean by schema object? For what purpose we are using view?
Tell me what are the advantages of using stored procedures?
What is log shipping?
Explain the difference between function and stored procedure?
diffrence between Cluster Index and non Cluster Index
How to optimize stored procedure optimization?
How do you measure the performance of a stored procedure?
How to create an inline table-valued function?
Why I have to use stored procedures?