MySQL Error 1046 - No Database Selected
What is MySQL?
MySQL is a Relational Database Management System (RDBMS). SQL in MySQL is the abbreviation of "Structured Query Language", which Oracle developed it in 1995. It is one of the most famous database management systems. It stores data in table format. A relational database is a database that stores data in more than one table, and those tables may be further connected through a primary key. It is also available free on the website of oracle.
It is written in C and C++ languages.
MySQL Error - No Database Selected
The error which occurs when the execution of a statement is done without first selecting a database is known as MySQL no Database Selected error. It may also be possible that the Database needs to be included, or the user may have chosen the wrong Database.
Causes of MySQL-No Database Selected Error
The MySQL - 1046 error occurs when a user tries to execute the commands without selecting a database first. This type of error commonly occurs when creating a table in the MySQL database using the command prompt (cmd). To execute the commands from the command prompt, selecting a database in MySQL is necessary. Otherwise, MySQL will not be able to determine from which Database the user is running the script.
There is a command in MySQL that can help a user to determine the currently selected Database. It is the best and quick way to ascertain if the error is caused by genuine reasons for not specifying the Database or something else. If the error arises by not selecting the Database, it becomes easy to overcome it by selecting <database_name> with the databases available in the system.
Before creating a table in MySQL, selecting a database where the table gets stored is necessary. Choosing the Database is not automatic; it is a manual process. So, before running any script or commands from the command prompt, ensure that the database name is provided to the server.
How to Resolve the Issue of “No Database Selected Error”
The issue of no database selected can be solved by following these simple steps-
Step 1: Open Command Prompt
- First of all, open MySQL Command line Client on the start menu.
- Then enter the passkey to execute the commands.
Step 2: Select the Database
If the user knows the database name:
- Select the Database in which you want to execute the commands.
- If you know the database name, input the database name preceding the keyword 'use'. Like:-
use <database_name>;
Note: It is important to follow this step. Otherwise, it will give a 1046(3D000) error message.
If user don’t know the name of the database:
Suppose a user needs to learn the database name on which he wants to execute statements or commands. He can show the list of Databases available in the system by using the keyword SHOW. If the user doesn't know the name of the Database:
SHOW databases;
This command will show all the databases available in the system in a list format. The user can choose a database that he wants by using <database_name>;
If the user wants to create a new database:
If the Database the user needed is unavailable or he wants to create a new one, he can create it by entering a new database name preceding the keyword 'create'.
CREATE DATABASE database_name;
Step 3: Execution of Statements
Once the Database is selected, a user can execute its required SQL commands. There are various commands available in MySQL to create tables and make amendments. For example-
CREATE TABLE <table_name>;
SHOW * from <table_name>;
How to View Currently Selected Database: To Avoid MySQL 1046 Error
If the user is unable to determine which Database is selected and he wants to view the currently selected Database, use the MySQL command listed below:
SELECT DATABASE();
A user can execute this command from both IDE of MySQL – MySQL command line prompt or MySQL workbench. Following this command is quite simple in both IDEs.
If the user is accessing it from the MySQL workbench, open it and execute the command:
SELECT DATABASE();
SELECT DATABASE() from DUAL;
This command will show the current Database a user is working on, i.e., it represents the Database the user has selected.
If the user is using MySQL terminal, they must log in using username and password and then execute the command:
SELECT DATABASE() from DUAL;
STATUS;
STATUS command represents the current status of the MySQL server in the system. It shows the connection id, the current Database selected, and the current user working on MySQL.