Full answer
sql
CREATE TABLE s (n INTEGER) STRICT;
INSERT INTO s VALUES ('42');
SELECT n, typeof(n) AS stored_as FROM s;| n | stored_as |
|---|---|
| 42 | integer |
The value '42' is still accepted, because it converts losslessly to an integer. 'abc' cannot, so the insert fails:
sql
CREATE TABLE s_bad (n INTEGER) STRICT;
INSERT INTO s_bad VALUES ('abc');
-- error on purposetext
Error: cannot store TEXT value in INTEGER column s_bad.nSTRICT also rejects unknown type names such as VARCHAR(20) or DATE, which ordinary SQLite tables accept silently:
sql
CREATE TABLE s2 (name VARCHAR(20)) STRICT; -- error on purposetext
Error: unknown datatype for s2.name: "VARCHAR(20)"A column declared ANY in a STRICT table keeps any value unchanged. Existing tables cannot be switched to STRICT in place; the usual path is to create a new table and copy the data.
sql
CREATE TABLE s (n INTEGER) STRICT;
INSERT INTO s VALUES ('42');
SELECT n, typeof(n) AS stored_as FROM s;
-- error on purpose
CREATE TABLE s_bad (n INTEGER) STRICT;
INSERT INTO s_bad VALUES ('abc');
-- error on purpose
CREATE TABLE s2 (name VARCHAR(20)) STRICT;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- STRICT enforces declared column types
- Lossless conversions such as '42' to 42 still happen
- Only INT, INTEGER, REAL, TEXT, BLOB and ANY are allowed
- Requires SQLite 3.37 or later
Common mistakes
- Using VARCHAR or DATE names in a STRICT table
- Assuming old SQLite versions understand STRICT
Follow-ups
- How would you migrate an existing table to STRICT?
- When might ANY be useful?
Tips
- Contrast with type affinity