No database selected
Error 1046 (SQLSTATE 3D000) is returned when a SQL statement references a table without qualifying it with a database name and no default database has been selected for the session via USE or the connection string.
- 1No USE <database> statement was executed before running table queries
- 2The default database was not specified in the connection string
- 3The session context was reset (e.g., after reconnect) and the database selection was lost
A table query is executed on a fresh connection with no default database.
-- Connected as: mysql -u root -p (no database specified) SELECT * FROM users;
expected output
ERROR 1046 (3D000): No database selected
Fix 1
Use the database or specify it in the connection string
WHEN Always specify a default database.
USE myapp_db; SELECT * FROM users; -- Or specify in the connection: -- mysql -u appuser -p myapp_db
Why this works
USE sets the default database for the session. All unqualified table references resolve against this database for the remainder of the session.
Fix 2
Fully qualify all table references
WHEN When queries must work regardless of the session default database.
SELECT * FROM myapp_db.users;
Why this works
schema.table notation makes the database reference explicit, bypassing the need for a USE statement.
✕ Add a USE statement inside stored procedures as a workaround
USE inside stored routines is not permitted in MySQL/MariaDB. Use fully qualified names or ensure the caller sets the database before invoking the routine.
Stable error code.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev