SQL reference (MySQL and MariaDB dialects)

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#

SQL
TRUNCATE [TABLE] [db_name.]tbl_name

Arguments#

NameMeaningDefault
tbl_nameThe table to empty. A temporary table of that name is emptied first.none

Remarks#

  • The AUTO_INCREMENT counter 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: DROP on the table (as MySQL).
  • Like all definition statements it ends an open transaction (implicit commit) and cannot be rolled back.

Examples#

Example:

SQL
TRUNCATE TABLE tmp;
SELECT COUNT(*) FROM tmp;     -- 0
TRUNCATE TABLE parent;        -- error 1701 while child tables refer to it

See also#