Examples of Fixing MySQL Error 1046 No Database Selected
Example 1: Explicitly Select the Database using the “USE” Command
In this example, we will look into the “USE” command for explicitly selecting a database to work on… In the first step, we have created a sample database named “Employee_Database”. We use the “IF NOT EXISTS” statement inside the CREATE DATABASE command because we ensure that there is no database in the same name and we also avoid ERROR 1007 which will inform us that.., there is a database with the same name, implying database already exists.
Next, we have attempted to create a table inside our DB but we got an ERROR 1046. This is because we haven’t selected our database. We are familiar with this by now… right..?
So, we have used the “USE” command. Let’s take a simple example for this.., Say.., I am speaking about “how to prepare a fruit juice?” but I did not specify the fruit name so, I am telling my listeners that, USE apple, meaning.., The lines which I speak further are all about the fruit apple. The same goes here… We are specifying the database to be used at the start and all the further queries we write are on the selected database that we have mentioned using the syntax: USE databaseName.
Now, we are proceeding with the creation of a table called “employee_data” ” which has id, name, dept, and salary as its columns.
After creating a table we are now inserting values inside the table. We can insert multiple records at once inside the table.
This command is optional but still, it is good to know our currently selected database or the database we are working on right now…
Example 2: Including the Database name in the Query
In this example, we will look into the concept of including the database name in the query itself, instead of explicitly specifying it by using the “USE” command. Upon opening our MySQL, we can start writing our SQL queries without specifying the database name, by adding the database name in the FROM clause. In the image below, we can see that the first query returned an error message of “No database selected”. In the subsequent query, we used the concept of including the database name in the query itself, which fixed the ERROR 1046.
In our example, we want to retrieve the data from the “employee_data” table of the “Employee_Database” database. So, we specify the DB name follow it up with a dot operator, and then the required table name.
Example 3: Combining both the Methods
In this example, we will look into a combination of both methods. (This is already highlighted in the “key concepts and syntax section” Kindly refer to the syntax of the second method). Upon opening our MySQL, we have used the “USE library_Records” command, and the database is changed meaning.., all our further queries are on the “library_records” database.
On our next query, we have included the Employee_Database DB and its table to retrieve info about that DB and table. As mentioned earlier, we could simply specify the database name and execute the required query. But, specifying the DB name is essential when trying to retrieve info from other database. Here library_records and Employee_Database or two different databases.
In this example, we have implemented three queries…
1. USE library_records.
2. SELECT query from Employee_Database.employee_data where the salary is greater than 50000 rupees.
3. finally, we have used the SELECT DATABASE() command to know the currently selected database.
In that, we could see the library_records database was returned as an output. This is because the SELECT DATABASE() query considers the the database that is selected, by using the “USE databaseName” command as the database that is currently in use, even though.., we have used the Employee_Database DB in our previous query.
The image below was added with the purpose of understanding the order/sequence of SQL queries.
Since the library_records DB was selected at the start.., we can continue to retrieve data or records from that database without calling or specifying it again. The image below is an example of it…
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.
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…
- To explicitly select the database by using the “USE” command.
- 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.