SQL DROP TABLE and DROP DATABASE
DROP removes a database object completely: the table’s structure, data, indexes and triggers, or a whole database with every table in it. It is instant and cannot be undone with ROLLBACK, so it is the one command where a typo hurts most. This lesson shows how to drop safely.
- Remove a table with
DROP TABLE - Make scripts re-runnable with
IF EXISTS - Drop tables that are referenced by foreign keys
- Drop a database, and compare DROP, TRUNCATE and DELETE
DROP TABLE
CREATE TABLE temp_report (id INT, total INT);SHOW TABLES LIKE 'temp%';DROP TABLE temp_report;SHOW TABLES LIKE 'temp%';| Tables_in_company (temp%) |
|---|
| temp_report |
- The table is created and
SHOW TABLESlists it. DROP TABLEremoves the table and all its rows.- The second
SHOW TABLESreturns an Empty set.
IF EXISTS
Dropping a table that does not exist is an error:
DROP TABLE no_such_table;ERROR 1051 (42S02): Unknown table 'company.no_such_table'
IF EXISTS turns it into a note, so setup scripts can run again and again:
DROP TABLE IF EXISTS no_such_table;SHOW WARNINGS;| Level | Code | Message |
|---|---|---|
| Note | 1051 | Unknown table 'company.no_such_table' |
Several tables at once
CREATE TABLE t1 (a INT);CREATE TABLE t2 (a INT);DROP TABLE IF EXISTS t1, t2;SHOW TABLES LIKE 't_';Tables referenced by foreign keys
A parent table cannot be dropped while a child table references it:
DROP TABLE customers;ERROR 3730 (HY000): Cannot drop table 'customers' referenced by a foreign key constraint 'orders_ibfk_1' on table 'orders'.
Drop the child first, then the parent:
DROP TABLE order_items;DROP TABLE orders;DROP TABLE customers;SHOW TABLES;| Tables_in_company |
|---|
| departments |
| employees |
| products |
order_itemsreferencesorders, andordersreferencescustomers.- Dropping in child-to-parent order satisfies every foreign key.
- The three tables are gone;
departments,employeesandproductsremain.
You may see scripts that run SET FOREIGN_KEY_CHECKS = 0; to drop tables in any order. That also lets broken data in, so use it only in throw-away development databases and switch it back on immediately.
DROP DATABASE
CREATE DATABASE scratch_db;DROP DATABASE scratch_db;DROP DATABASE IF EXISTS scratch_db;SHOW DATABASES;| Database |
|---|
| information_schema |
| mysql |
| performance_schema |
| sys |
DROP DATABASEdeletes the database and every table in it:Query OK, 0 rows affected(the count is the number of tables dropped).- Running it again with
IF EXISTSproduces only a warning.
DROP vs TRUNCATE vs DELETE
| DROP TABLE | TRUNCATE TABLE | DELETE FROM | |
|---|---|---|---|
| Removes data | yes | yes, all rows | chosen rows |
| Removes structure | yes | no | no |
| Can use WHERE | no | no | yes |
| Can be rolled back | no | no | yes |
| Fires DELETE triggers | no | no | yes |
Safety checklist
- Run
SELECT DATABASE();to be sure which database you are in. - Take a backup (
mysqldump) of anything you might need. - Prefer renaming (
RENAME TABLE orders TO orders_old_2026) and dropping later, once nobody has complained. - In production, grant the
DROPprivilege only to the accounts that need it (GRANT).
Quick quiz
1. What does DROP TABLE remove?
Nothing of the table remains.
2. Why use DROP TABLE IF EXISTS in scripts?
A missing table becomes a warning instead of an error.
3. In which order must you drop orders and customers?
The child table must go before the parent it references.
DROP FAQ
What is the difference between DROP and TRUNCATE?
DROP TABLE deletes the table completely, structure included. TRUNCATE TABLE deletes all rows but keeps the empty table.
Can I undo DROP TABLE?
Not with ROLLBACK. Restore from a backup or point-in-time recovery using the binary log.
How do I drop a table with a foreign key?
Drop the referencing (child) tables first, or remove the foreign key with ALTER TABLE child DROP FOREIGN KEY name;.