TRUNCATE TABLE: Remove all data from a table but retain its structure
#Comparison of TRUNCATE and DELETE commands in SQL #TRUNCATE TABLE: Remove all data from a table but retain its structure
The TRUNCATE TABLE statement in SQL is used to quickly remove all records from a table without logging individual row deletions. Unlike the DELETE statement, which removes records one by one and logs each action, TRUNCATE TABLE deallocates the entire table’s data pages, making it a faster and more efficient way to clear a table.
TRUNCATE TABLE table_name;
Replace table_name with the name of the table you want to truncate.
TRUNCATE runs much faster than DELETE.AUTO_INCREMENT primary key columns to their starting value.TRUNCATE cannot be undone.DELETE, it does not activate ON DELETE triggers.| Feature | TRUNCATE TABLE | DELETE |
|---|---|---|
| Speed | Fast | Slower (logs each row deletion) |
| Rollback | Not always possible | Possible (if used within a transaction) |
| Auto-Increment Reset | Yes | No |
| Trigger Activation | No | Yes |
employees table.
TRUNCATE TABLE employees;
This command instantly clears all records from the employees table while preserving its structure.
✅ When you need to remove all records quickly.
✅ When you want to reset auto-increment counters.
✅ When you don’t need to track deleted records.
❌ If you need to delete specific rows instead of the entire table.
❌ If you want to preserve auto-increment values.
❌ If your database requires rollback capability.
The TRUNCATE TABLE command is a powerful and efficient way to remove all records from a table in SQL. However, it should be used cautiously, as the operation cannot be undone in some databases. Understanding its differences from DELETE can help optimize database management and performance.