SQL reference (MySQL and MariaDB dialects)

Variables (user, local and system)

User variables (@name), local variables of stored programs, and system variables (@@name, @@global.name, @@session.name): syntax, scope and what Raft DB supports.

User variables#

SQL
SET @name = expr [, @name2 = expr2] ...;
SELECT @name := expr;
SELECT @name;
SET @name := expr;

A user variable starts with @ and lives as long as the connection. Its name may contain letters, digits, _, . and $. A variable that was never set is NULL. := assigns inside an expression and returns the value; = assigns only in SET (in other places it is a comparison).

The value keeps what it is (a number stays a number, text stays text); the type of a variable is that of its last value.

SQL
SET @j = '{"a": 1}';
SELECT JSON_VALID(@j);           -- 1
SELECT @n := 5, @n + 1;          -- 5, 6

User variables can be used in PREPARE ... FROM @var and EXECUTE ... USING @a, @b (see Prepared statements). They cannot be used in a generated column, a functional index or a CHECK constraint (the statement is refused).

Local variables of stored programs#

SQL
DECLARE name [, name] ... type [DEFAULT expr];
SET name = expr;
SELECT ... INTO name [, name] ...;

Inside a stored procedure, function or trigger DECLARE makes a local variable that lives in its BEGIN ... END block. A local variable hides a user variable or column of the same name. The statements of a body are run by replacing the local variables in the statement text with their values, so a local variable cannot be used where only a name is allowed (after a ., before ( or ., as an alias, in the column list of INSERT, or as the target of UPDATE ... SET). In a trigger, NEW.col and OLD.col are variables of the trigger. See CREATE PROCEDURE and CREATE TRIGGER.

System variables#

SQL
@@name
@@session.name
@@local.name
@@global.name
SET [SESSION | GLOBAL] name = expr;
SET @@session.name = expr;
SET @@global.name = expr;
SHOW [SESSION | GLOBAL] VARIABLES [LIKE 'pattern' | WHERE expr];
  • @@name is the session value (and the global value if the variable has no session value).
  • Every variable of MySQL and MariaDB is known with its default value: 513 of MySQL 9.7 and 554 of MariaDB 11.4 (without the machine-specific ones), listed on System variables. A session starts with them, then with what Raft DB does differently (for example @@version, @@version_comment, @@sql_mode, @@transaction_isolation).
  • SET GLOBAL is kept for the whole front-end and seen by sessions that start after it. It needs the SUPER privilege, unless the connection is the configuration's administrator (error 1227 otherwise).
  • SET name = DEFAULT restores the default.
  • SET of a variable that does not exist is error 1193 (Unknown system variable 'x').
  • A switch variable is 1 or 0 in SELECT @@switch and ON or OFF in SHOW VARIABLES.
  • Only some variables change what Raft DB does. The ones that do: sql_mode (see SQL modes), autocommit, div_precision_increment (the digits a division adds to the scale of its dividend, default 4), group_concat_max_len (default 1024), foreign_key_checks, max_sp_recursion_depth, character_set_* and collation_* (accepted; every text is utf8mb4). Most of the others are stored and reported and change nothing.
  • @@transaction_isolation reports READ-COMMITTED (a session that writes a table inside a transaction holds it until the end): see Transactions.
SQL
SELECT @@sql_mode;
SET SESSION sql_mode = 'STRICT_TRANS_TABLES';
SET sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));
SET @@global.group_concat_max_len = 4096;

Dialect differences#

  • The default sql_mode and the default collation differ (see SQL modes and Collation and case sensitivity).
  • The list of variables is the list of the server of the dialect: a variable that exists only in MariaDB (histogram_type, ...) is unknown to the MySQL dialect and the other way round.
  • A MariaDB client sends SET sql_mode = CONCAT(@@sql_mode, ',STRICT_TRANS_TABLES'), NAMES utf8mb4 on connect; a NAMES item inside a SET list is read.

See also#