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
- Importing a dump into a connection with no database.
mysqldump shop ordersandmysqldump shopwrite noUSEline, somysql -u root -p < shop.sqlfails at the firstDROP TABLEorCREATE TABLE. - The connection string has no database: a
mysql://user@host:3306URL without a path, an empty database field in a client, or a framework setting such asDB_DATABASEleft blank. - A script that relies on a
USEthat ran in a different session, or that was removed. - 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.