SQL - Truncate Table

The SQL TRUNCATE TABLE statement is used to delete all rows from a table, effectively emptying the table while keeping its structure intact. Unlike the DELETE statement, which removes individual rows and logs each deletion, the TRUNCATE TABLE statement is a more efficient way to remove all data from a table. 

TRUNCATE TABLE table_name;
  • TRUNCATE TABLE: This SQL keyword is used to indicate that you want to truncate the specified table.
  • table_name: Specify the name of the table you want to truncate.

For example, to truncate a table named "Customers", you would use the following statement:

TRUNCATE TABLE Customers;

Executing this statement will delete all rows from the "Customers" table, effectively emptying it. However, the table structure, column definitions, and indexes will remain unchanged.

It's important to note that the TRUNCATE TABLE statement is a data definition language (DDL) statement, not a data manipulation language (DML) statement like DELETE. As a result, it cannot be rolled back using a transaction and does not trigger any triggers or fire any associated delete triggers.

Additionally, it's crucial to exercise caution when using the TRUNCATE TABLE statement, as it permanently removes all data from the table. Make sure to take proper backups and ensure that you have the necessary permissions to perform the truncation operation.