Common MySQL Data Types and Selection Guidelines
This article explains MySQL's various numeric, date/time, and string data types, discusses their characteristics such as signed/unsigned integers, floating‑point precision, fixed‑point accuracy, and provides practical best‑practice recommendations for choosing optimal types in database design.
Choosing appropriate data types is crucial in database modeling; improper choices can cause unmet requirements, lack of scalability, and poor performance.
MySQL offers a rich set of data types, including numeric, date/time, and string types.
1. Numeric Types
Numeric types include integer, floating‑point, and fixed‑point types.
Integer types: TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT. They can be signed or unsigned; using UNSIGNED doubles the maximum value (e.g., unsigned SMALLINT ranges up to 65,535).
Floating‑point types: FLOAT (single‑precision, 4 bytes) and DOUBLE (double‑precision, 8 bytes). They store approximate values and are unsuitable for high‑precision calculations such as monetary amounts.
Fixed‑point type: DECIMAL (e.g., DECIMAL(5,2) stores values from –999.99 to 999.99). NUMERIC is an alias implemented with DECIMAL and can be used interchangeably.
2. Date and Time Types
Date/time types include DATE, DATETIME, TIMESTAMP, TIME, YEAR. The TIME type can store values from ‘‑838:59:59’ to ‘838:59:59’, which explains its unusually large range.
3. String Types
String types are divided into text strings, binary strings, and enumeration types.
Text strings: CHAR, VARCHAR, and TEXT families (TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT). CHAR has a fixed length; VARCHAR stores length plus data, saving space when the actual content is shorter.
Binary strings: BINARY, VARBINARY, and BLOB families (TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB) for storing binary data such as images, audio, or video. They are case‑sensitive during comparison and sorting.
Enumeration types: ENUM and SET. ENUM stores a single predefined value, while SET allows multiple selections from a predefined list.
4. Summary and Best Practices
Prefer smaller types when possible (e.g., use TINYINT instead of SMALLINT). Use DECIMAL for monetary values rather than FLOAT or DOUBLE. Favor CHAR over VARCHAR when the length is fixed, and VARCHAR over TEXT for moderate‑size strings. TIMESTAMP is generally better than DATETIME because it occupies less space and stores UTC time, automatically handling time‑zone conversion.
When designing primary keys, consider the trade‑offs of using VARCHAR as a primary key, weighing its flexibility against potential performance impacts.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.