1046
MySQLERRORCommonConnectionHIGH confidence

No 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
  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
trigger — this will error
-- 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 the database or specify it in the connection string
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.

Fully qualify all table references
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

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 versions

Stable error code.

Sources
Official documentation ↗

MariaDB Server error code 1046 / ER_NO_DB_ERROR

MariaDB USE statement

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors