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.

SQL Command

Advertisements