SQL Tutorial › Database Design › DROP TABLE & DATABASE

SQL DROP TABLE and DROP DATABASE

BeginnerLesson 58 of 697 min read7 queries run on MySQL 8.4

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.

Advertisement
In this lesson
  • 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

MySQL
CREATE TABLE temp_report (id INT, total INT);SHOW TABLES LIKE 'temp%';DROP TABLE temp_report;SHOW TABLES LIKE 'temp%';
ResultMySQL 8.4
mysql> CREATE TABLE temp_report (id INT, total INT)
Query OK, 0 rows affected
mysql> SHOW TABLES LIKE 'temp%'
Tables_in_company (temp%)
temp_report
1 row in set
mysql> DROP TABLE temp_report
Query OK, 0 rows affected
mysql> SHOW TABLES LIKE 'temp%'
Empty set
How this query works, step by step
  1. The table is created and SHOW TABLES lists it.
  2. DROP TABLE removes the table and all its rows.
  3. The second SHOW TABLES returns an Empty set.
Advertisement

IF EXISTS

Dropping a table that does not exist is an error:

MySQL
DROP TABLE no_such_table;
ResultMySQL 8.4
ERROR 1051 (42S02): Unknown table 'company.no_such_table'

IF EXISTS turns it into a note, so setup scripts can run again and again:

MySQL
DROP TABLE IF EXISTS no_such_table;SHOW WARNINGS;
ResultMySQL 8.4
mysql> DROP TABLE IF EXISTS no_such_table
Query OK, 0 rows affected, 1 warning
mysql> SHOW WARNINGS
LevelCodeMessage
Note1051Unknown table 'company.no_such_table'
1 row in set

Several tables at once

MySQL
CREATE TABLE t1 (a INT);CREATE TABLE t2 (a INT);DROP TABLE IF EXISTS t1, t2;SHOW TABLES LIKE 't_';
ResultMySQL 8.4
mysql> CREATE TABLE t1 (a INT)
Query OK, 0 rows affected
mysql> CREATE TABLE t2 (a INT)
Query OK, 0 rows affected
mysql> DROP TABLE IF EXISTS t1, t2
Query OK, 0 rows affected
mysql> SHOW TABLES LIKE 't_'
Empty set
Advertisement

Tables referenced by foreign keys

A parent table cannot be dropped while a child table references it:

MySQL
DROP TABLE customers;
ResultMySQL 8.4
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:

MySQL
DROP TABLE order_items;DROP TABLE orders;DROP TABLE customers;SHOW TABLES;
ResultMySQL 8.4
mysql> DROP TABLE order_items
Query OK, 0 rows affected
mysql> DROP TABLE orders
Query OK, 0 rows affected
mysql> DROP TABLE customers
Query OK, 0 rows affected
mysql> SHOW TABLES
Tables_in_company
departments
employees
products
3 rows in set
How this query works, step by step
  1. order_items references orders, and orders references customers.
  2. Dropping in child-to-parent order satisfies every foreign key.
  3. The three tables are gone; departments, employees and products remain.
Watch out

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

MySQL
CREATE DATABASE scratch_db;DROP DATABASE scratch_db;DROP DATABASE IF EXISTS scratch_db;SHOW DATABASES;
ResultMySQL 8.4
mysql> CREATE DATABASE scratch_db
Query OK, 1 row affected
mysql> DROP DATABASE scratch_db
Query OK, 0 rows affected
mysql> DROP DATABASE IF EXISTS scratch_db
Query OK, 0 rows affected, 1 warning
mysql> SHOW DATABASES
Database
information_schema
mysql
performance_schema
sys
4 rows in set
How this query works, step by step
  1. DROP DATABASE deletes the database and every table in it: Query OK, 0 rows affected (the count is the number of tables dropped).
  2. Running it again with IF EXISTS produces only a warning.
Advertisement

DROP vs TRUNCATE vs DELETE

DROP TABLETRUNCATE TABLEDELETE FROM
Removes datayesyes, all rowschosen rows
Removes structureyesnono
Can use WHEREnonoyes
Can be rolled backnonoyes
Fires DELETE triggersnonoyes

Safety checklist

  1. Run SELECT DATABASE(); to be sure which database you are in.
  2. Take a backup (mysqldump) of anything you might need.
  3. Prefer renaming (RENAME TABLE orders TO orders_old_2026) and dropping later, once nobody has complained.
  4. In production, grant the DROP privilege only to the accounts that need it (GRANT).

Quick quiz

1. What does DROP TABLE remove?

2. Why use DROP TABLE IF EXISTS in scripts?

3. In which order must you drop orders and customers?

Advertisement

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;.