SQL Server Truncate Table
The TRUNCATE TABLE statement in SQL Server is a DDL (Data Definition Language) command. This statement is used to removes all rows from the table or specified partition without removing the table structure. It is similar to the DELETE command, but it does not allow filtering the table records because we cannot use the WHERE clause with this command. However, in terms of time and resource usage, TRUNCATE is much faster than DELETE.
This command deallocates the data pages while removing the data in the table. It is similar to dropping and re-creating the table again. Unlike the DELETE statement, it only records page deallocations instead of row-by-row in the transaction log.
We should consider the following points while using the TRUNCATE command:
The following syntax explains the TRUNCATE command to remove data from the table:
Here schema_name indicates the name of your schema in which table is stored. It is an optional parameter. The table_name is the name of a table from which data will be truncated.
If we want to truncate more than one table within a single statement, we need to execute a truncate statement for each table separately.
TRUNCATE Table Example
Let us demonstrate how we can truncate the table with the help of an example. First, we will create an "employee" table by executing the below:
Next, we will insert some data into this table as follows:
We can verify the inserted data using the SELECT statement:
Suppose we need to delete all data of this table but don't want to remove its structure. In that case, SQL Server provides the TRUNCATE TABLE statement as follows:
To verify the deletion of the data by executing the SELECT statement again. This command gives the following output that shows table columns name without having records in the table:
How to ROLLBACK TRUNCATE in SQL Server?
As we know that we cannot roll back a truncate operation after committed. However, SQL Server enables us to do something to get the whole data or at least some parts back. It is due to the data is still store in the MDF file after executing the truncate operation. We cannot see this file because SQL Server uses this as free space.
We can get the truncated data back by reading deallocated data pages and convert them into readable data before the free space is overwritten with new data.
The only way in SQL Server to roll back the truncated data is by using a transaction operation as follows:
The SELECT statement will return the truncated data. See the below output:
How to TRUNCATE Table with FOREIGN key?
SQL Server cannot allow performing the TRUNCATE operation for the table that has a foreign key constraint. However, we have a solution to do this. But both solutions present a risk of losing the data integrity.
Solution 1: First, remove the constraint and then perform the TRUNCATE operation.
Solution 2: Log in to the database server and disable foreign key checks before the TRUNCATE operation and then re-enable as below:
Advantages of TRUNCATE TABLE over DELETE
The following are the main advantages of TRUNCATE in comparison to DELETE operation:
Less transaction log space
The DELETE statement removes one row at a time and inserts an entry for each removed row in the transaction log. In contrast, the TRUNCATE statement deallocates the pages, thus delete data from the table and inserts only the page deallocations in the transaction logs.
Use fewer locks
When we execute the DELETE command with a row lock, each row in the table is locked for removal. The TRUNCATE statement, on the other hand, locks the table and pages rather than individual rows.
If the table data is removed by the TRUNCATE command with an IDENTITY column, the counter for that column is reset to the seed value. But, the DELETE statement cannot do this.