Pages

Sunday, 23 September 2012

Difference between Truncate and Delete in SQL.

Another most frequently asked interview question.

Truncate
Delete
We can’t Rollback after performing Truncate.
Example: 
BEGIN TRAN
TRUNCATE TABLE tranTest
SELECT * FROM tranTest
ROLLBACK
SELECT * FROM tranTest
We can Rollback after delete.
Example: 
BEGIN TRAN
DELETE FROM tranTest
SELECT * FROM tranTest
ROLLBACK
SELECT * FROM tranTest
Truncate reset identity of table.
Truncate reset identity of table.
It locks the entire table.
It locks the table row.
Its DDL(Data Definition Language) command.
Its DML(Data Manipulation Language) command.
We  can’t use WHERE clause with it.
We can use WHERE to filter data to delete.
Trigger is not fired while truncate.
Trigger is fired.
Syntax : 
1) TRUNCATE TABLE table_name

Syntax : 
1) DELETE FROM table_name
2) DELETE FROM table_name WHERE 
example_column_id IN (1,2,3)

No comments:

Post a Comment