MySQL DROP DATABASE

The DROP DATABASE statement permanently deletes an entire database, including all tables and data stored inside it. This is a destructive operation and cannot be undone, so it must be used with caution.

Syntax

DROP DATABASE database_name;

Replace database_name with the name of the database to be deleted.

Example: Dropping a Database

The following statement deletes the database named school:

DROP DATABASE school;

MySQL responds with:

Query OK, 0 rows affected (0.05 sec)

The database school and everything inside it — all tables, rows, and data — is now permanently removed.

Verifying the Deletion

To confirm the database no longer exists, run:

SHOW DATABASES;

The deleted database will no longer appear in the list.

Dropping a Database Only if It Exists

If the database does not exist and the plain DROP DATABASE statement is used, MySQL throws an error:

ERROR 1008 (HY000): Can't drop database 'school'; database doesn't exist

To prevent this error, especially in scripts, use the IF EXISTS clause:

DROP DATABASE IF EXISTS school;

If the database exists, it is deleted. If it does not exist, MySQL skips the operation without an error.

Important Warning

Dropping a database is irreversible. All tables and all data inside those tables are permanently deleted. There is no undo. Before running this command:

  • Make sure a backup exists if the data might be needed later.
  • Double-check the database name to avoid deleting the wrong one.
  • Avoid running this command on a production (live) database without proper authorization.

Example Scenario

A developer created a test database while learning MySQL. After finishing, they want to clean it up:

-- Check what databases exist
SHOW DATABASES;

-- Remove the test database safely
DROP DATABASE IF EXISTS test_db;

-- Confirm it is gone
SHOW DATABASES;

Key Points

  • DROP DATABASE database_name; permanently deletes a database and all its contents.
  • Use IF EXISTS to avoid errors when the database may not exist.
  • This action cannot be reversed — always back up data before dropping a database.
  • Use SHOW DATABASES; to verify the database has been removed.

Leave a Comment

Your email address will not be published. Required fields are marked *