SCHEMA() Function in MySQL

In MySQL, the SCHEMA() function is a synonym for the DATABASE() function. It 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:

SCHEMA()

No arguments are required or accepted.

Example

Here’s an example to demonstrate:

SELECT SCHEMA();

Here’s the result in my terminal window:

+----------+
| SCHEMA() |
+----------+
| NULL     |
+----------+

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

Let’s switch to a database:

USE world;

And run it again:

SELECT SCHEMA();

Result:

+----------+
| SCHEMA() |
+----------+
| world    |
+----------+

No Arguments are Accepted

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

SELECT SCHEMA(wrong);

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 'wrong)' 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