Wednesday, February 16, 2011

Difference Between Delete & Truncate Commands

  • TRUNCATE is a DDL (data definition language) command whereas DELETE is a DML (data manipulation language) command
  • You can use WHERE clause(conditions) with DELETE but you can't use WHERE clause with TRUNCATE
  • A trigger doesn’t get fired in case of TRUNCATE whereas Triggers get fired in DELETE command
  • TRUNCATE resets the Identity counter if there is any identity column present in the table where delete not resets the identity counter
  • Delete and Truncate both are logged operation.But DELETE is a logged operation on a per row basis and TRUNCATE logs the de-allocation of the data pages in which the data exists. 
  • TRUNCATE is faster than DELETE 
  • Reason:When you type DELETE.all the data get copied into the Rollback Table space first.then delete operation get performed.That's why when you type ROLLBACK after deleting a table you can get back the data(The system get it for you from the Rollback Table space).All this process take time.But when you type TRUNCATE it removes data directly without copying it into the Rollback Table space.That's why TRUNCATE is faster.Once you Truncate you can't get back the data.

    Misconception : You Cannot Rollback  data when Using TRUNCATE Command.
    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.

    USE tempdb
    GO-- Create Test Table
    CREATE TABLE TruncateTest (ID INT)
    INSERT INTO TruncateTest (ID)
    SELECT 1
    UNION ALL
    SELECT 2
    UNION ALL
    SELECT 3
    GO-- Check the data before truncate
    SELECT * FROM TruncateTest
    GO-- Begin Transaction
    BEGIN TRAN
    -- Truncate Table
    TRUNCATE TABLE TruncateTest
    GO-- Check the data after truncate
    SELECT * FROM TruncateTest
    GO-- Rollback Transaction
    ROLLBACK TRAN
    GO-- Check the data after Rollback
    SELECT * FROM TruncateTest
    GO-- Clean up
    DROP TABLE TruncateTest
    GO
     
    Misconception :It is a common mistake to think that TRUNCATE is not logged 
    This is wrong.Actually, TRUNCATE does not remove data, but rather deallocates whole data pages and removes pointers to indexes. The data still exists until it is overwritten or the database is shrunk. This action does not require a lot of resources and is therefore very fast.
    The deallocation of the data pages is recorded in the log file,but the extents have been marked as empty for reuse. Therefore, BOL refers to TRUNCATE operations as "minimally logged" operations. You can use TRUNCATE within a transaction, and when this transaction is rolled-back, the data pages are reallocated again and the database is again in its original, consistent state.

No comments:

Post a Comment