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...
- 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.
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 commandJust 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 databaseLet 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 neededAfter 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 conceptIn 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.
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.
Creating a sample database named "Employee_Database"
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..?
Error message when trying to create a table inside the "Employee_Database" DB without selecting the databaseSo, 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.
Explicitly selecting the database to be worked on...Now, we are proceeding with the creation of a table called "employee_data" " which has id, name, dept, and salary as its columns.
Created a table in "Employee_Database" DB after using the "USE" command to select the databaseAfter creating a table we are now inserting values inside the table. We can insert multiple records at once inside the table.
Once the database is selected we can create tables and insert values inside it This command is optional but still, it is good to know our currently selected database or the database we are working on right now...
To know our currently selected database we can use the "SELECT DATABASE()" commandExample 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.
Including database name in our queryIn 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.
Including the database name in the query even after explicitly selecting the database using the "USE" command Note: The databases are different, USE command has library_Records DB, and Employee_Database DB is included in the query
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.
Since we've specified USE Library_Records SELECT DATABASE() will be Library_RecordsThe image below was added with the purpose of understanding the order/sequence of SQL queries.
Output of the queries combined for referenceSince 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...
Continuing the query we can retrieve data from the library_records DB without specifying it again, 'cause we have used the "USE" commandConclusion
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.
This has another advantage.., Say.., we want to work on a library database, instead of specifying the database name in the from clause each time, we could simply start the query with USE library and then proceed without specifying the database name again elsewhere in MySQL.
Anyways, we have two different methods to fix the "Error 1046: No database selected" error. Let's recap the commands we've discussed.
1. SHOW DATABASES;
2. SELECT DATABASE();
3. USE databaseName;
4. From clause: FROM databaseName.tableName;
Similar Reads
How to Use Docker for Your MySQL Database
In the modern software development field, the use of containers has become common. Docker, a leading containerization platform, has changed the way applications are developed, tested, and deployed. While Docker is often associated with packaging and running applications, it can also be a useful tool
5 min read
How to Show/List Tables in MySQL Database
In MySQL, the SHOW TABLES command is a powerful tool used to list the tables within a specific database. This command provides a convenient way to view the tables that exist in a database without needing to query the database schema directly. In this article, we are going to explore various ways whe
5 min read
How to retrieve data from MySQL database using PHP ?
There are steps to understand for retrieving the data from the MySQL database. Approach: Create the database, then create the table for data.Enter the rows in the table. You have to connect to the database. Now we understand each and every step as shown below.  Example 1: In this. we use PHPMyAdmin
2 min read
How to Fix a 500 Internal Server Error?
The "HTTP 500 - Internal Server Error" is a frustrating and common issue that indicates something has gone wrong on the server's side. This error is a catch-all message indicating that something went wrong on the web server but the server cannot specify what exactly the problem is. It's a common ser
10 min read
How to Show a List of All Databases in MySQL
MySQL is a popular open-source relational database management system (RDBMS) that is uniquely used to construct expandable and high-productivity databases. MySQL, which was created by MySQL AB and later acquired by its current owner Oracle Corporation, was originally introduced in 1995.MySQL is repu
7 min read
How to Check MySQL Database
SQL stands for Structured Query Language which is a computer language for storing, manipulating, and retrieving data stored in relational database management systems (RDBMS). SQL was developed at IBM by Donald Chamberlin, Donald C. Messerli, and Raymond F. Boyce in the year 1970s. MySQL is an open-s
4 min read
How to Reset Password for Azure Database
Microsoft Azure's Azure Database provides cloud-based database solutions for all types of data management requirements. Users don't need to worry about infrastructure upkeep while creating, scaling, and managing databases thanks to alternatives like SQL databases and Azure Database for MySQL. This p
4 min read
How to List all Databases in the Mongo Shell?
Knowing how to list databases in MongoDB is an important part of managing your data effectively. By using basic MongoDB shell commands, you can easily see what databases you have and understand their sizes. By using commands such as show dbs and db.stats() and users can gain valuable insights into t
4 min read
How to Select Data from two Different Servers in MySQL?
Efficiently managing data retrieval from two separate MySQL servers can be achieved through either federated tables or a middle-tier application. Federated tables allow for direct querying and joining of data across servers by creating a table on one server that references a table on another.In this
5 min read
How to Fix - NoSuchTable: Table doesn't exist in the database.
This article helps you to understand the "NoSuchTable: Table doesn't exist in the database" error, why it occurs and possible solutions you can try out to fix the errors. What is No Such Table Exist Error? Error message "NoSuchTable: Table doesn't exist in the database" appears when you have created
4 min read