← Back to SQL, page 9

sql-0165 · Medium mid

What is a STRICT table in SQLite, and what does it change?

A table created with the STRICT keyword (SQLite 3.37+) enforces declared column types: inserting text that cannot convert into an INTEGER column raises an error. Allowed types are limited to INT, INTEGER, REAL, TEXT, BLOB and ANY. It brings SQLite closer to PostgreSQL's strict typing.

Full answer
sql
CREATE TABLE s (n INTEGER) STRICT;
INSERT INTO s VALUES ('42');
SELECT n, typeof(n) AS stored_as FROM s;
nstored_as
42integer

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 purpose
text
Error: cannot store TEXT value in INTEGER column s_bad.n

STRICT 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 purpose
text
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