The SESSION_USER() function in MySQL is used to retrieve the current user and host name for the MySQL session. It returns a string in the format ‘user_name’@’host_name’, indicating the user and host associated with the current session.
Syntax
Here is the syntax for the SESSION_USER() function:
SESSION_USER()
Example
Now, let’s look at an example to better understand how to use this function:
-- Create a sample database CREATE DATABASE IF NOT EXISTS exampleDB; USE exampleDB; -- Create a sample table CREATE TABLE IF NOT EXISTS employees ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), department VARCHAR(255), salary INT ); -- Insert some sample data INSERT INTO employees (name, department, salary) VALUES ('John Doe', 'IT', 60000), ('Jane Smith', 'HR', 55000), ('Bob Johnson', 'Finance', 70000); -- Display the current user and host using SESSION_USER() SELECT SESSION_USER() AS current_user; -- Query the employee table SELECT * FROM employees;
In this example, we first create a sample database named exampleDB and a table named employees. After inserting some sample data into the employees table, we use the SELECT SESSION_USER() AS current_user; statement to retrieve and display the current user and host for the MySQL session.
Keep in mind that the SESSION_USER() function is a part of the session-related functions in MySQL, and its result depends on the user who is currently connected to the database. The output will be in the form ‘user_name’@’host_name’, indicating the user and host for the current session.