Grokking SQL for Tech Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
TRUNCATE
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

TRUNCATE

In SQL, the TRUNCATE statement removes all the rows and columns from a table but retains the table structure. It doesn't delete the entire table like the DROP command.

If you want to make the table empty, you can use truncate. Here's the syntax:

TRUNCATE TABLE table_name;

DROP vs TRUNCATE

The choice between DROP and TRUNCATE depends on the specific requirements and the level of removal needed for the task at hand.

But let's discuss the differences so you can identify which one you should use according to your use case.

AspectTruncateDrop
DefinitionRemoves all rows from a table but retains the table structure.Removes the entire table along with its structure and data.
OperationCan be rolled back (if supported by the database) to restore the truncated data.Irreversible operation. Once dropped, the table and its data are permanently deleted.
PerformanceGenerally faster than drop because it only needs to deal with data, not the entire table structure.Can be slower compared to truncate as it involves deleting the table and its structure.
UsageCommonly used when you want to quickly delete all data from a table and keep the table structure.Used when you want to completely remove a table, including its structure and associated objects.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible