- Truncate (SQL)
In
SQL , theTRUNCATE
statement removes all the data from a table. TheTRUNCATE
statement is not actually a part of theSQL standard, but manyrelational database management system s implement it. [ [http://dev.mysql.com/doc/refman/6.0/en/truncate.html MySQL 6.0 Reference Manual, 11.2.10 TRUNCATE Syntax] ] [ [http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_10007.htm Oracle® Database SQL Language Reference, TRUNCATE TABLE] ] [ [http://www.postgresql.org/docs/current/static/sql-truncate.html PostgreSQL Documentation, SQL Commands, TRUNCATE] ] ATRUNCATE my_table;
it is equivalent in function to aDELETE FROM my_table;
statement. However, on some systems, it is implemented differently. For example, in Oracle the truncate statement implies a commit and cannot be undone.The syntax for issuing a truncate command on a table could be implemented as:Truncate Table [schema] [.]
; TRUNCATE TABLE is a statement that 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; however, it bypasses the transaction log, and the only record of the truncation in the transaction logs is the page deallocation. Records removed by the TRUNCATE TABLE statement cannot be restored. Notable exception from this rule is
PostgreSQL 's implementation, where TRUNCATE is transactional - old version of the whole table is kept until COMMIT or ROLLBACK is issued. You cannot specify a WHERE clause in a TRUNCATE TABLE statement-it is all or nothing. The advantage to using TRUNCATE TABLE is that in addition to removing all rows from the table it resets the IDENTITY back to the SEED, and the deallocated pages are returned to the system for use in other areas.In addition, TRUNCATE TABLE statements cannot be used for tables involved in replication or log shipping, since both depend on the transaction log to keep remote databases consistent.TRUNCATE TABLE cannot be used when a foreign key references the table to be truncated, since TRUNCATE statements do not fire triggers. This could result in inconsistent data because ON DELETE/UPDATE triggers would not fire. If all table rows need to be deleted and there is a foreign key referencing the table, you must drop the index and recreate it. If a TRUNCATE TABLE statement is issued against a table that has foreign key references, the following error is returned:
e.g.Truncate Table [schema] [.]
; Notes
Wikimedia Foundation. 2010.