⥠The TRUNCATE TABLE statement is a Data Definition Language (DDL) command used to remove all rows from a table quickly and efficiently while preserving the table structure. Unlike DELETE, TRUNCATE TABLE removes every record without using a WHERE clause and is generally much faster for large tables.
đ What is TRUNCATE TABLE?
The TRUNCATE TABLE statement deletes all rows from a table but keeps the table itself, including its columns, indexes, and constraints. After truncation, the table remains empty and can immediately accept new data.
Information
đ¯ Why Use TRUNCATE TABLE?
TRUNCATE TABLE is useful when you need to quickly clear all data from a table without removing the table itself.
- đ Remove all records efficiently.
- đ Reset temporary or staging tables.
- đ Prepare tables for importing fresh data.
- đ Improve performance compared to deleting millions of rows.
- đ Keep the table structure unchanged.
đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Marks |
|---|---|---|---|
| 101 | Alice | Computer Science | 92 |
| 102 | Bob | Mathematics | 85 |
| 103 | Charlie | Physics | 78 |
đ Basic Syntax
TRUNCATE TABLE Syntax
TRUNCATE TABLE table_name;đĄ Truncate a Table
Remove every record from the Students table while keeping the table available for future use.
Truncate Students Table
TRUNCATE TABLE Students;After execution, the table structure remains unchanged, but all rows are removed.
đ Table After TRUNCATE
| StudentID | Name | Department | Marks |
|---|
đ Reusing the Table
Since the table structure still exists, new rows can be inserted immediately.
Insert After TRUNCATE
INSERT INTO Students
(StudentID, Name, Department, Marks)
VALUES
(104, 'David', 'Computer Science', 95);âī¸ TRUNCATE TABLE vs DELETE vs DROP TABLE
| Feature | TRUNCATE TABLE | DELETE | DROP TABLE |
|---|---|---|---|
| Removes Rows | â All rows | â Selected or all rows | â Entire table |
| Supports WHERE | â No | â Yes | â No |
| Keeps Table Structure | â Yes | â Yes | â No |
| Performance | Generally faster | Generally slower for large tables | Removes the table completely |
| Typical Use | Clear all data quickly. | Delete specific records. | Remove the table permanently. |
đĸ Identity and Auto-Increment Considerations
Some database systems reset identity or auto-increment values after a TRUNCATE TABLE operation, while others may behave differently.
| Database System | Typical Behavior |
|---|---|
| MySQL | Typically resets the AUTO_INCREMENT counter. |
| SQL Server | Typically resets the identity seed. |
| PostgreSQL | Can restart associated identity values using supported options. |
| Oracle | Sequences are independent and are not automatically reset. |
Important
đŧ Real-World Example
An ETL process loads fresh sales data every night into a staging table. Before importing the new data, yesterday's staging records are removed using TRUNCATE TABLE.
Clear a Staging Table
TRUNCATE TABLE SalesStaging;This quickly clears the staging table so the latest data can be imported.
đī¸ Database Compatibility
| Database System | TRUNCATE TABLE Support |
|---|---|
| MySQL | â Supported. |
| PostgreSQL | â Supported with additional options such as RESTART IDENTITY and CASCADE. |
| SQL Server | â Supported with some restrictions. |
| Oracle | â Supported. |
| SQLite | â Does not support TRUNCATE TABLE; use DELETE FROM table_name; instead. |
â ī¸ Common Mistakes
- â Expecting to delete only selected rows.
- â Using TRUNCATE TABLE when a WHERE clause is required.
- â Forgetting that all rows will be removed.
- â Attempting to truncate a table referenced by foreign key constraints when the database restricts it.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ TRUNCATE TABLE removes all rows from a table.
- đ The table structure, columns, indexes, and constraints remain intact.
- đ WHERE cannot be used with TRUNCATE TABLE.
- đ It is generally faster than deleting all rows with DELETE.
- đ Auto-increment or identity behavior depends on the database system.
- đ Foreign key relationships may restrict truncation in some databases.