Truncate Table Statement (Difference between Truncate and Delete)

Thursday, August 28, 2008

Truncate table statement can delete all record of table. There is no where clause uses with truncate command. Delete command removes record for the table and put entry in transaction log of every deleted record. Truncate table removes the data by de-allocating the data pages and records only the page de-allocations in the transaction log. Table records are store in data pages.
The actual data in your table is stored in Data Pages. The Page is the smallest unit of data storage in Microsoft SQL Server. A page contains the data in the rows. A row can only reside in one page. Each Page can contain 8KB of information, due to this; the maximum size of a Row is 8KB. A group of 8 adjacent pages is called an extent.
For better explanation of data pages, we take one example. Suppose I have 100 records in Employee table. These 100 records divided into 10 pages.
Records (rows of a table) keep in data page. After using truncate command, every pages deallocation entry would be maintain in transaction log. Truncate command can also be roll back. When we roll back the Truncate command, de-allocated pages would be roll back

For Example:- tbl_Employee table has 100 records. After executing the first 2 step, record would be zero in tbl_Employee table.
1) Begin Transaction
2) TRUNCATE TABLE tbl_Employee
Run the following select command for verifying the output.
3) Select * from tbl_Employee
Result would be zero record.
Now rollback the transaction
4) Rollback Transaction
Run the following select command again and find the output of 100 records.
5) Select * from tbl_Employee

Truncate command has following advantage over Delete command

->The DELETE command removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE command removes the data by de-allocating the data pages (used to store the table data) and records only the page de-allocations in the transaction log.

->When the DELETE command is executed using a row lock, each row in the table is locked for deletion. TRUNCATE command always locks the table and page, not each row.

->Truncate command de-allocates the pages that why it is faster than delete command.

->After a DELETE statement is executed, the table can still contain empty pages. For example, empty pages in a heap cannot be de-allocated. For indexes, the delete operation can leave empty pages behind, although these pages will be de-allocated quickly by a background cleanup process.

TRUNCATE TABLE command removes all rows from a table, not the structure (columns, constraints, indexes, etc) of the table. Table structure and its columns, constraints, indexes, etc remain as it is. To remove the table definition, structure and its data in one go, use the DROP TABLE statement.

Truncate table has following limitation

->You can’t use TRUNCATE TABLE statement on a table which is referenced by a FOREIGN KEY constraint. (You can truncate a table that has a foreign key that references itself.)

->You can’t use “where” clause with TRUNCATE TABLE statement.

Truncate command de-allocates the pages which hold multiple records that why we can’t use “where” clause with truncate command.

2 comments:

  1. Mukesh Thakur said...

    Really nice explaination. thanx

  2. javin paul said...

    fantastic post, delete is better if you want to rollback removed data, see here for more details truncate vs delete in SQL l

Post a Comment