TRUNCATE TABLE statement
TRUNCATE TABLE empties a table by dropping and recreating it, and resets the AUTO_INCREMENT counter.
TRUNCATE TABLE removes all rows of a table at once. It drops and recreates the table's storage and its unique-key claim tables, keeping the definition.
Syntax#
TRUNCATE [TABLE] [db_name.]tbl_nameArguments#
| Name | Meaning | Default |
|---|---|---|
tbl_name | The table to empty. A temporary table of that name is emptied first. | none |
Remarks#
- The
AUTO_INCREMENTcounter starts again. - A table that a foreign key refers to cannot be truncated: error 1701 (
Cannot truncate a table referenced in a foreign key constraint). - Privilege:
DROPon the table (as MySQL). - Like all definition statements it ends an open transaction (implicit commit) and cannot be rolled back.
Examples#
Example:
TRUNCATE TABLE tmp;
SELECT COUNT(*) FROM tmp; -- 0
TRUNCATE TABLE parent; -- error 1701 while child tables refer to it