The DROP DATABASE statement is used to completely remove a database from a MySQL server. When a database is dropped, the database and the objects stored inside it, such as tables, are removed.
DROP DATABASE is used to remove an existing database from MySQL.
DROP DATABASE school;
This removes the school database and the objects stored inside it.
The basic syntax is:
DROP DATABASE database_name;
Replace database_name with the database you want to remove.
Suppose a database named school exists.
DROP DATABASE school;
The school database will be removed from the MySQL server.
A database can contain many tables.
school
|
├── students
├── teachers
├── courses
└── fees
When the school database is dropped, these tables are also removed as part of the database.
DROP DATABASE should be treated as a destructive operation.
Before using it, make sure that the database is no longer required or that you have an appropriate backup.
Use SHOW DATABASES before dropping a database.
SHOW DATABASES;
This helps you verify the database name before performing the destructive operation.
You can use IF EXISTS to avoid an error if the database does not exist.
DROP DATABASE IF EXISTS school;
If the database exists, it is removed. If it does not exist, MySQL does not report the usual missing-database error for this operation.
IF EXISTS is useful in scripts that may be executed multiple times.
DROP DATABASE IF EXISTS test_db;
This makes the script more convenient when the database may or may not already exist.
After dropping a database, use SHOW DATABASES to check the available databases.
DROP DATABASE school;
SHOW DATABASES;
The dropped database should no longer appear in the accessible database list.
USE selects a database, while DROP DATABASE removes it.
USE school;
DROP DATABASE school;
Once the database is dropped, it no longer exists as a database on the server.
Even if a database does not contain user tables, it can be removed with DROP DATABASE.
CREATE DATABASE test_db;
DROP DATABASE test_db;
A database can contain multiple tables when it is dropped.
CREATE DATABASE school;
USE school;
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100)
);
DROP DATABASE school;
The database and its table are removed together.
You can execute DROP DATABASE in the MySQL Workbench SQL Editor.
DROP DATABASE school;
After execution, refresh the Schemas panel to update the displayed database list.
The same SQL statement can be executed from the MySQL command-line client.
mysql> DROP DATABASE school;
The command is sent to the MySQL server for execution.
| Command | What It Removes |
|---|---|
| DROP DATABASE | The database and its objects |
| DROP TABLE | A specific table |
| Command | Purpose |
|---|---|
| DROP DATABASE | Removes a database and its objects |
| DELETE | Removes selected rows from a table |
These commands operate at very different levels.
| Command | Purpose |
|---|---|
| DROP DATABASE | Removes the database and its objects |
| TRUNCATE TABLE | Removes rows from a specific table while keeping the table structure |
A MySQL account needs appropriate privileges to drop a database.
If the account does not have sufficient permission, MySQL can reject the operation.
Before using DROP DATABASE, verify the exact database name.
SHOW DATABASES;
For example, if the list contains:
school_db
library_db
company_db
make sure you drop the intended database and not another project database.
Development scripts may use:
DROP DATABASE IF EXISTS test_db;
CREATE DATABASE test_db;
This pattern can be useful for recreating a temporary development database.
During development, you may sometimes need to remove and recreate a test database.
DROP DATABASE IF EXISTS test_db;
CREATE DATABASE test_db;
USE test_db;
This creates a fresh database for testing.
DROP DATABASE can be useful in controlled development environments when you need to reset a temporary database.
DROP DATABASE IF EXISTS demo_db;
Do not use this approach on important production databases without a proper operational procedure.
Production databases often contain important application data.
Before dropping any production database, verify:
A backup can provide a recovery option before destructive database operations.
For important databases, follow the backup and recovery procedures appropriate for your environment before dropping the database.
Common issues include:
SHOW DATABASES;
DROP DATABASE IF EXISTS test_db;
SHOW DATABASES;
The first SHOW DATABASES helps identify available databases, while the second lets you verify the resulting database list.
A temporary development database can be recreated using:
DROP DATABASE IF EXISTS school_test;
CREATE DATABASE school_test;
USE school_test;
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100)
);
Before executing DROP DATABASE, ask:
CREATE DATABASE IF NOT EXISTS demo_db;
USE demo_db;
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100)
);
SHOW DATABASES;
DROP DATABASE IF EXISTS demo_db;
SHOW DATABASES;
This example creates a temporary database, creates a table, verifies the database list, removes the database, and checks the database list again.
Suppose a temporary database called training_db is no longer required.
SHOW DATABASES;
DROP DATABASE IF EXISTS training_db;
SHOW DATABASES;
The IF EXISTS option makes the command suitable for scripts where the database may already have been removed.
Question: Which statement is used to completely remove a database from MySQL?