Search code examples
sql-servert-sqltruncate

SQL Server TRUNCATE


I have a question to the TRUNCATE command.

How does TRUNCATE work in background ?

I read a text that says TRUNCATE creates a copy from the table and then start to delete all rows of the table. Is it correct?


Solution

  • with the information from here

    TRUNCATE logs the deallocation of the data pages in which the data exists which means that your data rows still actually exist in the data pages, but the extents have been marked as empty for reuse. This is what makes TRUNCATE a faster operation to perform over DELETE.

    You cannot TRUNCATE a table that has any foreign key constraints. You will have to remove the contraints, TRUNCATE the table, and reapply the contraints.

    TRUNCATE will reset any identity columns to the default seed value. This means if you have a table with an identity column and you have 264 rows with a seed value of 1, your last record will have the value 264 (assuming you started with value 1) in its identity columns. After TRUNCATEing your table, when you insert a new record into the empty table, the identity column will have a value of 1