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 Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan
14 min read
Waterfall Model - Software Engineering The Waterfall Model is a Traditional Software Development Methodology. It was first introduced by Winston W. Royce in 1970. It is a linear and sequential approach to software development that consists of several phases. This classical waterfall model is simple and idealistic. It is important because
13 min read