TExcelTAD
Home › MySQL › MySQL Data Types

MySQL Data Types

Every column has a data type. It decides which values the column accepts, how much space they take and how they compare and sort. Choosing good types is the cheapest way to keep data clean: a DATE column can't contain "next Tuesday", and an INT column can't contain "twelve".

Numbers

Integers

Type Bytes Signed range Unsigned range
TINYINT 1 -128 to 127 0 to 255
SMALLINT 2 -32,768 to 32,767 0 to 65,535
MEDIUMINT 3 about ±8.4 million 0 to 16,777,215
INT 4 about ±2.1 billion 0 to 4,294,967,295
BIGINT 8 about ±9.2 × 10^18 0 to 1.8 × 10^19

Add UNSIGNED for columns that can never be negative (ids, quantities). A value outside the range is rejected in strict mode:

mysql · try it yourself
CREATE TABLE stock_levels (product_id INT UNSIGNED, on_hand SMALLINT, bins TINYINT UNSIGNED);
INSERT INTO stock_levels VALUES (1, 32000, 255);
SELECT * FROM stock_levels;
INSERT INTO stock_levels VALUES (2, 40000, 1);
+------------+---------+------+
| product_id | on_hand | bins |
+------------+---------+------+
|          1 |   32000 |  255 |
+------------+---------+------+
ERROR 1264 (22003) at line 4: Out of range value for column 'on_hand' at row 1

Note: You may see INT(11) in older schemas. The number is only a display width hint, not a size limit, and MySQL 8 deprecated it. INT(3) still stores values up to about 2.1 billion. The one common exception is TINYINT(1), which tools treat as a boolean.

Exact decimals and floating point

Type Use for
DECIMAL(p, s) exact values: money, quantities with fixed decimals. p digits in total, s after the point. DECIMAL(8,2) holds up to 999999.99
FLOAT, DOUBLE approximate scientific values, measurements

Floating point numbers are stored in binary, so most decimal fractions can't be represented exactly. Never use them for money:

mysql · try it yourself
SELECT 0.1 + 0.2 AS decimal_math,
       0.1e0 + 0.2e0 AS double_math,
       CAST(19.999 AS DECIMAL(6,2)) AS rounded_to_scale;
+--------------+---------------------+------------------+
| decimal_math | double_math         | rounded_to_scale |
+--------------+---------------------+------------------+
|          0.3 | 0.30000000000000004 |            20.00 |
+--------------+---------------------+------------------+

The e0 makes the literals DOUBLE values; without it, MySQL treats 0.1 as an exact decimal.

Text

Type Holds
CHAR(n) exactly n characters, padded with spaces (codes, fixed-length values)
VARCHAR(n) up to n characters (names, e-mails, most text); n up to 65,535 bytes per row in total
TEXT up to 64 KB of text (descriptions, comments); also TINYTEXT, MEDIUMTEXT (16 MB), LONGTEXT (4 GB)
BINARY(n), VARBINARY(n), BLOB raw bytes instead of characters
ENUM('a', 'b', ...) one value from a fixed list
SET('a', 'b', ...) any combination of values from a fixed list

The n counts characters, not bytes, and a utf8mb4 character takes up to 4 bytes:

mysql · try it yourself
SELECT LENGTH('Café') AS bytes, CHAR_LENGTH('Café') AS characters;
+-------+------------+
| bytes | characters |
+-------+------------+
|     5 |          4 |
+-------+------------+

CHAR strips trailing spaces when you read it; VARCHAR keeps them:

mysql · try it yourself
CREATE TABLE codes (fixed CHAR(5), flexible VARCHAR(5));
INSERT INTO codes VALUES ('ab  ', 'ab  ');
SELECT CONCAT('[', fixed, ']') AS char_value, CONCAT('[', flexible, ']') AS varchar_value FROM codes;
+------------+---------------+
| char_value | varchar_value |
+------------+---------------+
| [ab]       | [ab  ]        |
+------------+---------------+

ENUM and SET

ENUM stores one of a list of allowed strings very compactly (as a number internally); SET stores any combination:

mysql · try it yourself
CREATE TABLE shirts (
  shirt_id INT PRIMARY KEY,
  size     ENUM('S', 'M', 'L', 'XL') NOT NULL,
  features SET('organic', 'recycled', 'vegan')
);
INSERT INTO shirts VALUES (1, 'M', 'vegan,organic'), (2, 'XL', ''), (3, 'S', 'recycled');
SELECT shirt_id, size, size + 0 AS size_index, features FROM shirts ORDER BY size;
+----------+------+------------+---------------+
| shirt_id | size | size_index | features      |
+----------+------+------------+---------------+
|        3 | S    |          1 | recycled      |
|        1 | M    |          2 | organic,vegan |
|        2 | XL   |          4 |               |
+----------+------+------------+---------------+

Note that ORDER BY size sorts in the list order (S, M, L, XL), not alphabetically, and SET values come back in the order the list defines. Changing an ENUM's list requires ALTER TABLE, which is why many designers prefer a small lookup table with a foreign key.

Dates and times

DATE, DATETIME, TIMESTAMP, TIME and YEAR are covered in the Dates lesson.

Booleans

MySQL has no real boolean type: BOOL and BOOLEAN are synonyms for TINYINT(1), and TRUE/FALSE are just 1 and 0:

mysql · try it yourself
SELECT TRUE, FALSE, TRUE + TRUE AS two, 5 > 3 AS comparison;
+------+-------+-----+------------+
| TRUE | FALSE | two | comparison |
+------+-------+-----+------------+
|    1 |     0 |   2 |          1 |
+------+-------+-----+------------+

JSON

JSON columns hold JSON documents (objects, arrays, values). MySQL validates the document and stores it in an efficient binary format; MariaDB accepts the same declaration but stores the text in a LONGTEXT column with a validity check. The JSON Functions lesson shows how to work with them.

Choosing types: quick guide

Data Good type
surrogate id INT UNSIGNED AUTO_INCREMENT (or BIGINT UNSIGNED)
money DECIMAL(10,2)
name, e-mail, title VARCHAR(n) with a realistic n
long free text TEXT
yes/no flag BOOLEAN (TINYINT(1))
date of birth, order date DATE
moment something happened DATETIME or TIMESTAMP
country code CHAR(2)

Test yourself

Which type should store prices?

  1. FLOAT
  2. DOUBLE
  3. DECIMAL(10,2)
  4. VARCHAR(10)

What does the 11 in INT(11) mean?

  1. At most 11 digits can be stored
  2. It is a deprecated display width with no effect on range
  3. 11 bytes of storage
  4. The default value

What does CHAR_LENGTH('Café') return with utf8mb4?

  1. 4
  2. 5
  3. 8
  4. 16