In SQL, the
TRUNCATE TABLE statement is a Data Definition Language (DDL) operation that marks the extents of a table for deallocation (empty for reuse). 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 SQL:2008 standard.
TRUNCATE TABLE mytable statement is logically (though not physically) equivalent to the
DELETE FROM mytable statement (without a
WHERE clause). The following characteristics distinguish
TRUNCATE TABLE from
- In the Oracle Database,
TRUNCATEis implicitly preceded and followed by a commit operation. (This may also be the case in MySQL, when using a transactional storage engine.)
TRUNCATE TABLEquickly 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 TABLEstatements to be committed or rolled back transactionally.
- You cannot specify a
WHEREclause in a
TRUNCATE TABLEstatement—it is all or nothing.
TRUNCATE TABLEcannot be used when a foreign key references the table to be truncated, since
TRUNCATE TABLEstatements do not fire triggers. This could result in inconsistent data because
ON UPDATEtriggers would not fire.
- In some database systems,
TRUNCATE TABLEresets the count of an Identity column back to the identity's seed.
- In Microsoft SQL Server 2000 and beyond in full recovery mode, every change to the database is logged, so
TRUNCATE TABLEstatements can be used for tables involved in log shipping.
|This database-related article is a stub. You can help Wikipedia by expanding it.|
|This programming-language-related article is a stub. You can help Wikipedia by expanding it.|