SQL reference (MySQL and MariaDB dialects)

Information functions

VERSION, DATABASE, USER, CURRENT_USER, CONNECTION_ID, LAST_INSERT_ID, ROW_COUNT, FOUND_ROWS, CHARSET and COLLATION.

These functions report on the session and the server. They are not deterministic, so they cannot be used in a generated column, a functional index or a CHECK constraint (the server refuses them as MySQL does).

COERCIBILITY(x) is worked out from what is written (explicit COLLATE 0, column 2, system constant 3, literal 4, number 5, NULL 6). CHARSET and COLLATION read COLLATE and N'..'. Not supported yet: BENCHMARK, CURRENT_ROLE, FORMAT_BYTES.

Summary of functions#

FunctionWhat it doesDialect
CHARSETReturns the character set of the argument: utf8mb4 for text, binary for anything else.MySQL and MariaDB
COLLATIONReturns the collation of the argument: the connection's collation for text (utf8mb4_0900_ai_ci in the MySQL dialect, utf8mb4_general_ci in the MariaDB dialect), binary for anything else.MySQL and MariaDB
CONNECTION_IDReturns the number of the connection (the thread id the server gave it).MySQL and MariaDB
CURRENT_USERReturns the account (user@host) whose privileges the session has, as it is named in the account table.MySQL and MariaDB
DATABASEReturns the name of the default database, NULL if there is none.MySQL and MariaDB
FOUND_ROWSReturns the number of rows the last SELECT found; after `SELECT SQL_CALC_FOUND_ROWS ...MySQL and MariaDB
LAST_INSERT_IDReturns the first AUTO_INCREMENT value the last INSERT of this session generated (not the last of a multi-row INSERT).MySQL and MariaDB
ROW_COUNTReturns the number of rows the last statement changed (inserted, updated, deleted), -1 for a statement that returns rows.MySQL and MariaDB
SESSION_USERReturn the user name and host the client connected as (user@host).MySQL and MariaDB
VERSIONReturns the server version: 8.0.36-Raft DB as MySQL, 11.4.13-MariaDB-Raft DB as MariaDB.MySQL and MariaDB

CHARSET#

SQL
CHARSET(str)

Returns the character set of the argument: utf8mb4 for text, binary for anything else. Every text of Raft DB is utf8mb4.

Example:

SQL
SELECT CHARSET('a'), CHARSET(1);

Result: utf8mb4, binary

COLLATION#

SQL
COLLATION(str)

Returns the collation of the argument: the connection's collation for text (utf8mb4_0900_ai_ci in the MySQL dialect, utf8mb4_general_ci in the MariaDB dialect), binary for anything else.

Example:

SQL
SELECT COLLATION('a'), COLLATION(1);

Result: utf8mb4_0900_ai_ci (MySQL dialect), binary

CONNECTION_ID#

SQL
CONNECTION_ID()

Returns the number of the connection (the thread id the server gave it).

Example:

SQL
SELECT CONNECTION_ID();

Result: a number, the id shown by SHOW PROCESSLIST

CURRENT_USER#

SQL
CURRENT_USER()
CURRENT_USER

Returns the account (user@host) whose privileges the session has, as it is named in the account table.

Example:

SQL
SELECT CURRENT_USER();

Result: for example root@%

DATABASE#

SQL
DATABASE()
SCHEMA()

Returns the name of the default database, NULL if there is none. SCHEMA is a synonym.

Example:

SQL
SELECT DATABASE();

Result: the name given by USE, or NULL

FOUND_ROWS#

SQL
FOUND_ROWS()

Returns the number of rows the last SELECT found; after SELECT SQL_CALC_FOUND_ROWS ... LIMIT n the number it would have found without the LIMIT.

Example:

SQL
SELECT SQL_CALC_FOUND_ROWS id FROM t LIMIT 2;
SELECT FOUND_ROWS();

Result: the number of rows of t

LAST_INSERT_ID#

SQL
LAST_INSERT_ID()

Returns the first AUTO_INCREMENT value the last INSERT of this session generated (not the last of a multi-row INSERT). It is kept per session, so another session's inserts do not change it. The form LAST_INSERT_ID(expr) is not supported: the argument is ignored.

Example:

SQL
INSERT INTO seq (v) VALUES ('x'), ('y'), ('z');
SELECT LAST_INSERT_ID(), MAX(id) FROM seq;

Result: the id of the row x, then the id of z

ROW_COUNT#

SQL
ROW_COUNT()

Returns the number of rows the last statement changed (inserted, updated, deleted), -1 for a statement that returns rows. An UPDATE that sets a column to the value it has does not count the row.

Example:

SQL
UPDATE words SET n = n;
SELECT ROW_COUNT();

Result: 0

SESSION_USER#

SQL
SESSION_USER()
SYSTEM_USER()
USER()

Return the user name and host the client connected as (user@host). They are synonyms.

Example:

SQL
SELECT USER();

Result: for example root@localhost

VERSION#

SQL
VERSION()

Returns the server version: 8.0.36-Raft DB as MySQL, 11.4.13-MariaDB-Raft DB as MariaDB.

Example:

SQL
SELECT VERSION();

Result: 8.0.36-Raft DB in the MySQL dialect

See also#