SQL reference (MySQL and MariaDB dialects)

SQL modes

The sql_mode variable: its default in each dialect, which modes change what Raft DB does (strict, zero dates, ONLY_FULL_GROUP_BY) and which are only stored.

sql_mode is a system variable with a comma-separated list of modes. It changes how Raft DB checks values and reads some statements.

Syntax#

SQL
SELECT @@sql_mode;
SET [SESSION | GLOBAL] sql_mode = 'mode [, mode] ...';
SET sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));

Defaults#

DialectDefault sql_mode
MySQLONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
MariaDBSTRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

When you set the variable, Raft DB writes the list as the server does: upper case, no spaces or empty entries, each mode once, in the server's order. A number (the bit form) is kept as given.

Modes that change what Raft DB does#

ModeEffect
STRICT_TRANS_TABLES, STRICT_ALL_TABLES, TRADITIONALStrict mode (any one of them, Raft DB does not tell transactional from non-transactional tables): a value that does not fit its column is an error. Without it, the value is made to fit and a warning is raised (see below).
NO_ZERO_DATEthe date 0000-00-00 is refused. Without it it is a date.
NO_ZERO_IN_DATEa date with a zero month or day (2024-00-10) is refused. Without it it is stored.
ONLY_FULL_GROUP_BYin a grouped query, every column of the select list, HAVING and ORDER BY outside an aggregate must be a grouping column (or depend on one); otherwise the statement is refused as MySQL refuses it.

Modes that are stored and have no effect#

ERROR_FOR_DIVISION_BY_ZERO (division by zero gives NULL in every mode; MySQL makes it an error in an INSERT in strict mode, Raft DB does not), NO_ENGINE_SUBSTITUTION, NO_AUTO_CREATE_USER, ANSI_QUOTES, PIPES_AS_CONCAT, IGNORE_SPACE, NO_BACKSLASH_ESCAPES, HIGH_NOT_PRECEDENCE, NO_UNSIGNED_SUBTRACTION, NO_AUTO_VALUE_ON_ZERO, ALLOW_INVALID_DATES, REAL_AS_FLOAT, ANSI, PAD_CHAR_TO_FULL_LENGTH, TIME_TRUNCATE_FRACTIONAL, EMPTY_STRING_IS_NULL, ORACLE and the other combined and legacy names. They are kept in the variable and shown by SELECT @@sql_mode, so that tools that set them do not fail.

Strict and non-strict mode#

ValueStrict (error)Non-strict (value stored, warning)
a whole number out of rangeerror 1264 Out of range value for columnthe nearest value the column holds (TINYINT 128 becomes 127)
text for a number column ('', 'abc')error 1366 (The integer value '...' given for column ... is not valid, and the same for double and decimal)the leading digits, or 0 ('' into an INT column is 0)
text longer than the columnerror 1406 Data too long for columncut to the length of the column
an invalid date, time, datetimeerror 1292 (The date value '...' given for column ... is not valid, SQLSTATE 22007)the zero value of the type, warning 1265 (The value stored in column ... was cut off)
a BIT value with too many bits, a bad ENUM or SET member, bad JSONan erroran error: these stay errors in non-strict mode

After a statement in non-strict mode SHOW WARNINGS lists the warnings (for example INSERT INTO b VALUES ('', 'abc', 'x') with the mode set to the empty string).

Examples#

SQL
SET SESSION sql_mode = '';
INSERT INTO a (id) VALUES ('');            -- stored as 0, with a warning
SHOW WARNINGS;
SET SESSION sql_mode = 'STRICT_TRANS_TABLES';
INSERT INTO a (id) VALUES ('');            -- error 1366
SET sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));
SELECT @@sql_mode;

Remarks#

  • The session reads the mode again before every statement, so a SET sql_mode applies from the next statement.
  • A MariaDB connector sets STRICT_TRANS_TABLES on connect (SET sql_mode = CONCAT(@@sql_mode, ',STRICT_TRANS_TABLES')); the mode is kept as the server would keep it.
  • Differences between MySQL and MariaDB: the default list (above), and MariaDB truncates the fractional digits of a time where MySQL rounds them (see Date and time types).

See also#