SQL DROP DATABASE

SQL Tutorial

Here is a clear, safe, and practical guide to the SQL DROP DATABASE statement, including syntax for major database engines and important warnings.


⚠️ WARNING: What SQL DROP DATABASE Does

DROP DATABASE permanently deletes the entire database, including:

  • All tables

  • All data

  • All views, procedures, triggers

  • All associated files (depending on engine)

This action cannot be undone.
Always ensure you back up the database before dropping it.


✅ ANSI SQL Basic Syntax

DROP DATABASE database_name;

🟦 MySQL — DROP DATABASE

DROP DATABASE mydb;

Safe version:

DROP DATABASE IF EXISTS mydb;

Check existing databases:

SHOW DATABASES;

🟪 PostgreSQL — DROP DATABASE

DROP DATABASE mydb;

Safe version:

DROP DATABASE IF EXISTS mydb;

Important:

You cannot drop a database you are currently connected to.
Use:

\c postgres
DROP DATABASE mydb;

🟧 SQL Server — DROP DATABASE

Drop multiple databases:

If users are connected:


🟨 Oracle — DROP DATABASE

(Used only in administrative contexts.)

DROP DATABASE;

Typically run inside SQL*Plus as a privileged user.

Oracle note:
Dropping a database requires mounting it and executing specific admin commands — usually done by DBAs, not developers.


🔥 Safety Best Practices

Always back up before dropping
✔ Confirm the current database with a command like SELECT DATABASE(); (MySQL) or SELECT current_database(); (PostgreSQL)
✔ Use IF EXISTS when available
✔ Do NOT automate DROP commands in production scripts
✔ Verify environment variables (dev/stage/prod) before execution


📝 Example: Safe Procedure for MySQL

-- Check current database
SELECT DATABASE();
— Drop only if exists
DROP DATABASE IF EXISTS testdb;

📝 Example: Safe Procedure for PostgreSQL

-- Switch to a different database
\c postgres
— Drop safely
DROP DATABASE IF EXISTS testdb;

You may also like...