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 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.
| Data | Typical type | Key decision |
|---|---|---|
| Units, age, id | INTEGER or equivalent | Choose a big enough range. An id should not be treated as a number to calculate with. |
| Price or amount | DECIMAL(p,s) / NUMERIC(p,s) | p is the total number of digits and s the number of decimals. In SQLite, consider whole cents. |
| Approximate measurement | FLOAT, REAL or equivalent | Representation error is possible: avoid exact equality and money. |
| Name, title, description | VARCHAR(n), NVARCHAR(n) or TEXT | Length limits and Unicode support vary by engine and configuration. |
| Date of birth | DATE | Do not add a time if the data has none. |
| When an event happened | Date-and-time type with a time zone or a UTC convention | Decide how the time zone is interpreted when saving and reading. |
| Yes / no | BOOLEAN, BIT or controlled integer | Check 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
| Need | PostgreSQL | MySQL | SQL Server | SQLite |
|---|---|---|---|---|
| Integer | integer | INT | int | INTEGER |
| Exact money | numeric(10,2) | DECIMAL(10,2) | decimal(10,2) | INTEGER in cents |
| Short text | varchar(80) | VARCHAR(80) | nvarchar(80) | TEXT with CHECK if a limit is needed |
| Date | date | DATE | date | TEXT ISO 8601, validated |
| Flag | boolean | BOOLEAN (alias) | bit | INTEGER 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
CHECKconstraint 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:
NULLmeans the data is missing. UseNOT NULLwhen 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.