Use it when designing columns or fixing conversion and rounding errors.
Choosing Data Types
Data types define what a column can hold: integers, numeric/decimal, text, dates/timestamps with or without time zone, booleans, UUIDs, JSON.
1. Column types: \d <table> / SHOW CREATE TABLE.
2. Float used for money? → numeric.
3. Time zone handling.
4. ID range sufficient (bigint)?
5. Migrate carefully.
value → type validation → storage → comparison/sorting
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Insert boundary values (very long text, negative numbers, far future dates) in a test. Confirm the application sends values in the correct type.
Step 2 Common failures Narrow the fault
utf8 in older MySQL is limited to three bytes and cannot store emoji; use utf8mb4. Integer IDs overflow at about 2.1 billion. Timestamps without time zone become ambiguous across daylight saving changes.
Money in DECIMAL/NUMERIC, not float. Timestamps with time zone (store UTC). Sizes that fit growth (bigint for IDs). Avoid storing numbers or dates as text.
\d <table>— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
Types affect storage, index size and query planning. Comparing a varchar column with a number forces a cast on every row. Collation decides sort order and case sensitivity.
ReferenceImportant terms & boundaries3 sections · depth on demand
Type guidance
| Data | Use | Avoid |
|---|---|---|
| Money | numeric(12,2) / DECIMAL | float, double |
| Timestamps | timestamptz (PostgreSQL), DATETIME(6) + UTC | Strings |
| Identifiers | bigint identity, uuid (v7 for index locality) | int for fast-growing tables |
| Text | text/varchar(n) with real limits | Fixed char(n) |
| Booleans | boolean | 'Y'/'N' strings |
| JSON | jsonb (PostgreSQL), JSON | Unparsed text blobs |
In depth
Choosing types by the question they must answer
| Data | PostgreSQL | MySQL / MariaDB | Why |
|---|---|---|---|
| Money, quantities with decimals | numeric(12,2) | DECIMAL(12,2) | Exact; float gives 0.1 + 0.2 ≠ 0.3 |
| Point in time | timestamptz | DATETIME(6) in UTC by convention, or TIMESTAMP | Unambiguous across time zones and DST |
| Calendar date | date | DATE | No accidental time part |
| IDs | bigint identity | BIGINT AUTO_INCREMENT | int overflows at 2,147,483,647 |
| Text | text (+ CHECK (length(x) <= n)) | VARCHAR(n) with utf8mb4 | Unicode incl. emoji |
| Flags | boolean | TINYINT(1) / BOOLEAN | |
| IP addresses | inet / cidr | VARBINARY(16) + functions | Correct sorting and containment |
| Semi-structured | jsonb (indexable with GIN) | JSON | Validated JSON |
Time zones
timestamptz stores an absolute instant (internally UTC) and displays it in the session's TimeZone. timestamp without time zone stores wall-clock time with no zone: 2026-03-29 02:30 does not exist in Copenhagen, and a value written by a server in another zone is misread silently. MySQL TIMESTAMP converts using the session time zone and is limited to 2038-01-19; DATETIME does no conversion.
Collation
Sort order and equality depend on collation: whether 'Æble' sorts after 'Zebra' and whether 'a' = 'A'. In MySQL utf8mb4_0900_ai_ci is accent- and case-insensitive, so a UNIQUE index treats 'Ole' and 'ole' as duplicates. Changing collation later means rebuilding indexes.
Check for overflow risk
SELECT pg_sequence_last_value('orders_id_seq'); -- compare with 2147483647 for int columns
Common mistakes
- Storing dates or numbers as text.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.