SQL reference (MySQL and MariaDB dialects)

Literals

How to write strings, numbers, hexadecimal and bit values, booleans, NULL and dates in a statement, and what Raft DB does differently from MySQL and MariaDB.

A literal is a value written in a statement. This page covers the forms Raft DB's lexer reads. The same lexer serves the MySQL and the MariaDB dialect; the places where the two differ are marked.

String literals#

SQL
'text'
"text"
_utf8mb4 'text'
_binary 'bytes'

A string is written between single quotes or between double quotes. (Raft DB does not have the ANSI_QUOTES mode: a double-quoted word is always a string, never a name.) To put the quote character inside the string, double it ('it''s') or put a backslash before it.

EscapeMeaning
\' \" \\the character itself
\n \t \rnewline, tab, carriage return
\0the zero byte
\bbackspace
\Zcontrol-Z (code 26)

Remarks:

  • A backslash before any other character gives that character without the backslash. MySQL keeps the backslash in \% and \_ so that they can be used in LIKE; Raft DB does not, so write \\% to match a percent sign with LIKE.
  • The mode NO_BACKSLASH_ESCAPES is accepted in sql_mode and has no effect.
  • Two string literals next to each other are not joined; use CONCAT.
  • A character set introducer in front of a string (_utf8mb4 'x', _latin1 'x') is read and dropped: every text in Raft DB is utf8mb4. _binary 'x' makes a binary string (the bytes of the text).
  • A statement must be valid UTF-8 text: bytes that are not are refused with error 1366 instead of being changed on their way in.
SQL
SELECT 'it''s', "say \"hi\"", 'line1\nline2';

Numeric literals#

SQL
42        -17        3.14        .5        1e3        1.5E-3
  • A whole number is an INT or BIGINT. A whole number that does not fit 64 bits (up to 18446744073709551615 for BIGINT UNSIGNED and beyond, as a decimal) keeps all its digits: the literal is an exact decimal.
  • A number with a decimal point and no exponent is an exact DECIMAL that remembers how it was written (1.50 has two decimals). Arithmetic on whole numbers and decimals is exact up to 128 bits.
  • A number with an exponent (1e3) is a DOUBLE.
  • A minus sign in front of a number is the unary minus operator, folded into the number.
SQL
SELECT 18446744073709551615, 1.50 + 1, 1e3, 7 DIV 2;

Hexadecimal literals#

SQL
0x41424344
X'41424344'
x'4142'

A hexadecimal literal is a string of bytes. In a number context it is a number: 0xAA + 0 is 170 in both dialects, and CAST(0xAA AS UNSIGNED) is 170. A X'..' literal must have an even number of digits.

The two dialects differ for X'..' as MySQL and MariaDB do: the MariaDB dialect reads it as a string when its bytes are valid UTF-8 (so X'0a'+0 is 0), the MySQL dialect reads it as a number in arithmetic (X'0a'+0 is 10).

Bytes that are not valid UTF-8 cannot be stored yet (see Binary types).

Bit-value literals#

SQL
b'101'
B'0101'

A bit literal is written with the letter b and a string of 0 and 1. Raft DB reads it as the whole number it spells (b'101' is 5). MySQL gives a binary string that prints as bytes; the number behaves the same in comparisons, arithmetic and when stored into a BIT column (see BIT type).

Boolean literals and NULL#

TRUE and FALSE are the numbers 1 and 0. NULL is the missing value; see NULL handling. The words are not case-sensitive.

Date and time literals#

A date or time is written as a string and converted to the type of the column or function that receives it.

TypeForm readNotes
DATE'YYYY-MM-DD'years below 1000 are refused (except the zero date)
DATETIME, TIMESTAMP'YYYY-MM-DD HH:MM:SS[.ffffff]'a T instead of the space is read; no time part means 00:00:00
TIME'[-]H:MM:SS[.ffffff]'hours up to 838, minutes and seconds up to 59
YEAR2024 or '2024'1901 to 2155, and 0

Not read: the typed forms DATE '2024-01-01', TIME '10:00:00', TIMESTAMP '...', the ODBC forms {d '2024-01-01'}, and the forms without separators ('20240101', 20240101). A value written with INTERVAL is not a literal but an expression (see Date and time functions).

SQL
INSERT INTO t (dt, ts, tm, y) VALUES ('2024-02-29', '2024-02-29 13:14:15', '12:34:56', 2024);

Executable comments#

Text between /*! and */ is read as code, so that mysqldump files load (/*!40101 SET NAMES utf8 */). See Comments.

Examples#

SQL
SELECT 'abc' = 'ABC';         -- 1: text comparison ignores case
SELECT 0x41 + 0, b'11' + 0;   -- 65, 3
SELECT TRUE, FALSE, NULL;     -- 1, 0, NULL

See also#