MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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

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.

MySQL Error - No Database Selected

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.