Before continuing: This entry is part of the deleting data guide. If you are new to the topic, start with DELETE.
DROP TABLE example
We create a temporary drafts table, drop it and confirm it no longer appears in the SQLite catalogue:
CREATE TABLE drafts (
id INTEGER PRIMARY KEY,
text_value TEXT
);
DROP TABLE drafts;
SELECT name
FROM sqlite_schema
WHERE type = 'table' AND name = 'drafts';The final query returns no rows. That confirms the object no longer exists; it does not mean the table is empty.
Check the result with small data
We create a throwaway table and drop it in the same test database.
CREATE TABLE demo_temporary (id INTEGER PRIMARY KEY);
DROP TABLE demo_temporary;
SELECT COUNT(*) AS remaining_tables
FROM sqlite_schema WHERE name = 'demo_temporary' AND type='table';| remaining_tables |
|---|
| 0 |
A count of zero shows that it no longer exists. DROP TABLE removes the structure along with the data, so take special care on production databases.
Check your understanding: What is the difference between DROP TABLE and DELETE FROM?
Show answer
DROP TABLE removes the whole table: its structure and its data. DELETE FROM removes rows and leaves the table, with its columns and constraints, ready for new data.
How DROP TABLE is used
IF EXISTS avoids an error when the table is not there, although some engines issue a warning. It does not make the operation safe: if the table exists, it is dropped.
Inspect before you delete. Confirm the environment, the schema, the dependencies and the backup. In production, a correct statement can still be the wrong decision.
What changes from case to case
DELETEremoves rows and acceptsWHERE.TRUNCATEempties the table in an engine-specific way and keeps its structure.DROP TABLEremoves the entire structure.
Common mix-ups with DROP TABLE
Using CASCADE without checking dependencies
CASCADE can remove dependent objects as well as the table. Check first what will disappear and use the most restrictive option that still does the job.