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#
'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.
| Escape | Meaning |
|---|---|
\' \" \\ | the character itself |
\n \t \r | newline, tab, carriage return |
\0 | the zero byte |
\b | backspace |
\Z | control-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 inLIKE; Raft DB does not, so write\\%to match a percent sign withLIKE. - The mode
NO_BACKSLASH_ESCAPESis accepted insql_modeand 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 isutf8mb4._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.
SELECT 'it''s', "say \"hi\"", 'line1\nline2';Numeric literals#
42 -17 3.14 .5 1e3 1.5E-3- A whole number is an
INTorBIGINT. A whole number that does not fit 64 bits (up to 18446744073709551615 forBIGINT UNSIGNEDand 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
DECIMALthat remembers how it was written (1.50has two decimals). Arithmetic on whole numbers and decimals is exact up to 128 bits. - A number with an exponent (
1e3) is aDOUBLE. - A minus sign in front of a number is the unary minus operator, folded into the number.
SELECT 18446744073709551615, 1.50 + 1, 1e3, 7 DIV 2;Hexadecimal literals#
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#
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.
| Type | Form read | Notes |
|---|---|---|
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 |
YEAR | 2024 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).
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#
SELECT 'abc' = 'ABC'; -- 1: text comparison ignores case
SELECT 0x41 + 0, b'11' + 0; -- 65, 3
SELECT TRUE, FALSE, NULL; -- 1, 0, NULL