MySQL DATABASE

The DATABASE() function in MySQL is a simple and straightforward function that returns the name of the current database. It does not require any parameters and can be used within SQL statements to dynamically reference the current database.

Syntax

The syntax for the DATABASE() function is as follows:

DATABASE()

Example

Let’s consider a simple example to illustrate the use of the DATABASE() function:

-- Create a new database
CREATE DATABASE sample_database;

-- Use the newly created database
USE sample_database;

-- Display the current database using DATABASE() function
SELECT DATABASE();

In this example, we first create a new database named sample_database using the CREATE DATABASE statement. After that, we switch to the newly created database using the USE statement. Finally, we use the SELECT DATABASE(); statement to display the name of the current database, which, in this case, is sample_database.

It’s important to note that the DATABASE() function is particularly useful when you want to write generic SQL queries or scripts that need to reference the current database dynamically. This can be beneficial in scenarios where the database name is subject to change or when writing portable SQL code that can be executed on different databases without modification.

Remember that the DATABASE() function does not take any arguments, and its purpose is to return the name of the current database in use at the time the function is called.