5 min read
•Question 28 of 29easyWhat are common SQL data types?
Choosing the right data types.
What You'll Learn
- Numeric types
- String types
- Date/time types
Numeric Types
query.sqlSQL
-- Integer types
TINYINT -- -128 to 127 (1 byte)
SMALLINT -- -32,768 to 32,767 (2 bytes)
INT -- -2B to 2B (4 bytes)
BIGINT -- Very large numbers (8 bytes)
-- Decimal types
DECIMAL(10,2) -- Exact, for money: 12345678.90
NUMERIC(10,2) -- Same as DECIMAL
FLOAT -- Approximate, 4 bytes
DOUBLE -- Approximate, 8 bytes
-- Example
CREATE TABLE products (
id INT,
quantity SMALLINT,
price DECIMAL(10,2),
rating FLOAT
);String Types
query.sqlSQL
-- Fixed/Variable length
CHAR(10) -- Fixed 10 characters (padded)
VARCHAR(255) -- Variable up to 255 characters
-- Large text
TEXT -- Up to 65KB
MEDIUMTEXT -- Up to 16MB
LONGTEXT -- Up to 4GB
-- Binary
BINARY(16) -- Fixed binary (UUIDs)
VARBINARY(255) -- Variable binary
BLOB -- Binary large object
-- Example
CREATE TABLE users (
id INT,
country_code CHAR(2), -- Always 2 chars: 'US', 'UK'
name VARCHAR(100),
bio TEXT
);Date/Time Types
query.sqlSQL
DATE -- '2024-01-15'
TIME -- '10:30:00'
DATETIME -- '2024-01-15 10:30:00'
TIMESTAMP -- Same, but with timezone (MySQL)
YEAR -- '2024'
-- Example
CREATE TABLE events (
id INT,
event_date DATE,
start_time TIME,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);Special Types
query.sqlSQL
-- Boolean
BOOLEAN -- TRUE/FALSE (or TINYINT(1) in MySQL)
-- JSON (MySQL 5.7+, PostgreSQL)
JSON -- Stores JSON documents
-- UUID (PostgreSQL)
UUID -- 128-bit unique identifier
-- ENUM
ENUM('small', 'medium', 'large')Choosing Types
| Data | Recommended Type |
|---|---|
| Money | DECIMAL(10,2) |
| Age | TINYINT UNSIGNED |
| VARCHAR(255) | |
| UUID | CHAR(36) or BINARY(16) |
| Long text | TEXT |