SQL Tutorial
The TRUNCATE TABLE
command in SQL is used to delete all rows from a table. It's similar to the DELETE
command without a WHERE
clause, but TRUNCATE TABLE
is faster and uses fewer system and transaction log resources.
The syntax for the TRUNCATE TABLE
command is quite simple:
TRUNCATE TABLE table_name;
So, for example, if you have a table named orders
, you would truncate it like this:
TRUNCATE TABLE orders;
This would delete all rows from the orders
table.
Here are some important points to remember about the TRUNCATE TABLE
command:
It cannot be used on tables that are referenced by a FOREIGN KEY constraint. You must remove the FOREIGN KEY constraint before truncating the table.
It is a DDL (Data Definition Language) command, not a DML (Data Manipulation Language) command. This means it's a structure-level command like CREATE
, ALTER
, and DROP
. Because of this, it does not generate individual row delete statements and does not log individual row deletes.
It is faster and uses fewer system resources than the DELETE
statement, which is why it's often used when you want to delete all rows from a table.
It removes the data 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 means that TRUNCATE TABLE
cannot be used to delete specific rows, while leaving others.
It cannot be used with indexed views.
Rollback is not available. Since TRUNCATE
is a DDL command, it doesn't have the ability to rollback any changes as DML commands do with transactions.
It resets the identity seed. If the table contains an identity column, the counter for that column is reset to the seed value defined for the column. If no seed was defined, the default value 1 is used.
Please be aware that the TRUNCATE TABLE
command permanently removes all records from a table. It's a very powerful command and should be used with caution.
Emptying a Table in SQL Using TRUNCATE:
TRUNCATE TABLE
is used to quickly remove all rows from a table.TRUNCATE TABLE example_table;
TRUNCATE TABLE vs DELETE in SQL:
TRUNCATE TABLE
is faster than DELETE
as it deallocates data pages, while DELETE
removes rows one by one.DELETE FROM example_table;
How to Remove All Data from a Table in SQL:
TRUNCATE TABLE
or DELETE
to remove all data from a table.TRUNCATE TABLE example_table;
DELETE FROM example_table;
SQL TRUNCATE TABLE Example:
TRUNCATE TABLE employees;
Difference Between TRUNCATE TABLE and DROP TABLE:
TRUNCATE TABLE
removes all rows but retains the table structure, while DROP TABLE
removes the entire table.DROP TABLE example_table;
Truncating a Table in [Your Database System]:
TRUNCATE TABLE example_table;
TRUNCATE TABLE example_table;
TRUNCATE TABLE example_table;
TRUNCATE TABLE example_table;
SQL TRUNCATE TABLE and Foreign Key Constraints:
TRUNCATE TABLE
is typically faster than DELETE
even when foreign key constraints exist.TRUNCATE TABLE parent_table;
Truncate Table Command in [Your Database System]:
TRUNCATE TABLE example_table;
TRUNCATE TABLE example_table;
TRUNCATE TABLE example_table;
TRUNCATE TABLE example_table;