Skip to main content

Table Data Types

SQLite handles data types differently from many server databases.

Instead of strict fixed column types, SQLite uses type affinity plus runtime storage classes.

Core Concepts

flowchart LR
A[Declared type text] --> B[SQLite assigns affinity]
B --> C[Inserted value]
C --> D[Stored as runtime storage class]
Storage classMeaningExample literal
NULLMissing valueNULL
INTEGERSigned integer42
REALFloating-point number3.14
TEXTText string'hello'
BLOBRaw bytesX'6869'
Declared typeTypical affinity
INTEGERINTEGER
TEXT / VARCHAR(...)TEXT
REAL / DOUBLEREAL
NUMERIC / DECIMAL(...)NUMERIC
BLOBBLOB

Code Examples

-- Declare columns with common SQLite type names.
CREATE TABLE type_demo (
id INTEGER PRIMARY KEY,
qty INTEGER,
price REAL,
sku TEXT,
meta BLOB,
amount NUMERIC
);
Expected output
Table type_demo is created successfully.
-- Inspect table metadata.
PRAGMA table_info(type_demo);
cidnametypenotnulldflt_valuepk
0idINTEGER0null1
1qtyINTEGER0null0
2priceREAL0null0
3skuTEXT0null0
4metaBLOB0null0
5amountNUMERIC0null0

SQLite-Specific Nuances

SQLite Nuance

SQLite does not enforce strict type matching in the same way as many client/server databases.

Use constraints (NOT NULL, CHECK, UNIQUE) to keep data quality high.

Common Pitfalls / Best Practices

Pitfall

Assuming VARCHAR(20) strictly limits text length in SQLite.

Length limits are not enforced just by the type declaration.

Best Practice

Choose readable type names (INTEGER, TEXT, REAL) and add constraints for business rules.

Quick Challenge

Design a products table with these columns:

  • product_id integer primary key
  • name text required
  • price numeric required
View Solution
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC NOT NULL
);
Expected output
Table is created with clear affinities and required fields.