MySQL DATABASE() Function

The MySQL DATABASE() function returns the current database name as a string.

DATABASE() Syntax

Here is the syntax of the MySQL DATABASE() function:

DATABASE()

Parameters

The MySQL DATABASE() function does not require any parameters.

Return value

The DATABASE() function returns a UTF8 string, which is the current database name.

If you have not selected a database, the SCHEMA() function will return NULL.

DATABASE() Examples

If you have just connected to the MySQL server and have not selected a database, the DATABASE() function will return NULL.

SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL       |
+------------+

Then, we use USE statement to select testdb as the default database:

USE testdb;

Now, let’s use the DATABASE() function get the current database:

SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| testdb     |
+------------+