TRUNCATE TABLE (Transact-SQL)
Removes all rows from a table quickly, but preserves the table structure. This is faster than DELETE without a WHERE clause.
Syntax
TRUNCATE TABLE table_name [ WITH ( options [ , ... ] ) ]
Where options can be:
FILESTREAM_ குறைக்க = { ON | OFF }EMPTYFILENOCONSERVE_SPACE
Description
TRUNCATE TABLE is used to remove all rows from a table. It is more efficient than the DELETE statement without a WHERE clause. TRUNCATE TABLE is minimally logged and uses the space deallocation mechanism. This means it is faster and uses fewer system resources than deleting rows one by one.
Key characteristics of TRUNCATE TABLE:
- It removes all rows from the table.
- It is faster and uses fewer resources than
DELETEwithout a WHERE clause. - It cannot be used on tables referenced by FOREIGN KEY constraints, unless the constraint is dropped or disabled first.
- It cannot be used on tables that are part of replication or other system-level operations where row-by-row processing is required.
- It resets identity columns to their seed values.
- It is a Data Definition Language (DDL) operation, not a Data Manipulation Language (DML) operation.
Permissions
TRUNCATE TABLE requires ALTER permission on the table. By default, ALTER permission is held by the table owner, schema owner, and sysadmin role members.
Restrictions
TRUNCATE TABLEcannot be used if the table is referenced by aFOREIGN KEYconstraint, or if it is subject to anINSERTorUPDATEtrigger.TRUNCATE TABLEcannot be used on tables that are part of a replication configuration or an indexed view.TRUNCATE TABLEcannot be used on a table with aPRIMARY KEYconstraint that is referenced by aFOREIGN KEYconstraint in another table.
Usage Examples
Example 1: Truncating a simple table
TRUNCATE TABLE Sales.SalesOrderDetail;
This statement removes all rows from the Sales.SalesOrderDetail table.
Example 2: Truncating a table with FILESTREAM data
TRUNCATE TABLE dbo.MyDocs WITH (FILESTREAM_ குறைக்க = ON);
This statement truncates the dbo.MyDocs table, which contains FILESTREAM data. The FILESTREAM_ குறைக்க option ensures that the FILESTREAM data is also purged.
Comparison with DELETE
While both TRUNCATE TABLE and DELETE can remove all rows from a table, they differ significantly:
- Logging:
DELETElogs each row deletion, making it slower and more resource-intensive.TRUNCATE TABLEis minimally logged. - Transaction Log:
DELETEcan be rolled back and operations within it are logged.TRUNCATE TABLEcan be rolled back, but it's a faster operation. - Triggers:
DELETEfiresDELETEtriggers.TRUNCATE TABLEdoes not fire triggers. - Identity Reset:
DELETEdoes not reset identity values.TRUNCATE TABLEresets identity values to their seed. - Where Clause:
DELETEcan use a WHERE clause to remove specific rows.TRUNCATE TABLEremoves all rows.
TRUNCATE TABLE when you need to empty a table quickly and do not need to preserve individual row information or fire triggers.
TRUNCATE TABLE is a destructive operation. It cannot be undone easily if you do not have a backup.