Tuesday, December 24, 2024

MySQL DATABASE() Function

 The DATABASE() function in MySQL is used to return the name of the currently selected database. It does not require any arguments. This function is particularly useful when you want to know which database is being used in the current session or when you're writing a query that needs to dynamically reference the current database.

Syntax:

DATABASE();

Example:

-- Select the current database
SELECT DATABASE();

Output:

If the current database is mydb, the query will return:

+-----------------+
| DATABASE()      |
+-----------------+
| mydb            |
+-----------------+

If no database is selected, the function returns NULL.

Key Points:

  • The function returns the name of the database in use for the current session.
  • If no database is selected, it will return NULL.

No comments:

Post a Comment