DROP DATABASE deletes a database and every table, row, view and stored program inside it. There is no recycle bin and no confirmation.
CREATE DATABASE old_project;
CREATE TABLE old_project.users (id INT);
SHOW DATABASES LIKE 'old_project';
DROP DATABASE old_project;
SHOW DATABASES LIKE 'old_project';
+------------------------+
| Database (old_project) |
+------------------------+
| old_project |
+------------------------+
1 row in set
Empty set
The first SHOW lists old_project. After the drop, it returns nothing.
IF EXISTS
Dropping something that is not there is an error, unless you add IF EXISTS:
DROP DATABASE missing_db;
ERROR 1008 (HY000): Can't drop database 'missing_db'; database doesn't exist
DROP DATABASE IF EXISTS missing_db;
SHOW WARNINGS;
Empty set
IF EXISTS makes scripts that reset a test environment safe to repeat.
What is deleted
- Every table and its data
- Views, stored procedures, functions, triggers and events in that database
- The folder on disk
What is not deleted: user accounts, and privileges are not removed automatically (grants that name the database stay until you revoke them).
It cannot be rolled back
DROP DATABASE is DDL, and DDL commits at once (DDL, DML, DQL, DCL and TCL). The only way back is a backup (Backup and Restore).
A safety checklist
- Check the name.
SELECT DATABASE(); - Look at what is inside.
SHOW TABLES FROM name; - Take a backup. Even if you “know” you do not need it.
- Use accounts with least privilege. Your web application’s database user must not have
DROPrights. - Do not run it in production by hand without a second person checking the name.
CREATE DATABASE temp_check;
CREATE TABLE temp_check.t (id INT);
SELECT table_name FROM information_schema.tables WHERE table_schema = 'temp_check';
DROP DATABASE IF EXISTS temp_check;
+------------+
| TABLE_NAME |
+------------+
| t |
+------------+
1 row in set
Typing the wrong name
DROP DATABASE shop; and DROP DATABASE shop_backup; look almost the same. Many production incidents begin with a tired person and a similar name. In real systems, restrict DROP to a few accounts and require a backup first.