Difference between truncate and delete in sql server with example

DELETE:


1. Delete is a DML command.

2. Delete statement is executed using a row lock, each row in the table is locked   for   deletion.         

3. We can specify filters in where clause

4. It deletes specified data if where condition exists.

5. Delete activates a trigger because the operation are logged individually.

6. Slower than truncate because, it keeps logs.

7. Rollback is possible.

8.Table identity column is reset to seed value



TRUNACTE:


1. Truncate is a DDLcommand.

2. Truncate table always locks the table and page but not each row.

3. Cannot use where condition.

4. It removes all the data.

5. truncate table cannot activate a trigger because the operation does not log 

    individual row deletions.

6. Faster in performance wise, because it does not keep any logs.

7. Rollback is not possible.

8.Table identity column is not reset



*Delete and truncate both can be rolled back when used with transaction.


*If transaction is done, means commited, then we can not rollback truncate command,

but we can still rollback delete command from log files,


as delete write records them in log file in case it is needed to rollback in future from log files.


Comments

Popular posts from this blog

Difference between INTERSECT and INNER JOIN?