Truncate (SQL) explained

In SQL, the TRUNCATE TABLE statement is a data manipulation language (DML)[1] operation that deletes all rows of a table without causing a triggered action. The result of this operation quickly removes all data from a table, typically bypassing a number of integrity enforcing mechanisms. It was officially introduced in the standard, as the optional feature F200, "TRUNCATE TABLE statement".

Behavior

TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes, and so on remain. To remove the table definition in addition to its data, use the DROP TABLE statement.

The TRUNCATE TABLE mytable statement is logically (though not physically) equivalent to the [[Delete (SQL)|DELETE]] FROM mytable statement (without a WHERE clause). The following characteristics distinguish TRUNCATE TABLE from DELETE:

DML/DDL

The SQL standard classifies TRUNCATE as a data change statement, synonymous with data manipulation (DML). This aligns with TRUNCATE being logically equivalent to an unconstrained DELETE operation.

However, some documents describe TRUNCATE as a data definition language (DDL) operation, because TRUNCATE may be seen as a combined DROP+CREATE operation.[2]

Notes and References

  1. The BNF for SQL:2023 defines TRUNCATE as an SQL data change statement: Web site: ISO/IEC 9075 BNF. iso.org . 2024-12-30 . ISO/IEC 9075-2.
  2. For example, MySQL's documentation classifies TRUNCATE as a DDL statement: Web site: MySQL :: MySQL 8.4 Reference Manual :: 15.1.37 TRUNCATE TABLE Statement . 2024-12-30 . dev.mysql.com.