SQL TRUNCATE Table

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

The TRUNCATE TABLE command in SQL is a Data Definition Language (DDL) operation used to delete all records from a table swiftly. It functions similarly to the DELETE command without a WHERE clause. The SQL TRUNCATE TABLE statement efficiently clears all records from a table in a single operation, providing a faster alternative to deleting records individually. Unlike the DROP TABLE command, the truncate in SQL effectively resets the table without removing its structure, which deletes the entire table structure. Learn more about this statement in our comprehensive guide.

TRUNCATE in SQL Syntax

The syntax of the truncate statement is:

Example

Let us consider we have a database(ABC) in which a Customers tables is present. The Customers table has the following five attributes:

  1. Customer ID
  2. Customer Name
  3. Phone Number
  4. Email Address
  5. Item Id
PROPERTYATTRIBUTE NAMEDATA TYPE
PRIMARYCUSTOMER_IDINT
CUSTOMER_NAMEVARCHAR
PHONE_NUMBERVARCHAR
ITEM_IDINT
EMAIL_ADDRESSVARCHAR

Lets insert some values in our Customer table.

Now the table will look like this -

CUSTOMER_IDCUSTOMER_NAMEPHONE_NUMBERITEM_IDEMAIL_ADDRESS
1Ram98452852822101ram12@gmail.com
2Kumar9934342422102kumar33@gmail.com
3Gaurav9434239856103gaurav22@gmail.com
4Shyam9999786756104shyamji@gmail.com

Now, we have to truncate the Customers table.

Output:

We are deleting the customer's table records using the truncate statement. Now, the table will be empty. This is because the truncate statement has deleted the data.

TRUNCATE vs DELETE in SQL

DELETETRUNCATE
Delete statement is a DML command.Truncate in sql is a DDL command.
Delete statement deletes all the rows specified using the WHERE clause.The Truncate statement removes all the records from the table.
It is slower than the TRUNCATE command.It is faster than the DELETE command.
It removes one record at a time.Truncate in sql removes all rows in a table by deallocating the pages used to store the table data.
Manual COMMIT is required after executing DELETE command to commit modifications.The TRUNCATE command automatically commits modifications to the table.
Each transaction is logged individually for record-keeping purposes.The only activity recorded is deallocating data storage pages.
It consumes a greater amount of transaction space than the TRUNCATE command.The TRUNCATE command is more efficient in transaction space usage than the DELETE command.

TRUNCATE vs DROP

While both TRUNCATE in sql and DROP commands are categorized under Data Definition Language (DDL) operations and affect the definitions of database objects, they have distinct functionalities. TRUNCATE removes all rows from a table, effectively resetting its data content while keeping the table structure intact. On the other hand, DROP completely removes the table from the database, freeing up the associated memory space. Notably, both commands automatically commit the changes upon execution, making them irreversible without the option to roll back.

DROPTRUNCATE
The DROP statement removes the table contents and its structure from the database.The truncate in sql statement removes all the records from the table.
The DROP statement deletes/invalidates the view associated with that table.No change occurs on the existing views.
This command is quicker to perform.Truncate statement is faster than DROP statement.
It is slower than Truncate but faster than the Delete commandIt is faster than both Delete and Drop commands.

Conclusion

In this article, we explored the usage of the TRUNCATE statement. The truncate command removes table records.

  • The truncate in sql statement cannot be used to remove the records when a Foreign key constraint references a table.
  • If we are dealing with large tables, we can use Microsoft SQL Server, which can easily manage the large tables.
  • A truncate statement removes the table's records, not the table structure.