TINYINT

Integer from -128 to 127 when signed, or from 0 to 255 when unsigned.

See also: Data Types in 10.6 ES, in 10.6 CS, in 10.5 ES, in 10.5 CS, in 10.4 ES, in 10.4 CS, in 10.3 ES, in 10.3 CS, in 10.2 ES, and in 10.2 CS

DETAILS

SYNONYMS

The following are synonyms for TINYINT:

  • BOOL

  • BOOLEAN

  • INT1

EXAMPLES

SIGNED and UNSIGNED

The TINYINT data type may be SIGNED (allowing negative values) or UNSIGNED (not allowing negative values).

Example of TINYINT SIGNED:

CREATE TABLE tinyint_signed_example (
  description VARCHAR(20),
  example TINYINT SIGNED
);
INSERT INTO tinyint_signed_example VALUES
  ('Zero', 0),
  ('Forty-Two', 42),
  ('Minimum', -128),
  ('Maximum', 127);

Example of TINYINT UNSIGNED:

CREATE TABLE tinyint_unsigned_example (
  description VARCHAR(20),
  example TINYINT UNSIGNED
);
INSERT INTO tinyint_unsigned_example VALUES
  ('Zero', 0),
  ('Forty-Two', 42),
  ('Minimum', 0),
  ('Maximum', 255);

Out-of-Range

A value is considered "out-of-range" when it is too small or too large to be stored in a data type. When SQL_MODE is strict (the default) an out-of-range value generates an error. If strict mode is not in effect, the value is rounded to the nearest valid value and a warning is generated (which might be hidden, depending on your warning settings).

Example of non-strict out-of-range behavior:

TRUNCATE tinyint_signed_example;
TRUNCATE tinyint_unsigned_example;

-- Disable strict mode or the inserts will fail
SET sql_mode=(SELECT REPLACE(@@sql_mode, 'STRICT_TRANS_TABLES', ''));

INSERT INTO tinyint_signed_example VALUES
  ('Underflow', -129),
  ('Overflow', 128);

INSERT INTO tinyint_unsigned_example VALUES
  ('Underflow', -1),
  ('Overflow', 256);
Warning (Code 1264): Out of range value for column 'example' at row 1
Warning (Code 1264): Out of range value for column 'example' at row 2
Warning (Code 1264): Out of range value for column 'example' at row 1
Warning (Code 1264): Out of range value for column 'example' at row 2

The resulting data would look like this:

SELECT * FROM tinyint_signed_example;
+-------------+---------+
| description | example |
+-------------+---------+
| Underflow   |    -128 |
| Overflow    |     127 |
+-------------+---------+
SELECT * FROM tinyint_unsigned_example;
+-------------+---------+
| description | example |
+-------------+---------+
| Underflow   |       0 |
| Overflow    |     255 |
+-------------+---------+

TINYINT ZEROFILL

A special type of TINYINT UNSIGNED is TINYINT ZEROFILL, which pads out the values with leading zeros in SELECT results. The number of leading zeros are just enough to pad the field out to the length of the type's maximum unsigned value, but the zeros are not included in an expression result or in a UNION SELECT column.

Using TINYINT ZEROFILL works the same way as TINYINT UNSIGNED for most operations except a simple SELECT. For example, with the following test table setup:

CREATE TABLE tinyint_zerofill_example (
  description VARCHAR(20),
  example TINYINT ZEROFILL
);
INSERT INTO tinyint_zerofill_example VALUES
  ('Zero', 0),
  ('Forty-Two', 42),
  ('Minimum', 0),
  ('Maximum', 255);

-- Turn off strict mode or the inserts will fail
SET sql_mode=(SELECT REPLACE(@@sql_mode, 'STRICT_TRANS_TABLES', ''));

INSERT INTO tinyint_zerofill_example VALUES
  ('Underflow', -1),
  ('Overflow', 256);
Warning (Code 1264): Out of range value for column 'example' at row 1
Warning (Code 1264): Out of range value for column 'example' at row 2

The resulting data would look like this:

SELECT *, example + 0 FROM tinyint_zerofill_example;
+-------------+---------+-------------+
| description | example | example + 0 |
+-------------+---------+-------------+
| Zero        |     000 |           0 |
| Forty-Two   |     042 |          42 |
| Minimum     |     000 |           0 |
| Maximum     |     255 |         255 |
| Underflow   |     000 |           0 |
| Overflow    |     255 |         255 |
+-------------+---------+-------------+

CHANGE HISTORY

Release Series

History

10.6 Enterprise

  • Present starting in MariaDB Enterprise Server 10.6.4-1.

10.6 Community

  • Present starting in MariaDB Community Server 10.6.0.

10.5 Enterprise

  • Present starting in MariaDB Enterprise Server 10.5.3-1.

10.5 Community

  • Present starting in MariaDB Community Server 10.5.0.

10.4 Enterprise

  • Present starting in MariaDB Enterprise Server 10.4.6-1.

10.4 Community

  • Present starting in MariaDB Community Server 10.4.0.

10.3 Enterprise

  • Present starting in MariaDB Enterprise Server 10.3.16-1.

10.3 Community

  • Present starting in MariaDB Community Server 10.3.0.

10.2 Enterprise

  • Present starting in MariaDB Enterprise Server 10.2.25-1.

10.2 Community

  • Present starting in MariaDB Community Server 10.2.0.

EXTERNAL REFERENCES