MySQL error 1046: No database selected
The statement names a table without a database, and the session has no default database. Run USE first or qualify the table as db.table.
- Error number
1046- Name
ER_NO_DB_ERROR- SQLSTATE
3D000- Category
- Databases, tables and columns
What it means
MySQL resolves an unqualified table name in the session's default database. If you connected without one and never ran USE, there's nowhere to look, so the statement fails with 1046 before any table lookup.
Common causes
- Connecting without a database (for example
mysql -u app -pwith no database argument) and then querying a table by name. - Running a dump or script that doesn't start with
USEorCREATE DATABASE ... ; USE .... - A connection pool or tool that opens connections without a default schema.
How to fix it
-
Select the database for the session.
USE shop;
-
Or qualify every table with its database.
SELECT * FROM shop.orders LIMIT 10;
-
Put the database in the connection itself, for example
mysql -u app -p shop, or thedatabasesetting of your driver.
Reproduce it safely
-- Connect without a database, then: SELECT * FROM orders; -- ERROR 1046 (3D000): No database selected
Related errors
More databases, tables and columns errors
1054Unknown column in field list1050Table already exists1051Unknown table1060Duplicate column name1091Can't DROP; check that column/key exists1305Function or procedure does not exist1442Trigger can't update its own table
The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.
Work on MySQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.