1046ERRORTier 1 — Safe✅ HIGH confidence

No database selected

Category: ConnectionVersions: All MariaDB / MySQL versions

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

  1. 1No USE <database> statement was executed before running table queries
  2. 2The default database was not specified in the connection string
  3. 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.

trigger — this will ERROR
-- Connected as: mysql -u root -p (no database specified)
SELECT * FROM users;
ERROR 1046 (3D000): No database selected

Fix 1: Use the database or specify it in the connection string

Always specify a default database.

fix
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 queries must work regardless of the session default database.

fix
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

📄 Reference pages

USE statementdatabase selection
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →