TRUNCATE

TRUNCATE is a Data Definition Language (DDL) command used in SQL to quickly remove all rows from a table. Unlike DELETE, it does not log individual row deletions, making it faster for large tables.

Key Points

  1. Purpose
    • Removes all data from a table.
    • Resets any auto-increment counters (depends on database system).
  2. Syntax
TRUNCATE TABLE table_name;
  1. Differences Between TRUNCATE and DELETE
FeatureTRUNCATEDELETE
LogsMinimalFull row-level logging
WHERE clauseNot allowedAllowed
SpeedFasterSlower for large tables
Trigger activationUsually does not fire triggersTriggers fire
  1. Use Cases
    • Clearing a table before loading new data.
    • Quickly removing all records without affecting table structure.
  2. Precautions
    • Cannot be rolled back in some databases (like MySQL with non-transactional tables).
    • Removes all rows, so be sure you don’t need the data.

Example

TRUNCATE TABLE employees;

This command removes all records from the employees table but keeps the table structure intact.

Home » Intermediate SQL for Data Professionals (SQL-201) > Data Manipulation > TRUNCATE