Description
The SQL TRUNCATE TABLE statement can be used to delete all records from a database table, quicker than a DELETE statement without a WHERE clause.
- Deletes all the records from a table, similar to a DELETE statement without a WHERE clause.
- But the table is not deleted and its structure, attributes, and indexes will be intact and untouched.
In the below example, the TRUNCATE TABLE statement is used to delete all records from a database table.
TRUNCATE TABLE employees;
In case the table doesn't exist, it returns an error message, which can be avoided by including the keyword IF EXISTS
as shown below.
TRUNCATE TABLE IF EXISTS employees;
Overall
We now know when and how to use the TRUNCATE TABLE statement to remove all the records from a table.