How to choose data types for columns
· Category: SQL & Databases
Short answer
Choose the smallest data type that can represent your domain accurately. Prefer fixed-length types for known sizes, use integers for IDs, and decimals for money.
Steps
- Use
INTorBIGINTfor auto-incrementing IDs based on expected cardinality. - Use
VARCHARfor strings with variable length andCHARfor fixed codes. - Use
DECIMALorNUMERICfor monetary values; avoid floating-point types. - Use
DATE,TIME, orTIMESTAMPfor temporal data. - Use
BOOLEANorTINYINTfor flags, depending on database support.
Tips
- Align data types across related tables to enable efficient joins without implicit conversions.
- Consider future growth; switching a column from
INTtoBIGINTlater can be expensive.
Common issues
- Using
TEXTorBLOBfor short strings wastes memory and disables some indexing features. - Storing dates as strings prevents date arithmetic and efficient range queries.r