Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4
ERROR 1046 means MySQL does not know which database the table name belongs to. After you log in without choosing a database, SELECT DATABASE() returns NULL, and any unqualified table name fails.
In the runs below, root logged in with no default database. Every case was fixed either by USE shop first or by writing shop. in front of the name.
01Running a script or dump that has no USE line
The script starts with CREATE TABLE orders (...). The database shop exists, but the session never selected it, so the CREATE failed with ERROR 1046. SELECT DATABASE() showed NULL.
Adding USE shop; at the top of the script made the same CREATE TABLE succeed, and SELECT DATABASE() then returned shop.
Setup
CREATE DATABASE shop;Query OK, 1 row affected (0.00 sec)CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));ERROR 1046 (3D000): No database selectedSELECT DATABASE();+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)If a script or dump file has no USE line, add USE <db>; at the top before running it.
USE shop;
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
SELECT DATABASE();Query OK, 0 rows affected (0.02 sec)
+------------+
| DATABASE() |
+------------+
| shop |
+------------+
1 row in set (0.00 sec)02SELECT with only the table name
SELECT * FROM orders failed with ERROR 1046 even though shop.orders has rows, because no database was selected.
SELECT * FROM shop.orders returned the 2 rows without changing the session.
Setup
CREATE DATABASE shop;
CREATE TABLE shop.orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
INSERT INTO shop.orders VALUES (1, 1, 100.00), (2, 1, 50.00);Query OK, 1 row affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0SELECT * FROM orders;ERROR 1046 (3D000): No database selectedSELECT DATABASE();+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)SELECT * FROM shop.orders;+----+-------------+--------+
| id | customer_id | amount |
+----+-------------+--------+
| 1 | 1 | 100.00 |
| 2 | 1 | 50.00 |
+----+-------------+--------+
2 rows in set (0.00 sec)03CREATE VIEW without a database, even when the table is qualified
The SELECT inside the view already used shop.orders, but CREATE VIEW order_summary AS ... still failed with ERROR 1046. The view itself needs a database to live in.
CREATE VIEW shop.order_summary AS ... succeeded, and selecting from it returned customer 1 with total 150.00.
Setup
CREATE DATABASE shop;
CREATE TABLE shop.orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
INSERT INTO shop.orders VALUES (1, 1, 100.00), (2, 1, 50.00);Query OK, 1 row affected (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0CREATE VIEW order_summary AS
SELECT customer_id, SUM(amount) AS total FROM shop.orders GROUP BY customer_id;ERROR 1046 (3D000): No database selectedSELECT DATABASE();+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)CREATE VIEW shop.order_summary AS
SELECT customer_id, SUM(amount) AS total FROM shop.orders GROUP BY customer_id;
SELECT * FROM shop.order_summary;Query OK, 0 rows affected (0.00 sec)
+-------------+--------+
| customer_id | total |
+-------------+--------+
| 1 | 150.00 |
+-------------+--------+
1 row in set (0.00 sec)FAQ
How do I fix No database selected in MySQL?
Select the database with USE shop; before the statement, or write the name as shop.orders. Both worked in the runs above.
How do I check which database is selected?
Run SELECT DATABASE();. It returned NULL in every failing session above, and shop after USE shop.
Why do I get ERROR 1046 when importing a .sql dump?
The dump has no USE line, so its first CREATE TABLE has no database. In the first case above, adding USE shop; at the top fixed it.
I wrote shop.orders in the SELECT — why does CREATE VIEW still fail?
The view name also needs a database. Only CREATE VIEW shop.order_summary worked in the third case.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13