This tutorial explains how to use the MySQL TRUNCATE TABLE statement with syntax and examples.
The TRUNCATE TABLE statement is used to remove all records from a table in MySQL. It performs the same function as a DELETE statement without a WHERE clause.
Warning: If you truncate a table, the TRUNCATE TABLE statement can not be rolled back.
The syntax for the TRUNCATE TABLE statement in MySQL is:
Parameters or Arguments
Optional. If specified, it is the name of the database.
The table that you wish to truncate.
When you truncate a table, the AUTO_INCREMENT counters on the table will be reset.
MySQL truncates the table by dropping and creating the table. Thus, the DELETE triggers for the table do not fire during the truncation.
Starting in MySQL 5.5, you can not truncate an InnoDB table that is referenced by a foreign key in another table.
Starting in MySQL 5.6, you can not truncate a NDB table that is referenced by a foreign key in another table.
In MySQL, truncating a table is a fast way to clear out records from a table if you don't need to worry about rolling back. In most cases, MySQL handles the process of table truncation a bit differently than other databases such as Oracle or SQL Server.
Let's look at an example of how to use the TRUNCATE TABLE statement in MySQL.
This example would truncate the table called customers and remove all records from that table.
It would be equivalent to the following DELETE statement in MySQL:
Both of these statements would result in all data from the customers table being deleted. The main difference between the two is that you can roll back the DELETE statement if you choose, but you can't roll back the TRUNCATE TABLE statement.
Let's look at one more example where we prefix the table name with the database name.
This example would truncate the table called products in the database called dbd.