Error
Error Code: 1146

MariaDB Error 1146: Table Not Found

📦 MariaDB
📋

Description

Error 1146 signifies that MariaDB cannot locate a table referenced in your SQL query. This typically occurs when the specified table does not exist in the current database or schema, or its name is misspelled.
💬

Error Message

Table '%s.%s' doesn't exist
🔍

Known Causes

4 known causes
⚠️
Incorrect Table Name
The table name specified in the SQL query contains a typographical error or incorrect casing.
⚠️
Wrong Database Context
The SQL query is executed against an unintended database, or the referenced table is not in the currently selected database.
⚠️
Table Not Created or Dropped
The table genuinely does not exist in the database, either because it was never created or was previously removed.
⚠️
Case Sensitivity Mismatch
The table name in the query does not exactly match the actual table name's casing, especially on case-sensitive operating systems.
🛠️

Solutions

5 solutions available

1. Verify Table Name and Database easy

Check table exists and spelling is correct

1
List all tables in current database
SHOW TABLES;
2
Check which database you're connected to
SELECT DATABASE();
3
Search for table by partial name
SHOW TABLES LIKE '%user%';
4
Use fully qualified table name
SELECT * FROM database_name.table_name;

2. Fix Case Sensitivity Issues easy

Handle table name casing on different OS

1
Check lower_case_table_names setting
SHOW VARIABLES LIKE 'lower_case_table_names';
2
Find actual table name with correct casing
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'your_database'
AND LOWER(TABLE_NAME) = 'users';
3
Use correct casing in your query
-- If table is actually 'Users' not 'users'
SELECT * FROM Users;

3. Create the Missing Table medium

Create table if it should exist

1
Create the table with appropriate structure
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(255) UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
2
Run migrations if using a framework
# Laravel
php artisan migrate

# Django
python manage.py migrate

# Rails
rake db:migrate

4. Recover Dropped Table advanced

Restore table from backup if accidentally dropped

1
Restore from mysqldump backup
mysql -u root -p database_name < backup.sql
2
Or restore single table from backup
# Extract table from backup
grep -A 1000 'CREATE TABLE `users`' backup.sql | grep -B 1000 -m 1 '^--' > users_table.sql
mysql -u root -p database_name < users_table.sql
3
Check binary logs if recently dropped (before logs rotated)
SHOW BINARY LOGS;
mysqlbinlog /var/lib/mysql/mysql-bin.000001 | grep -i 'users'

5. Fix Orphaned InnoDB Table advanced

Handle .frm file without .ibd file or vice versa

1
Check if table files exist on disk
ls -la /var/lib/mysql/database_name/ | grep users
2
If .frm exists but .ibd missing (InnoDB file-per-table)
-- Try to discard the orphaned tablespace
ALTER TABLE users DISCARD TABLESPACE;
-- Then restore .ibd from backup and import
ALTER TABLE users IMPORT TABLESPACE;
3
If table shows in SHOW TABLES but can't be accessed
-- Remove orphaned entry
DROP TABLE IF EXISTS users;
-- Then recreate
🔗

Related Errors

5 related errors