Outdated Version

You are viewing an older version of this section. View current production version.

CURRENT_USER

Returns the details (username and hostname) of the user executing the query.

Syntax

CURRENT_USER()
USER()

Return Type

String

Examples

memsql> SELECT CURRENT_USER();
+----------------+
| CURRENT_USER() |
+----------------+
| root@%         |
+----------------+

The below query returns the current user name and host name for the MySQL connection.

memsql> SELECT USER();
+------------------------------+
| USER()                       |
+------------------------------+
| root@localhost               |
+------------------------------+
1 row in set (1.22 sec)

This feature can be combined with views to enable row-level permissions. In this example, persons with the ‘employee’ role can only see themselves, while ‘managers’ can see all records:

memsql> CREATE TABLE employees (
    ->    id int primary key,
    ->    fullname varchar(64),
    ->    account_name varchar(64),
    ->    role enum('employee', 'manager'),
    ->    salary int
    -> );

memsql> CREATE TABLE permissions (
    ->    account_name varchar(64),
    ->    emp_id int,
    ->    primary key (account_name, emp_id)
    -> );

memsql> CREATE VIEW employee_view as
    ->    select e.* from employees e, permissions p
    ->    where e.id = p.emp_id
    ->    and p.account_name = current_user();

memsql> INSERT INTO employees values
    -> (1, 'SYSTEM', 'root@%', 'manager', 0),
    -> (2, 'Kevin Kelvin', 'kelvin@%', 'manager', 5000),
    -> (3, 'Oliver Heavyside', 'oliver@%', 'employee', 3000);

memsql> INSERT INTO permissions values
    -> ('root@%', 1), ('root@%', 2), ('root@%', 3),
    -> ('kelvin@%', 2), ('kelvin@%', 3),
    -> ('oliver@%', 3);

-- as root:
memsql> SELECT CURRENT_USER(), * FROM employee_view;
+----------------+----+------------------+--------------+----------+--------+
| CURRENT_USER() | id | fullname         | account_name | role     | salary |
+----------------+----+------------------+--------------+----------+--------+
| root@%         |  1 | SYSTEM           | root@%       | manager  |      0 |
| root@%         |  2 | Kevin Kelvin     | kevin@%      | manager  |   5000 |
| root@%         |  3 | Oliver Heavyside | oliver@%     | employee |   3000 |
+----------------+----+------------------+--------------+----------+--------+

-- as kelvin:
mysql> SELECT CURRENT_USER(), * FROM employee_view;
+----------------+----+------------------+--------------+----------+--------+
| CURRENT_USER() | id | fullname         | account_name | role     | salary |
+----------------+----+------------------+--------------+----------+--------+
| kelvin@%       |  2 | Kevin Kelvin     | kevin@%      | manager  |   5000 |
| kelvin@%       |  3 | Oliver Heavyside | oliver@%     | employee |   3000 |
+----------------+----+------------------+--------------+----------+--------+

-- as oliver:
mysql> SELECT CURRENT_USER(), * FROM employee_view;
+----------------+----+------------------+--------------+----------+--------+
|CURRENT_USER()  | id | fullname         | account_name | role     | salary |
+----------------+----+------------------+--------------+----------+--------+
| oliver@%       |  3 | Oliver Heavyside | oliver@%     | employee |   3000 |
+----------------+----+------------------+--------------+----------+--------+