Download

ERROR 1046 (3D000): No database selected

The statement names a table without saying which database it’s in, and the connection has no default database. Connect with a database, run USE <database> first, or write database.table.

MySQL error 1046· Tested on MySQL 8.4.11 and MariaDB 11.4.13· Updated 11 October 2026

ERROR 1046 (3D000): No database selected

What it means

In MySQL and MariaDB a table lives in a database (MySQL also calls it a schema). A table name without a database in front, such as orders, is looked up in the connection’s default database: the one you named when you connected, or the last USE. Error 1046 means there isn’t one, so the server can’t tell which orders you mean.

SELECT DATABASE(); shows the default database; NULL means none is selected. Statements that don’t touch a table, such as SELECT 1 or SELECT NOW(), work without one, which is why the error can appear halfway through a script.

Common causes

  1. Importing a dump into a connection with no database. mysqldump shop orders and mysqldump shop write no USE line, so mysql -u root -p < shop.sql fails at the first DROP TABLE or CREATE TABLE.
  2. The connection string has no database: a mysql://user@host:3306 URL without a path, an empty database field in a client, or a framework setting such as DB_DATABASE left blank.
  3. A script that relies on a USE that ran in a different session, or that was removed.
  4. A connection pool handing out a fresh connection that never ran your USE.

How to fix it

Name the database when you connect

On the command line, the database comes after the options:

mysql -h <host> -u <user> -p shop
mysql -h <host> -u <user> -p shop < shop.sql

In a URL it’s the path: mysql://<user>@<host>:3306/shop. See the MySQL connection string page for the other parts.

Select it in the session

USE shop;
SELECT DATABASE();

USE lasts until the connection closes or the next USE. Put it at the top of a script that should always run in one database.

Qualify the table names

shop.orders works whatever the default database is, and is clearer in scripts that read from more than one database:

SELECT COUNT(*) FROM shop.orders;

Dumps: choose one approach

Either import into a database you name (above), or make the dump with --databases shop, which adds CREATE DATABASE IF NOT EXISTS and USE for that name, so it always goes back into a database called shop. The first approach lets you restore into a copy with another name.

Reproduce it

On MySQL 8.4.11, connected without a database:

SELECT DATABASE();
SELECT * FROM emails;
CREATE TABLE things (id int);
SHOW TABLES;
SELECT * FROM seo_err_mysql.emails LIMIT 1;
USE seo_err_mysql;
SELECT COUNT(*) FROM emails;
DATABASE()
NULL
ERROR 1046 (3D000): No database selected
ERROR 1046 (3D000): No database selected
ERROR 1046 (3D000): No database selected

The qualified SELECT returned its row, and after USE the unqualified one worked too.

A one-table dump imported without naming a database stopped at its first statement:

mysqldump --no-tablespaces seo_err_mysql emails > emails.sql
mysql -h127.0.0.1 -useo_err_app -p < emails.sql
ERROR 1046 (3D000) at line 22: No database selected

Line 22 was DROP TABLE IF EXISTS `emails`;. MariaDB 11.4.13 gave the same error, number and SQLSTATE.

In Inlet

A connection in Inlet has a database field, and pasting a mysql:// URL fills it from the path. Query editor completion comes from the live schema of the database you’re connected to, and an error like this one links to this page.

Inlet: a database client for the Mac

One native app for PostgreSQL, MySQL, SQL Server, SQLite, MongoDB and Redis. It explains errors where they happen, holds your edits until you save them, and keeps production read-only until you say so.

Version 0.1.0 · macOS 26 Tahoe or later · Apple silicon and Intel