ERROR 1146 (42S02): Table 'database.table_name' doesn't exist

MySQL Error 1146 After Rename: Table 'X' Doesn't Exist

MySQL throws 1146 after a RENAME TABLE if the new name is referenced before the transaction commits, or if case-sensitivity trips you up on Linux. Here's exactly what to check in order.

You run RENAME TABLE orders TO orders_old, then immediately query orders_old and get ERROR 1146 (42S02): Table 'mydb.orders_old' doesn't exist. The screen stares back. You check SHOW TABLES — the table is there. What's actually happening here is almost always one of three things, and you can fix it in under a minute once you know which.

1. The rename is inside a transaction that hasn't committed

This is the most common cause I see in production. RENAME TABLE in MySQL (InnoDB) is not like a DROP or ALTER — it's transaction-safe. If you run it inside an open transaction and don't commit, the rename is visible only to your session. Other connections still see the old name, and your own session sees the new name immediately. But if you then start a new query in a different session or reconnect, you'll get 1146 because the rename never committed.

-- Session A
START TRANSACTION;
RENAME TABLE mydb.orders TO mydb.orders_old;
SELECT * FROM mydb.orders_old;  -- works fine in this session

-- Session B (different connection)
SELECT * FROM mydb.orders_old;  -- ERROR 1146: Table doesn't exist

The fix: Check if you're in an active transaction with SELECT @@autocommit. If it's 0, you're in transaction mode. Run COMMIT; after the rename. Better yet, wrap atomic renames in START TRANSACTION / COMMIT explicitly — but don't forget the commit.

COMMIT;
-- or if you want to roll back:
ROLLBACK;

One more gotcha: If you're using a framework like Laravel or Django that wraps queries in implicit transactions, a RENAME inside a migration might not commit until the migration finishes. If the migration errors out, the rename never commits.

2. Case-sensitivity mismatch on Linux with lower_case_table_names

This one burned me on a Ubuntu 20.04 server. MySQL on Linux is case-sensitive for table names by default — orders and Orders are different tables. But many apps are developed on macOS or Windows, where lower_case_table_names=1 is the default and everything is folded to lowercase.

Here's the scenario: You rename orders to Orders_archive via a GUI tool that sends RENAME TABLE orders TO Orders_archive. MySQL stores the table as Orders_archive. Your app queries orders_archive (all lowercase). MySQL looks for orders_archive, finds a file Orders_archive.ibd (different case), and throws 1146.

The fix is not to blindly set lower_case_table_names=1 on an existing database — that can break existing references because MySQL caches table names. Instead:

  1. Check your current setting: SHOW VARIABLES LIKE 'lower_case_table_names';
  2. If it's 0 on Linux (the default), rename to match the case your app expects. Use exact case: RENAME TABLE Orders_archive TO orders_archive;
  3. If you need to switch to case-insensitive mode, do it on a fresh server or after dumping and reloading all data. Setting lower_case_table_names=1 on an existing MySQL 8 instance will corrupt your table names.

The real issue: MySQL's data dictionary stores table names as-is. When you change lower_case_table_names after creation, the dictionary entries don't update. You'll see the table in SHOW TABLES but can't query it because the internal lookup uses the new folding rule.

3. Metadata lock from a long-running query or transaction

Less common but nasty. You rename a table, it seems to succeed, then the next query on that table fails with 1146. What's happening: RENAME TABLE needs an exclusive metadata lock on both the old and new table names. If another query holds a shared lock on the old table (like a SELECT that's still running), your RENAME waits. But MySQL 8 and later might report success before the lock is fully released in certain edge cases — especially with LOCK TABLES or GET_LOCK calls.

Check for blocking queries:

SHOW PROCESSLIST;
-- Look for queries in 'Waiting for table metadata lock' state

-- In MySQL 8:
SELECT * FROM performance_schema.metadata_locks
WHERE OBJECT_SCHEMA = 'mydb' AND OBJECT_NAME LIKE 'orders%';

The fix: Kill the blocking process with KILL <thread_id>;. Or wait for it to finish. After the lock is released, the renamed table becomes visible to all sessions. No need to rename again.

One more subtle variant: If you used RENAME TABLE to swap two tables (e.g., rename active to old, then new to active), and the second rename fails with 1146, it's because the first rename succeeded but the second one couldn't find active — because you just renamed it to old. This is a logic error, not a MySQL bug. Use RENAME TABLE active TO old, new TO active; as a single atomic statement instead.

Quick-reference summary

CauseSymptomFix
Uncommitted transaction Rename visible only in same session COMMIT; or ROLLBACK;
Case-sensitivity (lower_case_table_names=0) Table exists in SHOW TABLES but query fails with wrong case Rename to exact case of queries, or migrate to case-insensitive server
Metadata lock RENAME succeeds but subsequent queries hang or fail KILL blocking thread from SHOW PROCESSLIST

Start with the transaction check — it's the quickest to verify and the most frequent cause. If that's clean, look at case sensitivity (especially if you just migrated from Mac/Windows to Linux). Metadata locks are rare unless your app runs long reports or cron jobs that hold transactions open.

One final opinion: Avoid renaming tables in production during peak hours. Even though InnoDB supports online DDL, RENAME TABLE takes an exclusive metadata lock briefly. On a busy server with many concurrent queries, that brief lock can cascade into a pile of 1146 errors as connections try to access the old name while the rename is in progress. Do renames during maintenance windows or with pt-online-schema-change if you need zero downtime.

Related Errors in Database Errors
SQL Server 3154 Fix 'Restore Validation Check Failed' in SQL Server 0X0004D003 Fix XACT_S_SOMENORETAIN (0x0004D003) Transaction Aborted Implicit conversion error / Arithmetic overflow Column type mismatch on INSERT: real fix for SQL Server 9002 (log full) or 824 (I/O) - exact depends on context Fix 'Transaction Log Corruption Detected During Recovery' in SQL Server

Was this solution helpful?

EP
Erropedia Team
Tech Support Editors
The Erropedia editorial team researches and documents real-world tech errors from across Windows, Linux, macOS, networking, databases, cloud platforms, and more. Every solution is reviewed for accuracy and updated as software and systems evolve.