Syntax error Error 1046 No database Selected, how to resolve?

Error 1046 No database Selected, how to resolve?



The 1046 error occurs if you forget to select any database before creating a table. Let us see how and why this error occurs. We will try to create a table without selecting a database −

mysql> CREATE table MyTable1
   -> (
   -> id int
   -> );
ERROR 1046 (3D000): No database selected
Or
mysql> INSERT into sample values(1);
ERROR 1046 (3D000): No database selected

Look at the output above, we are getting the same 1046 error: "No database selected"

Now, we can resolve this error after selecting any database with the help of USE command −

mysql> USE business;
Database changed

Above, I have included the database with the name ?business'. After that, we can create the same table (which we tried creating above) under the database, "business" −

mysql> CREATE table MyTable1
   -> (
   -> id int
   -> );
Query OK, 0 rows affected (0.49 sec)

We can check whether the table is present or not in the "business" database. The query is as follows −

mysql> SHOW tables like '%MyTable1%';

The following is the output

+---------------------------------+
| Tables_in_business (%MyTable1%) |
+---------------------------------+
| mytable1                        |
+---------------------------------+
1 row in set (0.05 sec)
Updated on: 2019-07-30T22:30:23+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements