In MySQL, 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. If there is no default database, it returns NULL
.
Syntax
The syntax goes like this:
DATABASE()
No arguments are required or accepted.
Example
Here’s an example to demonstrate:
SELECT DATABASE();
Example result:
+------------+ | 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 sakila;
And run it again:
SELECT DATABASE();
Result:
+------------+ | DATABASE() | +------------+ | sakila | +------------+
No Arguments are Accepted
Passing arguments to DATABASE()
results in an error:
SELECT DATABASE(oops);
Result:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'oops)' 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.