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#
SELECT @@sql_mode;
SET [SESSION | GLOBAL] sql_mode = 'mode [, mode] ...';
SET sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));Defaults#
| Dialect | Default sql_mode |
|---|---|
| MySQL | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
| MariaDB | STRICT_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#
| Mode | Effect |
|---|---|
STRICT_TRANS_TABLES, STRICT_ALL_TABLES, TRADITIONAL | Strict 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_DATE | the date 0000-00-00 is refused. Without it it is a date. |
NO_ZERO_IN_DATE | a date with a zero month or day (2024-00-10) is refused. Without it it is stored. |
ONLY_FULL_GROUP_BY | in 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#
| Value | Strict (error) | Non-strict (value stored, warning) |
|---|---|---|
| a whole number out of range | error 1264 Out of range value for column | the 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 column | error 1406 Data too long for column | cut to the length of the column |
| an invalid date, time, datetime | error 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 JSON | an error | an 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#
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_modeapplies from the next statement. - A MariaDB connector sets
STRICT_TRANS_TABLESon 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).