SQL Data Types
SQL Data Types
SQL data types define what kind of values a column can store. Choosing the right data types improves data quality, reduces storage, and can improve performance.
Common Categories
- Numeric types (INT, DECIMAL, FLOAT)
- String types (CHAR, VARCHAR, TEXT)
- Date and time types (DATE, DATETIME, TIMESTAMP)
- Boolean-like types (often implemented as TINYINT)
Integer Types
Use integer types for whole numbers (ids, counters, quantities).
id INT AUTO_INCREMENT PRIMARY KEY
Exact Numbers (DECIMAL)
Use DECIMAL for money and values that require exact precision.
price DECIMAL(10,2) NOT NULL
Approximate Numbers (FLOAT/DOUBLE)
FLOAT and DOUBLE are approximate and can introduce rounding errors. Use them for scientific measurements, not money.
String Types
- CHAR(n) – fixed-length text (good for fixed-size codes)
- VARCHAR(n) – variable-length text (most common choice)
- TEXT – larger text content
Example: VARCHAR vs TEXT
name VARCHAR(100) NOT NULL, description TEXT
Date and Time Types
- DATE – stores a date (YYYY-MM-DD)
- DATETIME – stores date and time
- TIMESTAMP – stores date and time (behavior depends on database/timezone settings)
Example: Timestamps
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
Boolean Values
Some databases have a BOOLEAN type, but in MySQL/MariaDB it is commonly stored as TINYINT(1).
is_published TINYINT(1) NOT NULL DEFAULT 0
Choosing the Right Size
Do not oversize columns. For example, using VARCHAR(5000) for short values wastes resources and can hurt indexing.
Character Set (utf8mb4)
Use utf8mb4 for full Unicode support. Make sure your database and tables use compatible collation.
Common Mistakes
- Storing numbers as text
- Using FLOAT for money
- Using very large VARCHAR sizes without need
- Ignoring character set and collation
Best Practice
Choose the smallest appropriate type that fits your data. Use constraints (NOT NULL, UNIQUE) to enforce quality and use indexes for columns frequently searched or joined.
Next Step
You have completed the SQL Database section. Continue with SQL References for keywords and function quick references (optional).