SQL DROP DATABASE
DROP DATABASE removes a database and every table, view, function, and row inside it. No undo.
Basic form
SQL
DROP DATABASE shop;
If exists
SQL
DROP DATABASE IF EXISTS shop_test;
Common in test setup scripts — drop, recreate, then load fixtures.
What happens to active connections?
| DB | Behaviour |
|---|---|
| MySQL | Drops succeeds; queries on dropped DB return an error. |
| PostgreSQL | Fails if anyone is connected. Disconnect first or use WITH (FORCE) on PG 13+. |
| SQL Server | Fails if anyone is connected. ALTER DATABASE … SET SINGLE_USER first. |
Safer pattern in production
- Take a backup (
mysqldump,pg_dump, native backup). - Rename instead of drop:
ALTER DATABASE shop RENAME TO shop_retired_2026;(Postgres). - Wait a week. Then drop.
Tip: Treat
DROP DATABASE like rm -rf / — even with a backup, the recovery is painful. Run it manually, against the right environment, with someone reviewing your terminal.Example
Exercise
Drop a database safely only when it exists.
DROP DATABASE
EXISTS shop_test;
Two letters.
Discussion
Loading…