SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
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()
.
DATABASE()
Function:The DATABASE()
function returns the name of the current database. If no database is selected, it returns NULL
.
SELECT DATABASE();
This will return the name of the currently selected database.
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.
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.
SELECT CURRENT_USER();
This will return the current user in the format username@hostname
.
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.
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.
MySQL DATABASE() function example:
DATABASE()
function in MySQL returns the name of the current database.SELECT DATABASE();
How to use DATABASE() function in MySQL:
DATABASE()
function, simply include it in a SELECT
statement.SELECT DATABASE() AS CurrentDatabase;
Getting the current database in MySQL with DATABASE():
DATABASE()
function can be used to retrieve the current database in MySQL.SELECT "Current Database is: " AS Message, DATABASE() AS CurrentDatabase;
Using CURRENT_USER() in MySQL queries:
CURRENT_USER()
function returns the current user and host in MySQL.SELECT CURRENT_USER() AS CurrentUser;
Examples of DATABASE() and CURRENT_USER() functions in MySQL:
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;
Retrieve current user and database in MySQL:
SELECT CONCAT("Current User: ", CURRENT_USER(), " | Current Database: ", DATABASE()) AS UserInfo;
MySQL database name and user identification with functions:
SELECT CONCAT("Database: ", DATABASE(), " | User: ", CURRENT_USER()) AS DatabaseUser;
MySQL system functions for database and user information:
SELECT DATABASE() AS CurrentDatabase, CURRENT_USER() AS CurrentUser, VERSION() AS MySQLVersion;