MySQL | DATABASE() and CURRENT_USER() Functions
Last Updated :
03 Sep, 2024
In MySQL, certain functions provide crucial information about the current session which can be particularly useful when working with multiple databases or managing user permissions. Two such important functions are DATABASE()
and CURRENT_USER()
.
In this article, We will learn about the MySQL DATABASE() and CURRENT_USER() Functions with the help of various examples and so on.
MySQL DATABASE() Function
The DATABASE()
function in MySQL returns the name of the current database selected in the session. If no database is selected, it returns NULL
.
Syntax:
SELECT DATABASE();
Explanation: The SELECT DATABASE();
query in MySQL is used to retrieve the name of the currently selected database in the session. If no database has been selected the function will return NULL and
allowing us to confirm whether we are working within a specific database or not.
Example of DATABASE() Function
MySQL query that identifies the current database in use. If a database name my_database
is selected in the current session, the query should return my_database
. If no database has been selected, the query should return NULL
.
Syntax:
SELECT DATABASE();
If the current session is using a database named my_database
, the result will be:
my_database
If no database has been selected, the result will be:
NULL
This function is useful when we want to check or confirm which database you are working with, particularly in scripts or applications that interact with multiple databases.
MySQL CURRENT_USER() Function
The CURRENT_USER()
function in MySQL returns the user name and host name combination of the MySQL account that the server used to authenticate the current client session. Essentially, it tells us the MySQL account that is currently being used for our connection.
Syntax:
SELECT CURRENT_USER();
Key Points:
- Returns: The function returns a string in the format
'username@hostname'
. - Usage: Typically used to check the identity of the current user, especially in scenarios where permissions or authentication might be relevant.
- Session Context: The value returned by
CURRENT_USER()
reflects the account that the server authenticated using the connection credentials, not necessarily the account used for authentication attempts. If a proxy user was used, it would return the proxy user.
Example of CURRENT_USER() Function:
Let us consider the username of MySQL account used by the server to authenticate the current client is 'root' and the hostname is 'localhost'. Therefore to know the username and hostname for the MySQL account used by the server to authenticate the current client, the CURRENT_USER() function can be executed in the following way:

Output:
'root@localhost'
Conclusion
The DATABASE()
and CURRENT_USER()
functions in MySQL are essential for obtaining session-related information, such as the current database in use and the authenticated user. These functions can be particularly useful when working with multiple databases or managing user permissions in MySQL.
Similar Reads
SQL Server CURRENT_USER() Function The CURRENT_USER() function in SQL Server is a useful tool for identifying the currently logged-in user executing a query. This function helps track user activity and enforce security policies and is often used in auditing scenarios. In this article, We will learn about SQL Server CURRENT_USER() Fun
3 min read
CURRENT_TIME() function in MySQL In MySQL, the CURRENT_TIME() function is a valuable tool for retrieving the current system time from the server, providing the exact time in hours, minutes, and seconds. It is widely used in scenarios where tracking or querying the current time is essential without needing the full date. In this art
3 min read
List the available Databases for Current User in SQL SERVER Introduction : One of the pre-needful of Database Performance Health Check is to have access to the database which we're going to tune. As SQL DBAs we can also additionally discover it unexpected that quite sometimes, we ended up in a scenario in which we've got a customer who needs us to assist wit
2 min read
PHP | MySQL Database Introduction What is MySQL? MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation. The data in a MySQL database are stored in tables which consists of columns and rows.MySQL
4 min read
PHP | MySQL ( Creating Database ) What is a database? Database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion of data from database and organizes the data in the form of tables, views, schemas, reports etc. For Example, university database organizes the data about students, faculty,
3 min read