Resolving MySQL Error 1046: No Database Selected

Before getting started with the syntax let’s take a look at the SHOW ERRORS command (image below). This command is used to separate the error message we’ve received. For example, the message: ERROR 1046: No database selected will be separated into 3 parts after using the SHOW ERRORS command, They are level, code, and message. In the level column, we would know whether it is a warning or an error, In the code column we get the code number of the error, and in the Message column, we get the error message. This is more or less the same as the direct error message but, this command is a bit professional in understanding the error.

SHOW ERRORS command

Just like the “SHOW ERRORS” command, we have another command: SELECT DATABASE() This command will help us find the default database or currently selected database. It will return NULL when there is no default or currently selected database (image below).

SELECT DATABASE() command to check the default database or the currently selected database

Let us now discuss the syntax of the two different methods to resolve Error 1046.

Syntax for Explicit Selection of Database:

mysql> SHOW DATABASES ;

// will return the databases available or created

mysql> USE databaseName ;

Database changed

mysql>

Upon opening our MySQL we can start with the “SHOW DATABASES” command to know what databases we have. Then we can proceed with the “USE Database_Name command, where “Database_Name” is the name of the database that we want to work with.

Once the database is changed we need not specify the database name elsewhere unless needed

After selecting the database, we can now create tables, insert values, update values, and retrieve data from the database selected using SQL queries.

Syntax for Including Database Name in the Query:

mysql> SELECT *

—> FROM databaseName.tableName ;

This is another way of selecting the required database. We use the dot operator in this just like we would use it for classes and objects in languages like Java. Referencing our school example.., It is like saying SELECT 10 students FROM Grade-1 class A, Where Grade-1 will be our database name and class A will be our table name.

We would use the database name in the FROM clause and follow it up with a dot operator and the table name. No matter the length of the query we can simply add the database name in the FROM clause before specifying the table name.

Example of the syntax on a sample DBNote: There is no need to use the “USE” command. It is shown here to enhance the understanding of the concept

In the above image, we are currently using the “library_records” database, which we did by explicitly mentioning using the “USE” command. Now, if we want to get the records of the student_info table which belongs to the Student_Database, We can simply use the above syntax of adding the database name in the from clause before specifying the table name. If we use the SHOW DATABASE() command now it will return “library_records” as output. Let’s see why, in the upcoming examples.

How to fix MySQL Error 1046 No database selected?

MySQL error 1046, which stands for “No Database Selected,” is one of the most common errors that can prevent database operations. It happens when you run a query and don’t specify the target database.

In this article, we will discuss “How to resolve, Error 1046: No database selected”. The first and foremost step to resolve any error is, to read the error message carefully. It specifies that we haven’t selected any database, which simply means that we have to select a database to implement our queries on (and fix the error). Let’s take a look at the image below.

Error Message (code 1046)

In the image, we can see that, we are trying to SELECT all the columns FROM the table named “employee_data” but we haven’t specified from which database we have to select the columns and table. Let’s think of an example., Say… There is a school and in a school, we will have many classes like.., Grade-1 will have class-A Grade-2 will have class-A and so on… The above query is like saying Select 5 students from class A but without specifying the grade. So there are two main ways to resolve this…

  1. To explicitly select the database by using the “USE” command.
  2. To include the database name in our query.

By doing any of these two methods, we can make our query clearer and resolve the error. Let’s now proceed with the key concepts and syntax involved in our resolving process.

Similar Reads

Resolving MySQL Error 1046: No Database Selected

Before getting started with the syntax let’s take a look at the SHOW ERRORS command (image below). This command is used to separate the error message we’ve received. For example, the message: ERROR 1046: No database selected will be separated into 3 parts after using the SHOW ERRORS command, They are level, code, and message. In the level column, we would know whether it is a warning or an error, In the code column we get the code number of the error, and in the Message column, we get the error message. This is more or less the same as the direct error message but, this command is a bit professional in understanding the error....

Examples of Fixing MySQL Error 1046 No Database Selected

Example 1: Explicitly Select the Database using the “USE” Command...

Conclusion

In conclusion, the best method from the above discussed methods is the first method. Which is the “USE databaseName” method. This is desirable because, once we’ve decided on which database to work on, we could simply specify it at the start, before executing further queries. For instance, If I decide to work on a “Tournament Management System” database I could specify it at the beginning to avoid any confusions....