MySQL Error - #1046 - No database selected


The error-#1046 can occur when we are creating a table, but forget to select the database. Let us say we have started MySQL as shown below −

Command Line

After giving the correct password, the above window will open. Now create a table without choosing any database. This will show an error −

mysql> CREATE table TblUni
-> (
-> id int,
-> Name varchar(100)
-> );

ERROR 1046 (3D000): No database selected

The following screenshot is showing the same error −

No_database_selected

Now, choose any database to get rid of the above error. Firstly, let us check how many databases are present in MySQL with the help of SHOW command −

mysql> SHOW databases;

The following is the output −

+--------------------+
| Database           |
+--------------------+
| business           |
| hello              |
| information_schema |
| mybusiness         |
| mysql              |
| performance_schema |
| sample             |
| sys                | 
| test               |
+--------------------+
9 rows in set (0.00 sec)

Now, we can choose any database. Suppose I am using the database ‘business’, therefore we can choose with the help of ‘use’ command.

mysql> use business;
Database changed

After using database ‘business’, we can create the above table and we will not get any error.

mysql> CREATE table TblUni
-> (
-> id int,
-> Name varchar(100)
-> );
Query OK, 0 rows affected (0.50 sec)

Updated on: 24-Jun-2020

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements