SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

MySQL | DATABASE() and CURRENT_USER() Functions

In MySQL, there are a number of built-in functions that you can use to obtain information about the current environment or user session. Two of these functions are DATABASE() and CURRENT_USER().

1. DATABASE() Function:

The DATABASE() function returns the name of the current database. If no database is selected, it returns NULL.

Example:

SELECT DATABASE();

This will return the name of the currently selected database.

Usage:

If you're connected to a MySQL instance and you're not sure which database you're currently working in, the DATABASE() function can be useful.

2. CURRENT_USER() Function:

The CURRENT_USER() function returns the user name and host name for the MySQL account used by the current session. It��s useful to know which user account you're connected with, especially when dealing with permissions.

Example:

SELECT CURRENT_USER();

This will return the current user in the format username@hostname.

Usage:

This function is especially handy if you have multiple MySQL user accounts and you want to confirm which account you're currently using, or if you're in a session and you're unsure about your user privileges.

Notes:

  • Both these functions can be especially useful in applications where you want to display or log the current database or user information, or when writing scripts where you need to adapt behavior based on the current user or database.

  • Remember, while these functions provide information about the current session, they don't change the state of the session. If you want to change the current database, for instance, you'd use the USE databasename statement. If you want to change the user, you'd typically have to reconnect with the desired credentials.

  1. MySQL DATABASE() function example:

    • The DATABASE() function in MySQL returns the name of the current database.
    SELECT DATABASE();
    
  2. How to use DATABASE() function in MySQL:

    • To use the DATABASE() function, simply include it in a SELECT statement.
    SELECT DATABASE() AS CurrentDatabase;
    
  3. Getting the current database in MySQL with DATABASE():

    • The DATABASE() function can be used to retrieve the current database in MySQL.
    SELECT "Current Database is: " AS Message, DATABASE() AS CurrentDatabase;
    
  4. Using CURRENT_USER() in MySQL queries:

    • The CURRENT_USER() function returns the current user and host in MySQL.
    SELECT CURRENT_USER() AS CurrentUser;
    
  5. Examples of DATABASE() and CURRENT_USER() functions in MySQL:

    • Combining DATABASE() and CURRENT_USER() to get both database and user information.
    SELECT "Current Database is: " AS Message, DATABASE() AS CurrentDatabase, 
           "Current User is: " AS UserMessage, CURRENT_USER() AS CurrentUser;
    
  6. Retrieve current user and database in MySQL:

    • Another example to retrieve both current user and database using the functions.
    SELECT CONCAT("Current User: ", CURRENT_USER(), " | Current Database: ", DATABASE()) AS UserInfo;
    
  7. MySQL database name and user identification with functions:

    • Demonstrating the use of functions for database and user identification.
    SELECT CONCAT("Database: ", DATABASE(), " | User: ", CURRENT_USER()) AS DatabaseUser;
    
  8. MySQL system functions for database and user information:

    • MySQL provides various system functions for obtaining information about the database and user.
    SELECT DATABASE() AS CurrentDatabase, CURRENT_USER() AS CurrentUser, VERSION() AS MySQLVersion;