1046ERRORTier 1 — Safe✅ HIGH confidenceNo database selected
What this means
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.
Why it happens
- 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
How to reproduce
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;Fix 1: Use the database or specify it in the connection string
Always specify a default database.
USE myapp_db;
SELECT * FROM users;
-- Or specify in the connection:
-- mysql -u appuser -p myapp_dbWhy 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 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.
What not to do
Add a USE statement inside stored procedures as a workaround
Why it's wrong: 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.
Version notes
All versionsStable error code.Sources
📚 Official docs: https://mariadb.com/kb/en/use/
🔧 Source ref: MariaDB Server error code 1046 / ER_NO_DB_ERROR
📖 Further reading: MariaDB USE statement
Confidence assessment
✅ HIGH confidence
Stable and trivial to reproduce.
See also
🔗 Related errors
📄 Reference pages