Pages

Saturday 12 January 2013

Difference between Truncate and Delete commands in SQL Server

Truncate

Truncate command removes all rows present in a table without logging individual row deletion in transaction log. Truncate deletes only data present inside the table without affecting or modifying the table structure. We cannot use WHERE clause with Truncate statements. TRUNCATE is a DDL command.

Truncate removes identity.If the table contains an identity column, the counter for that column is reset to the seed value that is defined for the column.It Removes all the data.The operation cannot be rolled back and no triggers will be fired.Truncate command deletes rows page by page

Syntax for truncate command is shown below:

TRUNCATE TABLE table_name
 
Delete

Delete command also removes all rows present in a table but it removes all data and logs deletion of  individual row in transaction log. Same as Truncate, Delete command removes data from table without affecting or modifying table structure. We can use the WHERE clause with this Delete statement. In case of delete I am writing just the simple syntax for more detailed explanation of delete syntax visit this link: DELETE (Transact-SQL). Syntax is shown below, for more detailed information of Delete command syntax. DELETE is a DML command.After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it.Slower than truncate because, it maintain logs for every record.The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row.
DELETE retain the identity.Delete works at row level, thus row level constrains apply


Following command will delete all rows from table:

DELETE FROM table_name
 

No comments:

Post a Comment