// MySQL error reference

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
Error message
No database selected

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 -p with no database argument) and then querying a table by name.
  • Running a dump or script that doesn't start with USE or CREATE DATABASE ... ; USE ....
  • A connection pool or tool that opens connections without a default schema.

How to fix it

  1. Select the database for the session.

    USE shop;
  2. Or qualify every table with its database.

    SELECT * FROM shop.orders LIMIT 10;
  3. Put the database in the connection itself, for example mysql -u app -p shop, or the database setting 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

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.

// fewer errors, faster fixes

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.