How to Show the Current Database in MySQL

Mehvish Ashiq Feb 02, 2024
How to Show the Current Database in MySQL

This tutorial highlights various queries that we can use to show the current database in MySQL. We will learn by using the Windows Command line and MySQL Workbench.

Show the Current Database in MySQL

We can use the following query in MySQL Workbench to show the current database.

SELECT DATABASE();

Output:

+------------+
| database() |
+------------+
| test       |
+------------+

We can also use the DUAL table to get the name of the database currently in use.

SELECT DATABASE() FROM DUAL;

Output:

+------------+
| database() |
+------------+
| test       |
+------------+

The DUAL table is one column and one row (1x1) representation. Although, it does not do anything but a valid syntax in MySQL.

We get the same results by omitting the FROM DUAL.

All the queries given above can also be run on Windows Command-Line. Additionally, we can use the status to get the current database, username, connection id, port number, and more.

mysql> status

Output:

--------------
mysql  Ver 8.0.28 for Win64 on x86_64 (MySQL Community Server - GPL)

Connection id:          16
Current database:       test
Current user:           root@localhost
SSL:                    Cipher in use is TLS_AES_256_GCM_SHA384
Using delimiter:        ;
Server version:         8.0.28 MySQL Community Server - GPL
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    cp850
Conn.  characterset:    cp850
TCP port:               3306
Binary data as:         Hexadecimal
Uptime:                 1 day 4 hours 38 min 9 sec

Threads: 4  Questions: 734  Slow queries: 0  Opens: 288  Flush tables: 3  Open tables: 198  Queries per second avg: 0.007
--------------
Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - MySQL Database