ERROR 1051 (42S02): Unknown table 'schema.tablename' is raised when a DROP TABLE statement references a table that does not exist in the specified database. The error symbol is ER_BAD_TABLE_ERROR.
Impact
The DROP TABLE statement fails immediately and the named table is not dropped. In a single-statement context this is straightforward — the statement errors out and nothing changes. However, in a migration script or stored procedure that drops multiple tables in one statement (e.g., DROP TABLE a, b, c), MySQL's behavior changed in 5.7: if any table in the list does not exist, the entire statement fails and none of the tables are dropped.
Developers most commonly encounter this error during database migrations (particularly when running down-migrations or rollbacks against a database that was already partially migrated), when executing schema setup scripts on a fresh or partially initialised schema, or when an application issues DROP TABLE in cleanup code that may run more than once.
Common Causes
Running a migration or script twice. A migration that drops a table succeeds on first run, but if it is re-run (e.g., due to a deployment retry or manual re-execution), the table is already gone and MySQL raises 1051.
Down-migration against a partially migrated database. When rolling back only some migrations in a sequence, a later down-migration may try to drop a table that a corresponding up-migration never created.
Wrong database selected. The connection is pointing at a different schema than expected. The table exists in another database, but the current
USEcontext or the fully-qualified table name in the statement does not match.Typo or case mismatch in the table name. On case-sensitive filesystems (Linux),
Ordersandordersare distinct tables.DROP TABLE Orderswill fail with 1051 if the table was created asorders.DROP TABLE in a multi-table statement with one missing table.
DROP TABLE existing_table, missing_tablefails entirely — neither table is dropped — even though one of them exists.
Troubleshooting and Resolution Steps
Verify the table exists in the current database.
SELECT TABLE_SCHEMA, TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = 'tablename';If the table appears under a different schema, either switch databases with
USEor use a fully-qualified name.Check the current database context.
SELECT DATABASE();Confirm this is the schema you intended to operate on.
Use
IF EXISTSto makeDROP TABLEidempotent.This is the canonical fix for migrations and scripts that may run more than once:
DROP TABLE IF EXISTS tablename;With
IF EXISTS, MySQL silently skips the drop and emits aNote(not an error) when the table is absent. This is the recommended pattern for all migration down-scripts and schema teardown scripts.Use
IF EXISTSwith multi-table drops.DROP TABLE IF EXISTS table_a, table_b, table_c;Each table name is evaluated independently — existing tables are dropped and missing ones are skipped, with one
Notegenerated per missing table.Check for case sensitivity issues.
On Linux MySQL instances, table names are case-sensitive by default (
lower_case_table_names = 0). Verify the exact name:SHOW TABLES LIKE 'tablename';On macOS and Windows,
lower_case_table_namesdefaults to 1 or 2, making names case-insensitive. Production Linux servers with the default value 0 can behave differently from development machines.Inspect notes after
IF EXISTS.After a
DROP TABLE IF EXISTSthat skips a missing table, you can retrieve the note:SHOW WARNINGS; -- Note 1051 Unknown table 'mydb.tablename'Migration frameworks may surface this note as a warning; it is safe to ignore.
Additional Information
ER_BAD_TABLE_ERROR(1051) shares SQLSTATE42S02withER_NO_SUCH_TABLE(1146). Error 1146 is raised bySELECT,INSERT,UPDATE, andDELETEstatements; error 1051 is specific toDROP TABLEandDROP VIEWfor views.DROP VIEWraises a different error (1347ER_WRONG_OBJECT) when the named object exists but is a table, not a view — and raises 1051 when it does not exist at all.- ORM frameworks such as Rails ActiveRecord, Flyway, and Liquibase generate
DROP TABLE IF EXISTSby default in down-migrations. If you are writing raw SQL migrations, adopt the same convention. - In MySQL 5.6 and earlier,
DROP TABLEwith a missing table in a multi-table list would drop the tables that did exist and only warn about the missing one. This behavior changed in 5.7 — the whole statement now fails if strict handling is in effect. Always useIF EXISTSto write version-portable migration scripts. - The
lower_case_table_namessystem variable controls name case sensitivity and is set at server startup; it cannot be changed at runtime.
Frequently Asked Questions
Does DROP TABLE IF EXISTS generate any output when the table is missing?
Yes — MySQL generates a Note (severity level below Warning), not an error. The statement succeeds and the note can be retrieved with SHOW WARNINGS. Application code and migration tools typically suppress notes, so in practice the operation appears silent.
Why did my migration fail with 1051 even though the table used to exist?
Most likely the migration was already run (or partially run) before. The table was dropped on the first run; the second run encountered the missing table and failed. Add IF EXISTS to the DROP TABLE statement so the migration is idempotent and safe to run multiple times.
I have DROP TABLE a, b, c and only b is missing. Why did a and c not get dropped?
Without IF EXISTS, a single missing table in a multi-table DROP TABLE causes the entire statement to fail in MySQL 5.7+. Use DROP TABLE IF EXISTS a, b, c to drop all tables that exist and silently skip any that do not.
How is error 1051 different from error 1146?
Both use SQLSTATE 42S02 and both signal a missing table. Error 1051 is raised only by DROP TABLE (and DROP VIEW); error 1146 (ER_NO_SUCH_TABLE) is raised by DML and SELECT statements that reference a non-existent table.