Successfully added
SQL
by Patrik
What Does TRUNCATE TABLE Do in SQL?
TRUNCATE TABLE Explained
The SQL command TRUNCATE TABLE removes all rows from a table at once — quickly and efficiently.
Unlike DELETE, which deletes rows one by one, TRUNCATE empties the entire table without affecting its structure.
That means columns, data types, indexes, and permissions remain intact — only the data is gone.
When to Use TRUNCATE
Use TRUNCATE TABLE when
- you want to clear all data from a table,
- you don’t need a WHERE clause, and
- speed matters.
Example:
This command instantly removes all records from the customers table while keeping the table ready for new data.
Key Differences: TRUNCATE vs. DELETE
| Aspect | TRUNCATE TABLE |
DELETE FROM |
|---|---|---|
| Supports WHERE clause | No | Yes |
| Speed | Very fast | Slower |
| Rollback possible | Only in a transaction | Yes |
| Triggers fire | Usually no | Yes |
| Resets AUTO_INCREMENT | Yes (depends on DB) | No |
Important Notes
- You cannot truncate a table that is referenced by a foreign key.
- In most databases,
TRUNCATEis treated as a DDL command (Data Definition Language), likeCREATEorDROP. - Best practice: use
TRUNCATEfor temporary or helper tables where you’re certain the data can be safely removed.
SQL
databases
TRUNCATE
basics
performance
Referenced in:
Comments