In SQL, the TRUNCATE TABLE
statement is a Data Manipulation Language (DML) operation that deletes all rows of a table without causing a triggered action.[1] 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".
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 (SQL)|WHERE]]
clause). The following characteristics distinguish TRUNCATE TABLE
from DELETE
:
TRUNCATE
is implicitly preceded and followed by a commit operation. (This may also be the case in MySQL, when using a transactional storage engine.)TRUNCATE TABLE
quickly deletes all records in a table by deallocating the data pages used by the table. This reduces the resource overhead of logging the deletions, as well as the number of locks acquired. Records removed this way cannot be restored in a rollback operation. Two notable exceptions to this rule are the implementations found in PostgreSQL and Microsoft SQL Server, both of which allow TRUNCATE TABLE
statements to be committed or rolled back transactionally.WHERE
clause in a TRUNCATE TABLE
statement.TRUNCATE TABLE
cannot be used when a foreign key references the table to be truncated, since TRUNCATE TABLE
statements do not fire triggers. This could result in inconsistent data because ON DELETE
/ON UPDATE
triggers would not fire.TRUNCATE TABLE
resets the count of an Identity column back to the identity's seed.TRUNCATE TABLE
statements can be used for tables involved in log shipping.[2]