MariaDB DATABASE() Explained

In MariaDB, DATABASE() is a built-in function that returns the default (current) database name.

The result is returned as a string in the utf8 character set.

Syntax

The syntax goes like this:

DATABASE()

No arguments are required or accepted.

Example

Here’s an example to demonstrate:

SELECT DATABASE();

Here’s the result in my terminal window:

MariaDB [(none)]> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL       |
+------------+

In this case I don’t have a current database and so the result is NULL.

Let’s switch to a database:

USE KrankyKranes;

And run it again:

SELECT DATABASE();

Result:

Database changed
MariaDB [KrankyKranes]> SELECT DATABASE();
+--------------+
| DATABASE()   |
+--------------+
| KrankyKranes |
+--------------+

No Arguments are Accepted

Passing any arguments to DATABASE() results in an error:

SELECT DATABASE(123);

Result:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '123)' at line 1

Stored Routines

Within a stored routine (such as a stored procedure, stored function, etc), the default database is the database that the routine is associated with, which is not necessarily the same as the database that is the default in the calling context.

Synonym

The SCHEMA() function is a synonym for the DATABASE() function.