MySQL Drop Database and Table

DROP permanently removes an entire database or table, including all of its data and structure, and cannot be undone without a backup.

DROP DATABASE

The DROP DATABASE statement deletes an entire database, including every table, view, stored procedure, and trigger it contains. This is one of the most destructive commands in SQL — there is no built-in undo. Once executed successfully, the database and all associated files are removed from disk immediately.

Drop a database

DROP DATABASE test_shop;

DROP TABLE

DROP TABLE removes a single table's structure and all of its rows. Unlike DELETE or TRUNCATE, which only remove data, DROP TABLE also erases the table definition itself — columns, indexes, constraints, and all. Any foreign keys referencing the dropped table must be handled first, or the statement will fail.

Drop a single table

DROP TABLE order_items;

Safer Dropping with IF EXISTS

Running DROP TABLE or DROP DATABASE on an object that does not exist raises an error, which can halt a script. Adding the IF EXISTS clause makes the statement a no-op instead of an error when the target is missing — this is especially useful in setup or teardown scripts that need to run repeatedly and idempotently.

Drop multiple tables safely

DROP TABLE IF EXISTS invoices, invoice_items;
  • DROP DATABASE removes the entire schema and everything inside it
  • DROP TABLE removes a table's structure and data, but leaves the database intact
  • IF EXISTS prevents an error when the target does not exist
  • DROP TABLE can accept a comma-separated list of table names in one statement
  • Foreign key relationships must be dropped or disabled before dropping a referenced table
StatementWhat it removesReversible?
DELETE FROM tableRows only (structure stays)Yes, via transaction rollback before commit
TRUNCATE TABLEAll rows, resets auto-incrementNo
DROP TABLERows and table structureNo
DROP DATABASEEvery table, view, and routine in the schemaNo
Note: DROP DATABASE and DROP TABLE execute instantly and cannot be rolled back once the storage engine commits the change, even inside a transaction in most configurations. Always take a verified backup before running either command in a production environment.
Note: Before dropping anything in a shared environment, run SHOW TABLES or SHOW CREATE TABLE first to confirm you are targeting the exact object you intend to remove — typos in table names are a common and costly mistake.

Exercise: MySQL Drop Database and Table

What happens to a table's structure when DROP TABLE runs?