In the previous lesson, we learned how to create tables with CREATE TABLE. But in real database work, it is important not only to create structure, but also to understand how to clear tables or remove them completely. For that, SQL provides the TRUNCATE and DROP TABLE statements.
Both belong to Data Definition Language (DDL), but they solve different tasks. TRUNCATE quickly removes all rows from a table, while DROP TABLE removes the table itself together with its structure.
TRUNCATE DoesThe TRUNCATE statement removes all rows from a table, but the table itself remains.
After running TRUNCATE:
DELETE.TRUNCATE SyntaxTRUNCATE TABLE table_name;
TRUNCATE ExampleTRUNCATE TABLE logs;
After that, the logs table will still exist in the database, but all its rows will be removed.
DROP TABLE DoesThe DROP TABLE statement removes a table completely.
After running DROP TABLE:
DROP TABLE SyntaxDROP TABLE table_name;
DROP TABLE ExampleDROP TABLE old_reports;
After that, the old_reports table will be fully removed from the database.
TRUNCATE Differs from DROP TABLEAlthough both statements remove data, there is a fundamental difference between them.
TRUNCATE removes only rows.DROP TABLE removes both the rows and the table itself.TRUNCATE, the table remains and new data can be inserted into it again.DROP TABLE, the table is gone, and it must be recreated before it can be used again.TRUNCATE is suitable when you need to quickly clear a table while keeping its structure.DROP TABLE is suitable when the table is no longer needed at all.TRUNCATE Differs from DELETEBeginners often compare TRUNCATE with DELETE, because both commands can remove data from a table.
The main differences are:
DELETE can be used with WHERE to remove only part of the rows;TRUNCATE removes all rows from the table at once;DELETE belongs to DML, while TRUNCATE is usually treated as DDL;TRUNCATE is often faster in many DBMSs when the whole table must be cleared;If you need to remove only part of the data, DELETE is usually the right choice. If you need to quickly clear the whole table but keep its structure, TRUNCATE is often more convenient.
When using TRUNCATE and DROP TABLE, it is important to remember a few rules:
DROP TABLE if the table structure will still be needed;DELETE with TRUNCATE if you need to remove only part of the rows;TRUNCATE cannot be used if the table is referenced by foreign keys;TRUNCATE behavior and rollback support depend on the specific DBMS.Using these statements incorrectly can lead to loss of data or table structure.
Imagine that we have a helper table called daily_import into which intermediate data from an external system is loaded every day.
If the table is used regularly but must be completely cleared before each new load, TRUNCATE is appropriate:
TRUNCATE TABLE daily_import;
After that, the table structure remains, and new data can be loaded into it again.
If the table was created only for a one-time task and is no longer needed, it can be removed completely:
DROP TABLE daily_import;
In the first case, we prepare the existing table for reuse. In the second, we completely remove an object that is no longer needed from the database.
Key takeaways from this lesson:
TRUNCATE removes all rows from a table but keeps its structure.DROP TABLE removes both the data and the table itself.TRUNCATE and DELETE solve similar problems, but they work differently.TRUNCATE and DROP TABLE can differ slightly across DBMSs.In the next lesson, we will look at temporary tables and understand when they are convenient for intermediate results.