Microsoft SQL Server
TRUNCATE
In Microsoft SQL Server, the TRUNCATE statement is used to quickly remove all rows from a table, similar to the DELETE statement. However, unlike DELETE, TRUNCATE is faster because it doesn’t log individual row deletions and doesn't allow filtering or use of the WHERE clause. It simply removes all data from a table and resets any identity columns to their initial values. For beginners, it’s important to understand that TRUNCATE is irreversible and should be used with caution.
Here’s a breakdown of how to use the TRUNCATE statement with examples:
- Basic Syntax of the
TRUNCATEStatement:- The basic syntax for truncating a table is:
TRUNCATE TABLE table_name; - Steps for Using the
TRUNCATEStatement:- Specify the target table:
- You use the
TRUNCATE TABLEstatement followed by the name of the table from which you want to remove all rows.
- You use the
- Understand that TRUNCATE is non-filterable:
- Unlike the
DELETEstatement,TRUNCATEdoesn’t support theWHEREclause. It will remove all rows from the table.
- Unlike the
- Specify the target table:
- Example - Truncating the
SalesTable:- Suppose you want to remove all data from the
Salestable. Here’s how you would do it:
TRUNCATE TABLE Sales;- This will remove every row from the
Salestable and reset any identity columns, such asSaleID.
- Suppose you want to remove all data from the
- Example - Truncating the
ProductsTable:- If you want to remove all products from the
Productstable to reset the table:
TRUNCATE TABLE Products;- This will clear all product records and reset the identity column for
ProductID.
- If you want to remove all products from the
- Differences Between
TRUNCATEandDELETE:- Speed:
TRUNCATEis faster thanDELETEbecause it doesn’t log individual row deletions. - Identity columns reset: If the table has an identity column (e.g.,
CustomerID),TRUNCATEresets the counter, whileDELETEdoes not. - No WHERE clause:
TRUNCATEdoes not allow you to filter which rows to remove, unlikeDELETE, which lets you specify conditions.
- Speed:
- Best Practices:
- Backup your data: Before using
TRUNCATE, ensure that the data is backed up because it’s irreversible. - Use on tables with foreign keys cautiously:
TRUNCATEcannot be used on tables referenced by foreign key constraints. In such cases, you must either remove the foreign keys or use theDELETEstatement instead. - Avoid in production without careful planning: Since
TRUNCATEremoves all rows without logging individual deletions, it should be used cautiously in live environments.
- Backup your data: Before using
- TRUNCATE vs. DELETE in a Nutshell:
- TRUNCATE is faster but can’t be filtered.
- TRUNCATE resets identity columns.
- DELETE logs each deletion and supports filters via the
WHEREclause.
By following these guidelines, beginners can effectively use the TRUNCATE statement in SQL Server for fast and efficient data removal. Just remember to be cautious as it removes all data from the table without any filters.
To gain complete access, login with gmail or outlook, no need of signup, click here


Comments Not Found