SQL Data Types

SQL FundamentalsDatabase BasicsFree Lesson

Advertisement

SQL Data Types

Every column in a SQL table must have a data type that defines what kind of data it can store.

💡 Choosing the right data type is crucial for data integrity, storage efficiency, and query performance.

String Types

CREATE TABLE products (
    sku CHAR(10),              -- Fixed length (always 10 chars)
    name VARCHAR(255),          -- Variable length (up to 255)
    description TEXT            -- Very large text
);
TypeMax SizeBest For
CHAR(n)n charactersFixed codes, IDs
VARCHAR(n)n charactersNames, emails, addresses
TEXT~65KBArticles, descriptions

Numeric Types

CREATE TABLE inventory (
    price DECIMAL(10,2),       -- Exact: 10 digits, 2 after decimal
    quantity INTEGER,           -- Whole numbers
    rating SMALLINT             -- Small integer
);
TypeStorageRangeUse Case
INTEGER4 bytes±2.1 billionCounts, IDs
SMALLINT2 bytes±32,767Small numbers
BIGINT8 bytesVery largeLarge counters
DECIMAL(p,s)VariableExact precisionMoney
FLOAT4 bytesApproximateScientific
BOOLEAN1 byteTRUE/FALSEFlags

Date and Time Types

CREATE TABLE events (
    event_date DATE,                    -- 2024-01-15
    event_time TIME,                    -- 14:30:00
    event_timestamp TIMESTAMP           -- 2024-01-15 14:30:00
);
TypeFormatExample
DATEYYYY-MM-DD2024-01-15
TIMEHH:MM:SS14:30:00
TIMESTAMPYYYY-MM-DD HH:MM:SS2024-01-15 14:30:00

⚠️ Always use the smallest data type that can hold your data. A VARCHAR(10) is better than VARCHAR(1000) if you only need 10 characters.

✅ Key Takeaways

  1. Each column needs a data type that defines what it can store
  2. Use VARCHAR for variable text, CHAR for fixed-length text
  3. Use DECIMAL for exact values like money, FLOAT for approximations
  4. Choose the smallest type that fits your data
  5. BOOLEAN is great for true/false values

Advertisement

Need Expert SQL Help?

Get personalized SQL training or database consulting.

Advertisement