Tuesday, December 4, 2012

Difference Between Truncate and Delete


Sr. No.
Delete
Truncate
1.
Removes rows from a table or view. DELETE statements delete rows one at a time, logging each row in the transaction log, as well as maintaining log sequence number (LSN) information.
Removes all rows from a table without  logging the individual row deletions.  TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is  faster and uses fewer system and  transaction log resources.
2.
DELETE statement can be used even if you have Replication/CDC enabled for the table.
TRUNCATE statement can NOT be used if you have Replication/ CDC enabled for the table.
3.
You can use DELETE statement on a parent table and if CASCADE ON DELETE is enabled then data from child table also get deleted. If CASCADE ON DELETE is NOT enabled and any of the child table has related then you can’t delete records from parent table.
You can’t truncate a parent table irrespective of whether the child table has any record or not. Truncate table statement will fail for parent table even if CASCADE ON DELETE is enabled.
4.
Triggers get executed on Delete statement.
Triggers does NOT executed on  TRUNCATE statement.
5.
Records removed using DELETE can be roll  backed and can be restored point in time.
can be roll backed.
6.
You can specify a WHERE clause in a DELETE  FROM statement-it is all or selected records.
You can not specify a WHERE clause in a  TRUNCATE TABLE statement. It is all or  nothing
7.
DELETE is classified as DML operation.(Data Manipulation  Language)
TRUNCATE is classified as DDL operation. (Data Defition Language)

No comments:

Post a Comment