TRUNCATE Statement
The TRUNCATE statement is used to remove all records from a table quickly, but the table structure and its schema remain intact. It resets any identity columns to their seed values.
Example: Truncating a Table
TRUNCATE TABLE FreedomFighters;
Output:
All records from FreedomFighters table have been deleted.
Note:
TRUNCATE is faster than DELETE because it does not generate individual row delete operations. However, it does not fire any triggers that might be associated with the table.
Do's and Don'ts
Do's
- Use
TRUNCATEfor quick removal of all records when you do not need to fire triggers. - Ensure you have a backup if the data is important, as
TRUNCATEcannot be rolled back if not used inside a transaction. - Be aware of any relationships or dependencies before truncating a table.
Don'ts
- Don't use
TRUNCATEon tables referenced by foreign key constraints. - Don't rely on
TRUNCATEif you need to trigger actions on row deletion. - Don't use
TRUNCATEin a production environment without proper caution and backup.