Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

Choosing Data Types

Direct answer

Data types define what a column can hold: integers, numeric/decimal, text, dates/timestamps with or without time zone, booleans, UUIDs, JSON.

When to use this

Use it when designing columns or fixing conversion and rounding errors.

First 30 seconds

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.

Operational verification

value → type validation → storage → comparison/sorting

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it when designing columns or fixing conversion and rounding errors.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

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.

Key checks
  • \d <table> — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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

DataUseAvoid
Moneynumeric(12,2) / DECIMALfloat, double
Timestampstimestamptz (PostgreSQL), DATETIME(6) + UTCStrings
Identifiersbigint identity, uuid (v7 for index locality)int for fast-growing tables
Texttext/varchar(n) with real limitsFixed char(n)
Booleansboolean'Y'/'N' strings
JSONjsonb (PostgreSQL), JSONUnparsed text blobs

In depth

Choosing types by the question they must answer

DataPostgreSQLMySQL / MariaDBWhy
Money, quantities with decimalsnumeric(12,2)DECIMAL(12,2)Exact; float gives 0.1 + 0.2 ≠ 0.3
Point in timetimestamptzDATETIME(6) in UTC by convention, or TIMESTAMPUnambiguous across time zones and DST
Calendar datedateDATENo accidental time part
IDsbigint identityBIGINT AUTO_INCREMENTint overflows at 2,147,483,647
Texttext (+ CHECK (length(x) <= n))VARCHAR(n) with utf8mb4Unicode incl. emoji
FlagsbooleanTINYINT(1) / BOOLEAN
IP addressesinet / cidrVARBINARY(16) + functionsCorrect sorting and containment
Semi-structuredjsonb (indexable with GIN)JSONValidated 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
Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Terminology layer

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.

Aliases that resolve here
choosing data typessql data typesutf8mb4 collation
Confirmation

Confirm action

Continue?