</>
Skip to content
SQL lessons (42/54)

SQL — Drop DB

Syntax

DROP DATABASE database_name;

Safe Version

Always use IF EXISTS to avoid errors:

DROP DATABASE IF EXISTS old_store;

What Happens When You Drop

  • All tables inside the database are deleted
  • All data is permanently lost
  • All indexes, views, and stored procedures are removed
  • The database no longer appears in SHOW DATABASES

Full Example

-- Check what exists
SHOW DATABASES;

-- Drop safely
DROP DATABASE IF EXISTS test_db;

-- Verify it's gone
SHOW DATABASES;

PostgreSQL

DROP DATABASE my_store;

-- Force drop (disconnects active connections first)
DROP DATABASE my_store WITH (FORCE);

Common Scenarios

Resetting a test database

DROP DATABASE IF EXISTS test_db;
CREATE DATABASE test_db;
USE test_db;
-- re-run migrations or seed scripts

Cleaning up after a failed migration

DROP DATABASE IF EXISTS staging_backup;

Cautions

  • There is no undo — once dropped, the data is gone
  • Always double-check the database name before executing
  • In production, take a backup before dropping
  • Some databases prevent dropping while connections are active

Best Practices

  • Never drop a production database without a backup
  • Use IF EXISTS in all scripts
  • Document which databases are safe to drop
  • Prefer DROP TABLE over DROP DATABASE if you only need to remove specific tables

Practice

  1. Create a temporary database, add a table, then drop it
  2. Use DROP DATABASE IF EXISTS in a script that creates a fresh test database

Related Topics

Frequently Asked Questions about Drop DB

What is Drop DB in SQL?

Drop DB is a fundamental concept in SQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Drop DB?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Drop DB.

Why is Drop DB important in SQL?

Drop DB is essential for SQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.