The SQL TRUNCATE TABLE table statement is used to remove all the rows (complete data) from a table. It performs the same work as SQL DELETE TABLE without the WHERE clause.
Warning: You should double-check before executing the SQL TRUNCATE Table statement as once a table truncated it cannot be rolled back.
TRUNCATE TABLE vs DROP TABLE
The DROP Table command deletes the complete data of a table along with the structure of the table. On the other hand, the TRUNCATE Table deletes all the data from a table but doesn’t delete the table structure.
TRUNCATE TABLE vs DELETE TABLE
The TRUNCATE Table operation is faster and uses less resources than the DELETE TABLE command.
SQL TRUNCATE TABLE Syntax
The basic syntax of TRUNCATE Table in SQL is as follows.
TRUNCATE TABLE table_name;
Here the table_name specifies the name of the table which needs to be truncated.
SQL TRUNCATE TABLE Example
Suppose you have the following employee table.
EmpNo | Name | Salary |
121 | Sagar | 85000 |
122 | Shankar | 70000 |
123 | Ravi | 60000 |
124 | Nick | 95000 |
If you want to delete all the rows of this table, you can use the TRUNCATE TABLE command as follows.
TRUNCATE TABLE employee;