MySQL error 1146 (Table Doesn't Exist) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Verify the schema context: `SELECT DATABASE();` — if NULL, run `USE dbname;` or prefix tables with dbname.. If that does not apply, list tables to confirm the exact name: `SHOW TABLES;` or `SHOW TABLES FROM dbname LIKE '%pattern%';` — the full checklist is below.
MySQL Error Code: 1146
Official name: Table Doesn't Exist
System name: Table doesn't exist
Service: MySQL
Table '%s.%s' doesn't exist
You may see this error reported as one of the following messages:
Table 'mydb.users' doesn't existAfter restoring a dump, the app queries orders while connected with no default database selected. MySQL returns ERROR 1146 (42S02): Table 'mydb.orders' doesn't exist. Note the format: the prefix is the database MySQL actually looked in — if it says mysql.orders or a bare orders, you are connected to the wrong database rather than missing a table.
Run these to confirm the cause before applying a fix — each command answers a specific question.
SELECT DATABASE();
SHOW TABLES LIKE '%orders%';
DATABASE() returning NULL means no default schema selected — qualify your table names or run USE mydb.
-- from the shell: verify per-table integrity
-- mysqlcheck -u root -p --check mydb
SHOW TABLES;
An .ibd file present but the table invisible usually means a crashed or partially-restored tablespace.
Table truly absent?
├─ migration not applied → run it (flyway / laravel migrate / prisma db push)
├─ dump restored without tables → re-import including CREATE TABLE
└─ wrong environment → fix DB name in the connection string
Table exists but invisible?
├─ no default database → USE mydb (or qualify as mydb.orders)
├─ case sensitivity: Orders vs orders on Linux (lower_case_table_names)
└─ crashed tablespace → restore from backup or re-create
SELECT DATABASE();
SHOW TABLES;
SELECT * FROM dbname.users LIMIT 1; -- fully qualified
Confirms which database is active and whether the table exists under its real name.
Log the failing table from err.sql and include the schema in the query to avoid cross-db mistakes.
pool.query('SELECT * FROM users WHERE id = ?', [id], (err, rows) => {
if (err && err.code === 'ER_NO_SUCH_TABLE') console.error(err.sql)
})
If a test run creates tables, make sure create_all / migrations run before queries.
Base.metadata.create_all(engine)
# or run migrations: alembic upgrade head
Always qualify the database in the DSN so PDO never lands in the wrong schema.
$pdo = new PDO('mysql:host=' . $host . ';dbname=' . $db, $user, $pass);
$pdo->exec('USE ' . $db);
Most often this happens when a typo in the table name, or wrong database selected (no `USE db` / no db prefix in the query), or when case mismatch: table names are case-sensitive on Linux depending on lower_case_table_names.
Verify the schema context: `SELECT DATABASE();` — if NULL, run `USE dbname;` or prefix tables with dbname..
This page documents fixes for: nodejs-mysql2, python-sqlalchemy, php-pdo.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official MySQL documentation linked below and adds practical troubleshooting guidance on top.