SQL TRUNCATE
TRUNCATE is a DDL (Data Definition Language) command used to remove all rows from a table quickly, while keeping the table structure (columns, constraints, etc.). It deletes the data but does not delete the table itself.
Why TRUNCATE TABLE is Considered DDL
• Operation Method: TRUNCATE typically deallocates entire data pages or extents used by the table, rather than deleting rows one by one.
• Minimal Logging: Because it deallocates data pages, it records minimal information in the transaction log
• Permissions: Executing TRUNCATE TABLE usually requires ALTER or DROP permissions on the table, which are DDL-related privileges, whereas DELETE only requires DELETE permission.
Important Points:
• Removes all records from the table
• Table structure remains
• Faster than DELETE
• Cannot use WHERE condition
• Resets auto-increment value
Basic Syntax
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE Orders; Now the table becomes empty, but the structure remains.
Short Definition
TRUNCATE is a DDL command used to delete all rows from a table while keeping the table structure intact.