Careeroza
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
| Type | Description |
|---|---|
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 |
TEXT | long text (descriptions, comments) |
NVARCHAR(n) | SQL Server: Unicode variable-length text |
Numeric Types
| Type | Description |
|---|---|
INT / INTEGER | whole numbers (about ±2.1 billion) |
BIGINT | large whole numbers (ids of big tables) |
SMALLINT, TINYINT | small whole numbers, save space |
DECIMAL(p,s) / NUMERIC(p,s) | exact numbers: p digits total, s after the point. Use for money. |
FLOAT, REAL, DOUBLE | approximate numbers for scientific values (rounding errors possible) |
BOOLEAN / BIT | true/false (MySQL stores it as TINYINT(1)) |
Date and Time Types
| Type | Format | Example |
|---|---|---|
DATE | YYYY-MM-DD | '2024-03-22' |
TIME | HH:MI:SS | '14:30:00' |
DATETIME / TIMESTAMP | YYYY-MM-DD HH:MI:SS | '2024-03-22 14:30:00' |
TIMESTAMP WITH TIME ZONE | date-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), neverFLOAT(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.