MySQL "ERROR 1146: Table doesn't exist": the database, the case, or the files
Fix MySQL ERROR 1146 "Table doesn't exist": check the database you are in, the case of the name on Linux, migrations, and tables that exist on disk but not in InnoDB.
by OhSudo ·
ERROR 1146 (42S02): Table 'shop.order' doesn't existMySQL looked in the database before the dot, shop, for a table called order, and didn’t find it. The message gives you both names, so read them: often the table exists, just under another name or in another database.
TL;DR
SELECT DATABASE();SHOW TABLES;
SELECT table_schema, table_nameFROM information_schema.tablesWHERE table_name LIKE '%order%';The last query shows where the table really is, and how its name is spelled.
1. The wrong database
The database comes from your connection, or from USE. A script that worked on one server may be connected to another database on the next:
Name it in full, which also works across databases:
Or check your connection string. A missing database name in it gives ERROR 1046: No database selected instead.
2. A typo, or a plural
It’s the first thing to rule out. Here the table is orders, and the query asked for order. Search, rather than guess:
3. Upper and lower case on Linux
This is the classic one: it works on Windows or macOS, and fails on the Linux server. On Linux, each table is a file, and file names there are case-sensitive. So Orders and orders are different tables:
ERROR 1146 (42S02): Table 'shop.orders' doesn't existSee how your server treats names:
| Value | Meaning | Default on |
|---|---|---|
0 |
Case-sensitive | Linux |
1 |
Stored in lowercase, compared without case | Windows |
2 |
Stored as written, compared without case | macOS |
The fix is consistent names, ideally all lowercase. Rename the table:
Changing the setting itself is a bigger step. On MySQL 8, lower_case_table_names can only be set when the data folder is first created, and the server refuses to start if you change it later. So it means a fresh install and a reload of the data.
4. The table was never created
- Migrations didn’t run on this server, after a fresh deploy. Check your tool’s status command.
- A dump was restored into another database, or only partly restored. A failed import stops at its first error. Read the import’s output, and see MySQL error 1064 if it says the syntax is wrong.
- In Docker, the scripts in
/docker-entrypoint-initdb.donly run on an empty data folder. With an existing volume, nothing new is created. - A temporary table exists only in the session that created it.
5. It’s on disk, and not in MySQL
Copying files from the data folder doesn’t copy InnoDB tables. Their data and their entry in MySQL’s dictionary have to move together. After a copied folder, or a damaged dictionary, you see the table’s files, and MySQL says:
ERROR 1146 (42S02): Table 'shop.orders' doesn't existERROR 1932 (42S02): Table 'shop.orders' doesn't exist in engineDon’t copy table files between servers, except with the documented transportable tablespace feature. Restore from a dump instead, made with mysqldump or MySQL Shell’s dump utilities, or from a physical backup tool such as Percona XtraBackup or MySQL Enterprise Backup. Check the error log too: it often explains why InnoDB couldn’t open the table.
6. A view, or a trigger, pointing at a missing table
If the query uses a view whose table was renamed or dropped, you get a related error:
ERROR 1356 (HY000): View 'shop.order_totals' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use themFind which views use the table, then recreate them:
SELECT table_name FROM information_schema.viewsWHERE table_schema = 'shop' AND view_definition LIKE '%orders%';Warning
Don’t recreate an empty table with the same name just to make the error stop. The application then runs, and writes data in the wrong place, while the real table, if it exists somewhere, is ignored. Find the original first.
Shell, we have a problem!
Similar, but different: ERROR 1054: Unknown column means the table exists, and a column doesn’t. ERROR 1064 is a syntax error. ERROR 1045 is about logging in. On PostgreSQL, this error is relation does not exist, and on Oracle, ORA-00942. To compare their syntax, see the SQL Dialect Reference.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.