Integer types
TINYINT to BIGINT with UNSIGNED, BOOL, SERIAL and AUTO_INCREMENT: ranges, behaviour at the limits and the differences between the dialects.
Syntax#
{ TINYINT | SMALLINT | MEDIUMINT | INT | INTEGER | BIGINT } [(display_width)] [UNSIGNED | SIGNED] [ZEROFILL]
BOOL | BOOLEAN
SERIALRanges#
| Type | Bytes | Signed | Unsigned |
|---|---|---|---|
TINYINT | 1 | -128 to 127 | 0 to 255 |
SMALLINT | 2 | -32768 to 32767 | 0 to 65535 |
MEDIUMINT | 3 | -8388608 to 8388607 | 0 to 16777215 |
INT, INTEGER | 4 | -2147483648 to 2147483647 | 0 to 4294967295 |
BIGINT | 8 | -9223372036854775808 to 9223372036854775807 | 0 to 18446744073709551615 |
Remarks#
BOOLandBOOLEANareTINYINT(1).TRUEis 1 andFALSEis 0; any number is stored as given (aBOOLcan hold 5).SERIALisBIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE. AnAUTO_INCREMENTcolumn 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. InsertingNULLor 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 UNSIGNEDabove 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 bySHOW CREATE TABLE(int(11),bigint(20),tinyint(4),smallint(6),mediumint(9)); MySQL 8 does not show it (except fortinyint(1)). It does not change the range.ZEROFILLis accepted and ignored. - Result type of an expression.
+ - *on whole numbers isINTwhile the width of the result is at most 11 andBIGINTabove;/is aDECIMAL;DIVand%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#
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