
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to check which database is selected in MySQL?
We can check this with the help of DATABASE() method from dual. Suppose, we are using the database business. The query is as follows −
mysql> use business; Database changed
Now we can check which database is selected with the help of DATABASE() from dual. The query is as follows −
mysql> SELECT DATABASE() FROM DUAL;
Here is the output.
+------------+ | DATABASE() | +------------+ | business | +------------+ 1 row in set (0.00 sec)
Now let us consider another database to get that particular database name. The query is as follows.
mysql> use test Database changed mysql> SELECT DATABASE() FROM DUAL;
The following is the output.
+------------+ | DATABASE() | +------------+ | test | +------------+ 1 row in set (0.00 sec)
We can check that the above databases are present or not in MySQL. The query is as follows −
mysql> show databases;
Here is the output that displays all the databases.
+--------------------+ | Database | +--------------------+ | business | | databasesample | | education | | hello | | information_schema | | mybusiness | | mysql | | performance_schema | | sample | | schemasample | | sys | | test | | universitydatabase | +--------------------+ 13 rows in set (0.05 sec)
As you can see above, we have both databases, and we can get the current database name with the help of DATABASE() method.
An alternate way is to use the status command::
mysql> status;
The following is the output.

- Related Articles
- MySQL Error - #1046 - No database selected
- How to check if a MySQL database exists?
- Error 1046 No database Selected, how to resolve?
- How to check an empty table already in a MySQL database?
- MySQL query to check if database is empty or not?
- How to check statement of creating a particular MySQL database?
- Check how many rows are in a MySQL database table?
- How to check whether a radio button is selected with JavaScript?
- Which PHP function is used to select a MySQL database?
- How to check table status of the tables in a particular MySQL database?
- How to check database connections in Laravel?
- Check user rights before attempting to CREATE MySQL DATABASE?
- Which PHP function is used to disconnect from MySQL database connection?
- How to check if a table already exists in the database with MySQL with INFORMATION_SCHEMA.TABLES.?
- In MySQL, how to check for a pattern which is not present within an expression?

Advertisements