TRUNCATE
suggest changeIntroduction
The TRUNCATE statement deletes all data from a table. This is similar to DELETE with no filter, but, depending on the database software, has certain restrictions and optimizations.
Syntax
- TRUNCATE TABLE table_name;
Remarks
TRUNCATE is a DDL (Data Definition Language) command, and as such there are significant differences between it and DELETE (a Data Manipulation Language, DML, command). While TRUNCATE can be a means of quickly removing large volumes of records from a database, these differences should be understood in order to decide if using a TRUNCATE command is suitable in your particular situation.
- TRUNCATE does not fire
DELETEtriggers. Related rows are not removed for you. Many dialects also refuse TRUNCATE when another table has a foreign key to this one. - TRUNCATE deallocates data pages. DELETE removes rows; that space is often reusable inside the file and is not always returned to the operating system. Whether TRUNCATE itself can be rolled back depends on the dialect: SQL Server and PostgreSQL can roll it back, and Oracle commits it implicitly.
- If the table uses an identity column, SQL Server resets the seed. A later insert can reuse an identifier that another table still stores.
- Depending on the security roles and the dialect, you may not have permission to TRUNCATE.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents