Saturday 7 September 2013

Difference between DELETE and TRUNCATE command in SQL

     Difference between DELETE and TRUNCATE command

The most important and the most favorite question of the interviewers, that i had came across, is 'what's the difference between delete and truncate command?'. It is very important to know the difference between the two when you are working on a database.

Few important difference are as follows:
  1. TRUNCATE:
    • TRUNCATE is faster and uses fewer system and transaction log resources than DELETE.
    • TRUNCATE removes the data by de-allocating the data pages used to store the table's data, and only the page de-allocations are recorded in the transaction log.
    • TRUNCATE removes all rows from a table, but the table structure, its columns, constraints, indexes and so on, remains. 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 cannot be rolled back.
    • TRUNCATE is DDL Command.
    • TRUNCATE Resets identity of the table
    • Cannot use WHERE conditions
  2. 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.
    • DELETE is DML Command.
    • DELETE does not reset identity of the table.
Recently, I came across a database. The size of the MDF file of that database was around 300 GB and when i took the backup of the database the backup file was of only 90 GB. I was confused and consult with the team that was working on that database to know there basic practice of working on DB. I found that they always use 'DELETE' command to remove data from a table. As mentioned above, DELETE command always stores an entry for each row in transaction log and hence,  this discrepancy was observed.

It is always recommended that you use TRUNCATE over DELETE if you want to delete all the records from the table.

One more impotant difference is: DELETE and TRUNCATE both can be rolled back when surrounded by TRANSACTION if the current session is not closed. If TRUNCATE is written in Query Editor surrounded by TRANSACTION and if session is closed, it can not be rolled back but DELETE can be rolled back.


3 comments:

  1. Truncate can be rolled back inside transaction.

    ReplyDelete
  2. DROP statement is one more command that is used to remove a table definition and all data, indexes, triggers, constraints, and permission specifications for that table.

    you can take the help of this link to know more about truncate, delete and drop command: http://sqltechtips.blogspot.com/2015/10/truncate-delete-and-drop-in-sql.html

    ReplyDelete