Design and best practice · Designing databases

SQL data types: how to choose the right one

The data type defines which values a column expects. Choose by meaning, range and precision; the names and rules vary by engine.

Example

This example stores amounts in cents so that sums and filters work with integers. It applies explicit constraints to the title and the flag. The date is stored as ISO 8601 text, so the application must check that it is a real date.

Create, insert and query
CREATE TABLE product_types_demo (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80),
  price_cents INTEGER NOT NULL CHECK (price_cents >= 0),
  active INTEGER NOT NULL CHECK (active IN (0, 1)),
  created_at TEXT NOT NULL
);

INSERT INTO product_types_demo
  (id, name, price_cents, active, created_at)
VALUES (1, 'Notebook', 1295, 1, '2026-09-28');

SELECT name, price_cents, active
FROM product_types_demo
WHERE price_cents >= 1000;

The value 1295 equals £12.95 if the currency uses two decimal places. Store the currency too if your application supports more than one; not all of them have two decimal places. You can try the query in the site's editor SQL.

Differences and trade-offs

Choosing the type for each kind of data

Start with three questions: can it have a fraction? What range and precision does it need? Is it text, a date or a quantity? Then check the rules of the engine you use in production. This table is a starting point, not a substitute for those rules.

Recommended type by use
DataTypical typeKey decision
Units, age, idINTEGER or equivalentChoose a big enough range. An id should not be treated as a number to calculate with.
Price or amountDECIMAL(p,s) / NUMERIC(p,s)p is the total number of digits and s the number of decimals. In SQLite, consider whole cents.
Approximate measurementFLOAT, REAL or equivalentRepresentation error is possible: avoid exact equality and money.
Name, title, descriptionVARCHAR(n), NVARCHAR(n) or TEXTLength limits and Unicode support vary by engine and configuration.
Date of birthDATEDo not add a time if the data has none.
When an event happenedDate-and-time type with a time zone or a UTC conventionDecide how the time zone is interpreted when saving and reading.
Yes / noBOOLEAN, BIT or controlled integerCheck the representation and limit the valid values.

Precision: DECIMAL vs FLOAT

DECIMAL(10,2) allows up to ten digits in total, two of them after the decimal point. It is a common choice for amounts on engines with exact decimals. FLOAT and REAL represent approximate numbers; they are useful for measurements, but a sum can come out slightly different from the expected decimal. In SQLite, DECIMAL(10,2) has NUMERIC affinity and is not an exact fixed decimal as it is on the other engines mentioned.

Length: VARCHAR, NVARCHAR and TEXT

If the data has a maximum length set by the business, document it and validate it. VARCHAR(80) does not mean the same on every engine: SQLite does not enforce that limit. In SQL Server, NVARCHAR is a Unicode option and its n parameter counts byte pairs, not always visible characters. PostgreSQL and MySQL also offer variable-length text types, with different limits and storage. Do not choose a type just because the name looks similar.

Dates, times and booleans

A date without a time is different from a point in time. PostgreSQL offers DATE and TIMESTAMP WITH TIME ZONE; SQL Server offers date and datetimeoffset; MySQL offers DATE, DATETIME and TIMESTAMP. Decide on the time zone and how it is displayed before migrating data. For flags, PostgreSQL has boolean, MySQL treats BOOLEAN as a synonym for TINYINT(1) and SQL Server uses bit. SQLite has no native date or boolean types: dates are usually stored as ISO 8601 text or numbers, and flags as 0 and 1.

Quick comparison: PostgreSQL, MySQL, SQL Server and SQLite

Rough equivalents; check the documentation for your version
NeedPostgreSQLMySQLSQL ServerSQLite
IntegerintegerINTintINTEGER
Exact moneynumeric(10,2)DECIMAL(10,2)decimal(10,2)INTEGER in cents
Short textvarchar(80)VARCHAR(80)nvarchar(80)TEXT with CHECK if a limit is needed
DatedateDATEdateTEXT ISO 8601, validated
FlagbooleanBOOLEAN (alias)bitINTEGER with CHECK

The table compares intentions, not interchangeable declarations. In particular, a migration from SQLite needs its validations, precision and dates reviewed. SQL Server also supports Unicode with VARCHAR under UTF-8 collations; the choice between VARCHAR and NVARCHAR depends on the configuration and the case.

Avoid this confusion

  • Storing amounts as FLOAT: an approximate representation can distort comparisons and sums. Use an exact decimal where available, or whole smaller units if the model allows it.
  • Assuming VARCHAR(n) always enforces a limit: in SQLite the stated length is not enforced; add a CHECK constraint or validate it in the application.
  • Storing dates as text with no defined format: mixed formats make sorting and filtering harder. Use a native date type where one exists; in SQLite, agree on an ISO 8601 format and validate the value.
  • Confusing NULL with false or zero: NULL means the data is missing. Use NOT NULL when the column is required, and see the guide on IS NULL and IS NOT NULL.
  • Copying the same declaration between engines: test ranges, Unicode, time zones and constraints before migrating. A similar type name does not guarantee the same behaviour.