Databases & SQL Master Course — 066. SQLite STRICT Tables
Lesson 066 SQLite STRICT Tables
Prerequisites and objectives
You understand SQLite affinity and integrity constraints. You will compare an ordinary table with a STRICT table, test a lossless conversion, reject an incompatible value, and preserve an exact source representation with ANY. The example handles fictional inventory readings rather than financial or scientific measurements.
Create the practice fixture
Use SQLite 3.37.0 or later in a fresh disposable database. STRICT is a table option, so one database may contain both ordinary and strict tables. Every experiment uses a transaction and ends with rollback. The two empty tables deliberately separate flexible storage from the stronger contract being tested.
CREATE TABLE flexible_readings (reading_id INTEGER PRIMARY KEY, units INTEGER);
CREATE TABLE strict_readings (
reading_id INTEGER PRIMARY KEY,
units INTEGER NOT NULL CHECK(units>=0),
raw_code ANY
) STRICT;
Compare declared type and stored type
In an ordinary SQLite table, an INTEGER declaration gives a column affinity. A value that cannot be converted into an integer may still be stored as text. That flexibility is sometimes intentional, but it can violate an application’s assumption that every units value is numeric.
A STRICT table narrows accepted storage types. It still attempts suitable lossless conversion; it does not require the caller to spell every input literal using the final storage class. Therefore text that represents an exact integer can become an integer, while a nonnumeric word is rejected.
BEGIN;
INSERT INTO flexible_readings VALUES (1,'many');
SELECT units,typeof(units) FROM flexible_readings;
INSERT INTO strict_readings VALUES (1,'12','0007');
SELECT units,typeof(units),raw_code,typeof(raw_code) FROM strict_readings;
INSERT INTO strict_readings VALUES (2,'many','X');
ROLLBACK;
The ordinary table stores many as text. The strict table converts the units text to integer twelve, while ANY preserves the raw code as text with its leading zeros. The rejected second insertion shows the new type boundary. The result must be inspected with typeof rather than inferred from a SQL editor’s visual formatting.

Keep type and domain rules separate
An integer can still be invalid for a particular domain. Negative five is an integer, but it violates our nonnegative stock rule. NULL may also remain acceptable in a strict column unless a required-value rule prohibits it. STRICT, NOT NULL, and CHECK therefore address different parts of the contract.
The supported strict type names in the documented feature are INT, INTEGER, REAL, TEXT, BLOB, and ANY. Familiar declarations such as VARCHAR or DECIMAL are not accepted as strict type names. Do not translate a schema from another database by copying every declaration and merely appending STRICT.
Use ANY only for deliberate mixed data
ANY is useful when exact input representation matters, such as a raw source code whose leading zeros are significant. In a STRICT table it preserves the supplied value and type without affinity conversion. It should not become a blanket escape hatch that weakens every column just to avoid deciding what the data means.
If all business codes are text, a TEXT column may be a clearer contract than ANY. If a staging field legitimately preserves several source storage classes, ANY can document that choice. Keep raw preservation separate from the normalized fields used in arithmetic, joins, or validated business identifiers.
Plan adoption and compatibility
Adding STRICT to a CREATE statement only affects the new table being created. Existing ordinary tables do not become strict because a newer SQLite runtime opens them. A migration must inspect current stored types, define acceptable conversions, and test historical rows against the destination schema.
Every application or tool opening a database with strict tables needs compatible SQLite support. Check the actual embedded SQLite version rather than assuming the operating system’s command-line version is the same. Schema features, client libraries, and deployment compatibility belong in the migration plan alongside the data tests.
Four worked exercises
Exercise 1
Insert an integer-looking text value into strict units and inspect its stored type.
Worked solution
BEGIN;
INSERT INTO strict_readings VALUES (1,'8','A');
SELECT units,typeof(units) FROM strict_readings;
ROLLBACK;
The value is stored as integer eight. STRICT permits a lossless conversion, so accepted input syntax is not limited to numeric literals. The storage contract is stronger than ordinary affinity without being a ban on every conversion.
Exercise 2
Try storing a fractional value in strict integer units.
Worked solution
BEGIN;
INSERT INTO strict_readings VALUES (1,2.5,'A');
SELECT COUNT(*) FROM strict_readings;
ROLLBACK;
Two and a half cannot be losslessly represented as an integer, so it is rejected rather than truncated. If fractional quantities are legitimate, choose a different documented representation instead of silently rounding data to satisfy the schema.
Exercise 3
Separate a type-valid negative integer from an acceptable stock value.
Worked solution
BEGIN;
INSERT INTO strict_readings VALUES (1,-1,'A');
INSERT INTO strict_readings VALUES (2,NULL,'B');
ROLLBACK;
The negative integer fails the domain CHECK. The absent quantity fails NOT NULL. These failures show that type enforcement alone does not establish nonnegative or required values.
Exercise 4
Store the same digits once as text and once as an integer in strict ANY.
Worked solution
BEGIN;
INSERT INTO strict_readings VALUES (1,0,'0007'),(2,0,7);
SELECT reading_id,raw_code,typeof(raw_code) FROM strict_readings ORDER BY reading_id;
ROLLBACK;
The two raw values preserve different representations. Leading zeros remain in the text value. A later normalization rule may decide whether they represent the same business code, but ANY itself makes no such decision.
Knowledge check
Question 1
Does STRICT reject every text literal supplied to INTEGER?
- Yes, regardless of its content
- No, all text is stored unchanged
- Only when a primary key is present
- No, a lossless conversion can be accepted
Answer and explanation
D. Integer-looking text can be converted while incompatible text is rejected.
Question 2
What does ANY preserve in a STRICT table?
- The supplied value and storage type
- Only numeric magnitude after conversion
- All supplied values converted to TEXT
- Text converted according to ordinary numeric affinity
Answer and explanation
A. ANY retains representations such as text with leading zeros.
Question 3
Why keep CHECK(units>=0)?
- STRICT guarantees that stored integers are positive
- INTEGER affinity rejects negative values before CHECK runs
- Negative integers are type-valid but domain-invalid
- NOT NULL rejects negatives only when CHECK is also present
Answer and explanation
C. Type correctness does not imply business correctness.
Question 4
Which declaration is supported by the documented strict type set?
- VARCHAR(80)
- TEXT
- DECIMAL(10,2)
- DATE
Answer and explanation
B. STRICT accepts a limited type vocabulary; date and precision policies require deliberate representations.
Question 5
What should be checked before adopting strict tables?
- Only the table’s row count
- Only the migration runner’s SQLite version
- Every deployed reader and writer’s SQLite compatibility
- Whether all primary keys use AUTOINCREMENT
Answer and explanation
C. A database schema feature must be supported by the actual clients using it.
Pitfalls and summary
Do not equate strict typing with complete validation or assume it forbids all conversions. Inspect stored types, test incompatible and boundary inputs, and retain domain constraints. Use ANY for a clear preservation requirement, not as a shortcut around schema design. Version compatibility is part of the table’s usable contract.
SQLite behavior and references
Examples were checked with SQLite 3.53.1 on 10 October 2026. Use a SQLite SQL editor; Python is not a prerequisite. Verify dialect-specific functions, comparison rules, and numeric behavior before moving the examples to another engine.
Primary references
- SQLite STRICT tables: Supported types, lossless conversion, and ANY
- SQLite datatypes: Storage classes and affinity