MySQL DATABASE() Function

The DATABASE() function in MySQL returns the name of the currently selected (or default) database. This is a very simple yet useful function for identifying which database your SQL commands are currently operating on.



DATABASE(): Definition and Usage

DATABASE() is frequently used in database applications for logging, reporting, or to dynamically adapt SQL code based on the currently active database. It doesn't take any arguments. If no database is currently selected, it might return NULL or an empty string, depending on the MySQL server's configuration.

Syntax

Syntax

DATABASE()
      

Example

Getting the Current Database Name

This query retrieves the name of the currently selected database.

Syntax

SELECT DATABASE();
      
Output

(The name of your currently selected database will be displayed here. Example: 'mydatabase')
      

**Note:** Replace `'mydatabase'` with the actual name of your currently selected database. If no database is selected, the output might be `NULL` or an empty string.