MySQL Data Types
This chapter provides a structured overview of MySQL data types, including numeric, string, temporal, and other specialized types. Understanding these types is essential for esigning efficient and reliable database schemas.
Numeric Types
Integer Types
MySQL provides several integer types (e.g., TINYINT, INT, BIGINT) to store whole numbers. Historically, these types allowed an optional display width (M).
Key points:
- The display width
(M)defines the minimum display width, not the storage size. - It is only meaningful when used with:
UNSIGNED: disallows negative valuesZEROFILL: pads the number with leading zeros to match widthM
- If the actual value exceeds
M, it is still stored correctly (no truncation).
⚠️ Important (MySQL 8+):
- As of MySQL 8.0.17, integer display width is deprecated and ignored.
1 | CREATE TABLE t_int( |
👉 Takeaway: Avoid using (M) in modern MySQL. Focus on choosing the correct integer type and constraints instead.
Floating-Point and Fixed-Point Types
MySQL supports two categories for storing decimal numbers:
Floating-Point Types
FLOAT(single precision)DOUBLE(double precision)
These types store approximate values and may introduce rounding errors due to binary representation.
Fixed-Point Type
DECIMAL
This type stores exact values and is ideal for financial or high-precision data.
Precision Definition (M, D)
M(precision): total number of digitsD(scale): number of digits after the decimal point- Must satisfy:
0 ≤ D ≤ M
Example:
DECIMAL(5,2)→ range:-999.99to999.99
Behavior:
- Extra decimal digits → rounded
- Excess integer digits → error (Out of range)
1 | CREATE TABLE t_double( |
👉 Best Practice:
- Use
DECIMALfor money and precise calculations - Use
FLOAT/DOUBLEfor scientific or approximate data
BIT Type
The BIT type is used to store binary values.
- Length range: 1 to 64 bits
- Storage is byte-aligned (minimum 1 byte)
Input formats:
- Integer (e.g.,
5) - Binary literal
- Hexadecimal
Important constraints:
- The value must fit within the defined number of bits
Display behavior:
- Returned as binary by default
- Use conversion functions for readability:
BIN()→ binary stringCONV()→ base conversion
1 | CREATE TABLE t_bit( |
👉 Use Case: Flags, bitmasks, and compact boolean storage.
String Types
Fixed-Length vs Variable-Length Strings
CHAR(M)
- Fixed-length string
- Range: 0–255 characters
- Right-padded with spaces if shorter than
M - Trailing spaces are removed when retrieved
VARCHAR(M)
- Variable-length string
- Stores only actual content
- Requires 1–2 extra bytes for length metadata
Maximum length depends on:
- Row size limit (65,535 bytes)
- Character set (e.g.,
utf8mb4uses up to 4 bytes per character)
1 | CREATE TABLE t_char ( |
Choosing Between CHAR and VARCHAR
- Use
CHARfor fixed-length data (e.g., country codes, IDs) - Use
VARCHARfor variable-length data (e.g., names, comments)
Storage engine considerations:
- MyISAM:
CHARmay improve read performance - InnoDB:
VARCHARis usually more space-efficient
ENUM and SET
These types restrict values to predefined lists.
ENUM
- Allows only one value
- Internally stored as an integer index
- Max: 65,535 values
SET
- Allows multiple values (combinations)
- Internally stored as bitmask values (powers of 2)
- Max: 64 members
1 | CREATE TABLE t_enum ( |
👉 Use Case:
- ENUM: status, category
- SET: tags, multi-select attributes
TEXT Types
TEXT types are used to store large amounts of text data (e.g., articles, logs).
Variants include:
TINYTEXTTEXTMEDIUMTEXTLONGTEXT
Binary String Types
Binary types store raw byte data rather than character data.
BINARY(M)
- Fixed-length binary data
- Padded with
\0if shorter thanM
VARBINARY(M)
- Variable-length binary data
- Requires length specification
- Includes additional bytes for length storage
👉 Use Case: Images, encrypted data, or non-text binary content.
Date and Time Types
MySQL provides several types for handling temporal data.
YEAR
- Format:
YYYY - Range: 1901–2155
- 2-digit format is deprecated
Special cases:
0→0000'0'→2000
DATE
- Format:
YYYY-MM-DD
TIME
- Format:
HH:MM:SS
DATETIME
- Stores date and time without timezone conversion
TIMESTAMP
- Range: 1970–2038 (UTC)
- Automatically converts between time zones
Key Difference
TIMESTAMP: timezone-awareDATETIME: timezone-independent
1 | CREATE TABLE t_date ( |
👉 Best Practice:
- Use
TIMESTAMPfor system-generated time (logs, updates) - Use
DATETIMEfor business data (appointments, schedules)
Other Data Types
JSON Type
- Supported since MySQL 5.7.8
- Enables structured document storage
- Provides built-in JSON functions for querying
Spatial Types
MySQL supports spatial (geographic) data based on OpenGIS standards.
Core type:
GEOMETRY
Common subtypes:
POINTLINESTRINGPOLYGON
Collection types:
MULTIPOINTMULTILINESTRINGMULTIPOLYGONGEOMETRYCOLLECTION
👉 Use Cases:
- Location-based services (maps, ride-sharing)
- Geofencing (e.g., attendance systems)
- Route and spatial analysis
Summary
- Choose data types based on precision, storage, and use case
- Prefer modern practices (avoid deprecated features like integer display width)
- Use appropriate types to balance performance, accuracy, and storage efficiency



