Table of Contents
- Introduction
- Why Data Types Matter in SQL
- Overview of SQL Data Type Categories
- Integer Types
- Decimal and Floating-Point Types
- Character and String Types
- Date and Time Types
- Boolean Type
- Binary and Blob Types
- ENUM and SET (MySQL Specific)
- Serial and Auto-Incrementing Types
- Choosing the Right Data Type
- Data Type Compatibility Across Databases
- Implicit and Explicit Type Casting
- NULL Values and Data Types
- Performance Implications of Data Types
- Data Type Errors and Troubleshooting
- Best Practices for Using Data Types
- Real-World Schema Example
- Summary and Next Steps
1. Introduction
In SQL, every column in a table must be assigned a data type. Data types determine what kind of data can be stored in each column — numbers, text, dates, boolean values, and more. Choosing the right data type is essential for data integrity, performance, and storage efficiency.
2. Why Data Types Matter in SQL
- Validation: Prevents incorrect data from being stored
- Storage Optimization: Saves space with efficient types
- Performance: Speeds up indexing and searching
- Data Integrity: Helps avoid logic and arithmetic errors
- Compatibility: Ensures consistency when exporting/importing data
3. Overview of SQL Data Type Categories
Category | Common Data Types |
---|---|
Numeric | INT, BIGINT, DECIMAL, FLOAT |
Character/String | CHAR, VARCHAR, TEXT |
Date and Time | DATE, TIME, TIMESTAMP, DATETIME |
Boolean | BOOLEAN, BOOL |
Binary | BLOB, BYTEA |
Special/Other | ENUM, SET, SERIAL |
4. Integer Types
Used to store whole numbers.
Data Type | Range | Notes |
---|---|---|
TINYINT | -128 to 127 | Small storage (1 byte) |
SMALLINT | -32,768 to 32,767 | Good for small numbers |
INT | -2^31 to 2^31-1 | Common choice for integers |
BIGINT | Very large | Up to 2^63-1, useful for IDs |
Example:
age INT;
population BIGINT;
5. Decimal and Floating-Point Types
Used for storing numbers with decimals.
- DECIMAL(p, s): Precise fixed-point values.
- FLOAT/REAL/DOUBLE: Approximate values, prone to rounding errors.
Example:
price DECIMAL(10, 2); -- 99999999.99
weight FLOAT;
Use DECIMAL
for financial calculations; FLOAT
for scientific or approximate values.
6. Character and String Types
Type | Description |
---|---|
CHAR(n) | Fixed length, pads with spaces |
VARCHAR(n) | Variable length, up to n characters |
TEXT | Long text blocks (blogs, descriptions) |
Example:
username VARCHAR(50);
postal_code CHAR(6);
bio TEXT;
Avoid using TEXT
unless necessary — it lacks indexing in some systems.
7. Date and Time Types
Type | Description |
---|---|
DATE | Only date (YYYY-MM-DD) |
TIME | Only time (HH:MM:SS) |
DATETIME | Date and time |
TIMESTAMP | Unix timestamp + time zone support |
Example:
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
birthday DATE;
PostgreSQL supports more options like INTERVAL
, TIME WITH TIME ZONE
.
8. Boolean Type
Stores true or false values.
is_active BOOLEAN;
- In MySQL: stored as
TINYINT(1)
behind the scenes - In PostgreSQL: true
BOOLEAN
type
9. Binary and Blob Types
Used to store binary data like images, PDFs, or files.
Type | Description |
---|---|
BLOB | Binary Large Object (MySQL) |
BYTEA | Binary type (PostgreSQL) |
Avoid using binary types for large media — use external storage + reference.
10. ENUM and SET (MySQL Specific)
ENUM
status ENUM('active', 'inactive', 'pending');
SET
tags SET('blog', 'news', 'video');
They restrict allowed values and save space but reduce portability.
11. Serial and Auto-Incrementing Types
Used for primary key columns that auto-increment:
SQLite:
id INTEGER PRIMARY KEY AUTOINCREMENT
MySQL:
id INT AUTO_INCREMENT PRIMARY KEY
PostgreSQL:
id SERIAL PRIMARY KEY
12. Choosing the Right Data Type
- For IDs: use
INT
orBIGINT
withAUTO_INCREMENT
orSERIAL
- For names/emails:
VARCHAR(100)
- For monetary values:
DECIMAL(10, 2)
- For status flags:
BOOLEAN
orENUM
- For timestamps:
TIMESTAMP
Avoid over-allocating space — use just enough to fit expected data.
13. Data Type Compatibility Across Databases
SQL Standard | SQLite | MySQL | PostgreSQL |
---|---|---|---|
INT | INTEGER | INT | INTEGER |
VARCHAR(n) | TEXT (dynamic) | VARCHAR(n) | VARCHAR(n) |
BOOLEAN | NUMERIC (0/1) | TINYINT(1) | BOOLEAN |
TIMESTAMP | TEXT or NUM | TIMESTAMP | TIMESTAMP |
SQLite uses dynamic typing, so types are more relaxed.
14. Implicit and Explicit Type Casting
SQL can convert data types:
SELECT '123' + 1; -- Implicit cast to integer
Use CAST()
or ::
(PostgreSQL):
SELECT CAST('2024-01-01' AS DATE);
SELECT '10'::INT; -- PostgreSQL
15. NULL Values and Data Types
NULL
means the value is unknown or not applicable.
age INT NULL;
Be cautious: NULL != 0
and NULL != ''
Always use IS NULL
or IS NOT NULL
when filtering.
16. Performance Implications of Data Types
- Smaller types = faster performance
- Indexes on large TEXT or BLOB fields are costly
- Use appropriate precision in numbers (avoid FLOAT if DECIMAL is safer)
17. Data Type Errors and Troubleshooting
Common errors include:
- Type mismatch (e.g., inserting text into INT)
- Truncation (e.g., inserting long strings into VARCHAR(10))
- Implicit conversions breaking logic
Solution: always validate types and apply constraints carefully.
18. Best Practices for Using Data Types
- Be explicit with data types — avoid relying on defaults
- Always constrain string lengths with
VARCHAR(n)
- Use
DECIMAL
for money, neverFLOAT
- Don’t use
TEXT
unless necessary - Use
BOOLEAN
for true/false flags, not strings like ‘yes’/’no’
19. Real-World Schema Example
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
description TEXT,
is_available BOOLEAN DEFAULT TRUE,
added_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
This schema combines most of the common data types used in real projects.
20. Summary and Next Steps
Understanding SQL data types is foundational to writing clean, optimized, and scalable queries. By assigning the right type to each column, you gain better control over your application’s data.