Truncate (SQL): Difference between revisions
Aadirulez8 (talk | contribs) m v2.05 - Auto / Fix errors for CW project (Link equal to linktext) |
|||
(18 intermediate revisions by 15 users not shown) | |||
Line 1: | Line 1: | ||
{{Short description|Database command for removing data}} |
|||
In [[SQL]], the '''<code>TRUNCATE TABLE</code>''' statement is a [[ |
In [[SQL]], the '''<code>TRUNCATE TABLE</code>''' statement is a [[data manipulation language]] (DML)<ref>The [[Backus-Naur form|BNF]] for SQL:2023 defines TRUNCATE as an SQL data change statement: {{cite web |title=ISO/IEC 9075 BNF|url=https://standards.iso.org/iso-iec/9075/-2/ed-6/en/ISO_IEC_9075-2(E)_Foundation.bnf.txt|website=iso.org |access-date=2024-12-30 |ref=ISO/IEC 9075-2}}</ref> operation that deletes all rows of a table without causing a triggered action. The result of this operation quickly removes all data from a [[Table (database)|table]], typically bypassing a number of integrity enforcing mechanisms. It was officially introduced in the [[SQL:2008]] standard, as the optional feature F200, "TRUNCATE TABLE statement". |
||
== Behavior == |
|||
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 [[Data_definition_language#DROP_statement|DROP TABLE]] statement. |
|||
The <code>TRUNCATE TABLE mytable</code> statement is logically (though not physically) equivalent to the <code>[[Delete (SQL)|DELETE]] FROM mytable</code> statement (without a <code>[[Where (SQL)|WHERE]]</code> clause). The following characteristics distinguish <code>TRUNCATE TABLE</code> from <code>DELETE</code>: |
The <code>TRUNCATE TABLE mytable</code> statement is logically (though not physically) equivalent to the <code>[[Delete (SQL)|DELETE]] FROM mytable</code> statement (without a <code>[[Where (SQL)|WHERE]]</code> clause). The following characteristics distinguish <code>TRUNCATE TABLE</code> from <code>DELETE</code>: |
||
Line 5: | Line 10: | ||
* In the [[Oracle Database]], <code>TRUNCATE</code> is implicitly preceded and followed by a [[Commit (data management)|commit]] operation. (This may also be the case in [[MySQL]], when using a transactional storage engine.) |
* In the [[Oracle Database]], <code>TRUNCATE</code> is implicitly preceded and followed by a [[Commit (data management)|commit]] operation. (This may also be the case in [[MySQL]], when using a transactional storage engine.) |
||
* 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. |
||
* |
* It is not possible to specify a <code>WHERE</code> clause in a <code>TRUNCATE TABLE</code> statement. |
||
* <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 computer systems, <code>TRUNCATE TABLE</code> resets the count of an [[Identity column]] back to the identity's ''seed''. |
* In some computer 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.<ref>{{cite web |
|||
|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 |
|||
|publisher=Microsoft |
|||
|date=December 2005 |
|||
}}</ref> |
|||
== |
== DML/DDL == |
||
<references/> |
|||
The SQL standard classifies TRUNCATE as a ''data change statement'', synonymous with data manipulation (DML). This aligns with TRUNCATE being logically equivalent to an unconstrained DELETE operation. |
|||
However, some documents describe TRUNCATE as a [[data definition language]] (DDL) operation, because TRUNCATE may be seen as a combined DROP+CREATE operation<ref>For example, MySQL's documentation classifies TRUNCATE as a DDL statement: {{Cite web |title=MySQL :: MySQL 8.4 Reference Manual :: 15.1.37 TRUNCATE TABLE Statement |url=https://dev.mysql.com/doc/refman/8.4/en/truncate-table.html |access-date=2024-12-30 |website=dev.mysql.com}}</ref>. |
|||
== References == |
|||
{{Reflist}} |
|||
{{-}} |
{{-}} |
Latest revision as of 09:15, 8 January 2025
In SQL, the TRUNCATE TABLE
statement is a data manipulation language (DML)[1] operation that deletes all rows of a table without causing a triggered action. 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, as the optional feature F200, "TRUNCATE TABLE statement".
Behavior
[edit]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 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. - It is not possible to specify a
WHERE
clause in aTRUNCATE TABLE
statement. 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 computer systems,
TRUNCATE TABLE
resets the count of an Identity column back to the identity's seed.
DML/DDL
[edit]The SQL standard classifies TRUNCATE as a data change statement, synonymous with data manipulation (DML). This aligns with TRUNCATE being logically equivalent to an unconstrained DELETE operation.
However, some documents describe TRUNCATE as a data definition language (DDL) operation, because TRUNCATE may be seen as a combined DROP+CREATE operation[2].
References
[edit]- ^ The BNF for SQL:2023 defines TRUNCATE as an SQL data change statement: "ISO/IEC 9075 BNF". iso.org. Retrieved 2024-12-30.
- ^ For example, MySQL's documentation classifies TRUNCATE as a DDL statement: "MySQL :: MySQL 8.4 Reference Manual :: 15.1.37 TRUNCATE TABLE Statement". dev.mysql.com. Retrieved 2024-12-30.