SQL reference (MySQL and MariaDB dialects)

Integer types

TINYINT to BIGINT with UNSIGNED, BOOL, SERIAL and AUTO_INCREMENT: ranges, behaviour at the limits and the differences between the dialects.

Syntax#

SQL
{ TINYINT | SMALLINT | MEDIUMINT | INT | INTEGER | BIGINT } [(display_width)] [UNSIGNED | SIGNED] [ZEROFILL]
BOOL | BOOLEAN
SERIAL

Ranges#

TypeBytesSignedUnsigned
TINYINT1-128 to 1270 to 255
SMALLINT2-32768 to 327670 to 65535
MEDIUMINT3-8388608 to 83886070 to 16777215
INT, INTEGER4-2147483648 to 21474836470 to 4294967295
BIGINT8-9223372036854775808 to 92233720368547758070 to 18446744073709551615

Remarks#

  • BOOL and BOOLEAN are TINYINT(1). TRUE is 1 and FALSE is 0; any number is stored as given (a BOOL can hold 5).
  • SERIAL is BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE. An AUTO_INCREMENT column may lead a unique key of a table that has no primary key.
  • AUTO_INCREMENT. A column with it must be a key (error 1075 otherwise) and a table has at most one. Inserting NULL or 0 takes the next value; inserting a larger value moves the counter on; a smaller value does not move it back. LAST_INSERT_ID() returns the first value a statement generated.
  • UNSIGNED. A negative value for an unsigned column is out of range. Arithmetic on unsigned operands gives an unsigned result when both sides are unsigned or literal. MySQL's error 1690 (BIGINT UNSIGNED value is out of range) when an unsigned subtraction goes below zero is not raised.
  • Values beyond 64 bits. A whole number beyond signed 64 bits (a BIGINT UNSIGNED above 9223372036854775807, or a literal) keeps all its digits: it is kept as its decimal text and compared and ordered by the front-end, not by Raft DB's own indexes. Whole-number and decimal arithmetic is exact up to 128 bits.
  • Display width (INT(11)) is read and, in the MariaDB dialect, shown by SHOW CREATE TABLE (int(11), bigint(20), tinyint(4), smallint(6), mediumint(9)); MySQL 8 does not show it (except for tinyint(1)). It does not change the range. ZEROFILL is accepted and ignored.
  • Result type of an expression. + - * on whole numbers is INT while the width of the result is at most 11 and BIGINT above; / is a DECIMAL; DIV and % are whole numbers (see Expressions and operator precedence).
  • Out of range. In strict mode (the default) a value outside the range is error 1264 (The value for column 'c' (row 1) is outside the allowed range); a text that is not a number is error 1366 (The integer value '...' given for column ... is not valid). Without strict mode the value is made to fit and a warning is raised: the nearest bound for a number, the leading digits (or 0) for a text. A decimal such as 2.5 is rounded half away from zero when it goes into an integer column. See SQL modes.

Examples#

SQL
CREATE TABLE s (id SERIAL, v INT);                 -- MariaDB
CREATE TABLE seq (id INT AUTO_INCREMENT PRIMARY KEY, v VARCHAR(5));
INSERT INTO seq (v) VALUES ('a');
INSERT INTO seq (id, v) VALUES (10, 'b');
INSERT INTO seq (v) VALUES ('c');                  -- gets 11
INSERT INTO types (ti) VALUES (128);               -- strict: error 1264 (TINYINT holds up to 127)
INSERT INTO types (u8) VALUES (-1);                -- strict: error 1264

See also#