Data Types

SQL Data Types

Every column has a data type that decides which values it can hold and how much space they take. Picking the right type keeps data valid and queries fast.

String Types

TypeDescription
CHAR(n)fixed length, padded with spaces, e.g. country codes CHAR(2)
VARCHAR(n)variable length up to n characters, the usual choice for names and emails
TEXTlong text (descriptions, comments)
NVARCHAR(n)SQL Server: Unicode variable-length text

Numeric Types

TypeDescription
INT / INTEGERwhole numbers (about ±2.1 billion)
BIGINTlarge whole numbers (ids of big tables)
SMALLINT, TINYINTsmall whole numbers, save space
DECIMAL(p,s) / NUMERIC(p,s)exact numbers: p digits total, s after the point. Use for money.
FLOAT, REAL, DOUBLEapproximate numbers for scientific values (rounding errors possible)
BOOLEAN / BITtrue/false (MySQL stores it as TINYINT(1))

Date and Time Types

TypeFormatExample
DATEYYYY-MM-DD'2024-03-22'
TIMEHH:MI:SS'14:30:00'
DATETIME / TIMESTAMPYYYY-MM-DD HH:MI:SS'2024-03-22 14:30:00'
TIMESTAMP WITH TIME ZONEdate-time plus offset (PostgreSQL timestamptz)'2024-03-22 14:30:00+05:30'

Note: SQLite has no separate date type: dates are stored as text in ISO format (YYYY-MM-DD) and handled with date functions.

Choosing Types

  • Money: DECIMAL(10,2), never FLOAT (0.1 + 0.2 is not exactly 0.3 in floating point).
  • Phone numbers and PIN codes: VARCHAR. They are not numbers you calculate with, and they can start with 0.
  • Use the smallest type that safely fits the data, but leave room to grow.