Data Types Deep Dive

SQL FoundationsData TypesFree Lesson

Advertisement

Why Data Types Matter

Choosing the correct data type ensures:

  • Data integrity — prevents invalid data entry
  • Storage efficiency — uses minimal disk/memory space
  • Query performance — comparisons and indexes work faster
  • Correct operations — math works on numbers, pattern matching on strings

💡 Using wrong data types is a common source of bugs. Storing dates as strings, for example, breaks sorting and date arithmetic.

Numeric Types

Integer Types

TypeStorageRange
TINYINT1 byte-128 to 127
SMALLINT2 bytes-32,768 to 32,767
MEDIUMINT3 bytes-8,388,608 to 8,388,607
INT4 bytes-2.1 billion to 2.1 billion
BIGINT8 bytes±9.2 quintillion
CREATE TABLE measurements (
    small_count SMALLINT,    -- Sensor readings
    population BIGINT,       -- Country population
    flag TINYINT             -- Boolean-like (0 or 1)
);

Decimal Types

TypeDescriptionExample
DECIMAL(p,s)Exact precisionDECIMAL(10,2) → 12345678.90
NUMERIC(p,s)Same as DECIMALNUMERIC(5,3) → 12.345
FLOATApproximate (4 bytes)3.14159
DOUBLEApproximate (8 bytes)More precise than FLOAT
CREATE TABLE financial_data (
    price DECIMAL(10,2),     -- Exact: $99999999.99
    interest_rate FLOAT,     -- Approximate: 4.5%
    precise_amount NUMERIC(15,6)  -- 6 decimal places
);

⚠️ Never use FLOAT/DOUBLE for financial data. Floating-point rounding errors cause accounting discrepancies. Always use DECIMAL for money.

String Types

TypeDescriptionMax Length
CHAR(n)Fixed-length string255 bytes
VARCHAR(n)Variable-length string65,535 bytes
TEXTLong text65,535 bytes
MEDIUMTEXTMedium text16 MB
LONGTEXTLong text4 GB
ENUMPredefined listOne of the listed values
CREATE TABLE products (
    code CHAR(5),           -- Fixed: 'ABC12'
    name VARCHAR(100),      -- Variable: 'Wireless Mouse'
    description TEXT,       -- Long: product details
    category ENUM('Electronics', 'Clothing', 'Food')  -- Restricted
);

CHAR vs VARCHAR

FeatureCHAR(10)VARCHAR(10)
StorageAlways 10 bytes1-10 bytes + 1-2 overhead
PerformanceFaster (fixed size)Slightly slower
Best ForFixed-length codes (ZIP, state)Variable-length names, descriptions

Date and Time Types

TypeFormatExample
DATEYYYY-MM-DD'2024-01-15'
TIMEHH:MM:SS'14:30:00'
DATETIMEYYYY-MM-DD HH:MM:SS'2024-01-15 14:30:00'
TIMESTAMPAuto-updates, UTC'2024-01-15 19:30:00'
YEARYYYY2024
CREATE TABLE events (
    event_id INT PRIMARY KEY,
    event_name VARCHAR(100),
    event_date DATE,
    event_time TIME,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Choosing the Right Type

DataRecommended TypeWhy
Person's ageTINYINT0-150 fits in 1 byte
Product priceDECIMAL(10,2)Exact money values
Email addressVARCHAR(255)Variable length, max 255 typical
Country nameVARCHAR(100)Variable length
Boolean flagBOOLEAN or TINYINTTrue/false or 0/1
TimestampTIMESTAMPAuto-updates, timezone-aware
ZIP codeCHAR(5) or VARCHAR(10)Fixed/variable length
UUIDCHAR(36) or BINARY(16)Fixed or binary format

Type Conversion

-- Explicit conversion
SELECT CAST('123' AS INT);
SELECT CONVERT('2024-01-15', DATE);

-- Implicit conversion (avoid in production)
SELECT * FROM orders WHERE order_id = '123';  -- String compared to INT

⚠️ Implicit type conversions can cause unexpected results and prevent index usage. Always use matching types in comparisons.

✏️ Exercise: Design a table for a library with appropriate data types: book_id, title, author, isbn, page_count, price, published_date, is_available.

See Solution

CREATE TABLE books (
    book_id INT PRIMARY KEY,
    title VARCHAR(200),
    author VARCHAR(100),
    isbn CHAR(13),
    page_count SMALLINT,
    price DECIMAL(8,2),
    published_date DATE,
    is_available BOOLEAN
);

✅ Key Takeaways

  1. Choose data types based on the actual data, not convenience
  2. Use DECIMAL for financial data — never FLOAT/DOUBLE
  3. VARCHAR is preferred over CHAR for variable-length data
  4. TIMESTAMP auto-updates and is timezone-aware; DATETIME does not
  5. Implicit type conversions can break queries — always match types explicitly

Advertisement

Need Expert SQL Help?

Get personalized SQL training or database consulting.

Advertisement