Truncate (SQL): Difference between revisions
Appearance
Content deleted Content added
m Bot: Migrating 6 interwiki links, now provided by Wikidata on d:q2664576 (Report Errors) |
WP:CHECKWIKI error fix. Fix code tag. Do general fixes and cleanup if needed. - using AWB (9429) |
||
Line 6: | Line 6: | ||
* Typically, <code>TRUNCATE TABLE</code> quickly deletes all records in a table by deallocating the data pages used by the table. This reduces the resource overhead of [[Transaction log|logging]] the deletions, as well as the number of [[Lock (database)|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 <code>TRUNCATE TABLE</code> statements to be committed or rolled back transactionally. |
* Typically, <code>TRUNCATE TABLE</code> quickly deletes all records in a table by deallocating the data pages used by the table. This reduces the resource overhead of [[Transaction log|logging]] the deletions, as well as the number of [[Lock (database)|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 <code>TRUNCATE TABLE</code> statements to be committed or rolled back transactionally. |
||
* You cannot specify a <code>WHERE</code> clause in a <code>TRUNCATE TABLE</code> statement—it is all or nothing. |
* You cannot specify a <code>WHERE</code> clause in a <code>TRUNCATE TABLE</code> statement—it is all or nothing. |
||
* <code>TRUNCATE TABLE</code> cannot be used when a foreign key references the table to be truncated, since <code>TRUNCATE TABLE</code> statements do not fire [[Database trigger|triggers]]. This could result in inconsistent data because <code>ON DELETE<code>/<code>ON UPDATE</code> triggers would not fire. |
* <code>TRUNCATE TABLE</code> cannot be used when a foreign key references the table to be truncated, since <code>TRUNCATE TABLE</code> statements do not fire [[Database trigger|triggers]]. This could result in inconsistent data because <code>ON DELETE</code>/<code>ON UPDATE</code> triggers would not fire. |
||
* In some database systems, <code>TRUNCATE TABLE</code> resets the count of an [[Identity column]] back to the identity's ''seed''. |
* In some database systems, <code>TRUNCATE TABLE</code> resets 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 <code>TRUNCATE TABLE</code> statements can be used for tables involved in log shipping. |
* In [[Microsoft SQL Server]] 2000 and beyond in full recovery mode, every change to the database is logged, so <code>TRUNCATE TABLE</code> statements can be used for tables involved in log shipping.<ref>{{cite web |
||
|url=http://support.microsoft.com/kb/272093 |
|url=http://support.microsoft.com/kb/272093 |
||
|title=Description of the effects of nonlogged and minimally logged operations on transaction log backup and the restore process in SQL Server |
|title=Description of the effects of nonlogged and minimally logged operations on transaction log backup and the restore process in SQL Server |
||
Line 23: | Line 23: | ||
{{DEFAULTSORT:Truncate (Sql)}} |
{{DEFAULTSORT:Truncate (Sql)}} |
||
[[Category:SQL keywords]] |
[[Category:SQL keywords]] |
||
{{database-stub}} |
{{database-stub}} |
Revision as of 22:26, 19 August 2013
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.
The 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 DELETE
:
- In the Oracle Database,
TRUNCATE
is implicitly preceded and followed by a commit operation. (This may also be the case in MySQL, when using a transactional storage engine.) - Typically,
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 allowTRUNCATE TABLE
statements to be committed or rolled back transactionally. - You cannot specify a
WHERE
clause in aTRUNCATE TABLE
statement—it is all or nothing. TRUNCATE TABLE
cannot be used when a foreign key references the table to be truncated, sinceTRUNCATE TABLE
statements do not fire triggers. This could result in inconsistent data becauseON DELETE
/ON UPDATE
triggers would not fire.- In some database systems,
TRUNCATE TABLE
resets 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 TABLE
statements can be used for tables involved in log shipping.[1]
References
Notes