Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

What does STRICT mode change in a SQLite table?

Quick answer

SQLite STRICT mode adds stricter type checking to an individual table: every column must declare a type, and each declared type must be INT, INTEGER, REAL, TEXT, BLOB, or ANY. SQLite still tries its usual affinity conversions, but rejects values it cannot convert losslessly. STRICT requires SQLite 3.37.0 or later; it does not replace constraints such as NOT NULL or CHECK. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What does STRICT change, and when should I use it?

Geez

STRICT is an option on a table definition that makes SQLite enforce a narrower set of column types. Every column must declare a type, and the permitted names are INT, INTEGER, REAL, TEXT, BLOB, and ANY. [S1]

For a column other than ANY, a value must be NULL (unless another constraint forbids NULL) or have the specified type. SQLite still attempts its usual affinity conversions; if a value cannot be converted losslessly, the insert or update is rejected. [S1]

For example, assuming your application expects an item label to be text and its count to be an integer, a disposable table could be:

CREATE TABLE demo_item (
  label TEXT NOT NULL,
  quantity INTEGER NOT NULL
) STRICT;

The NOT NULL rules here come from the schema, not from STRICT itself. STRICT does not replace ordinary constraints; CHECK, UNIQUE, foreign-key, and other table behavior continue to work as before. [S1]

Follow-up question

Does STRICT mean SQLite will reject every value that arrives in the “wrong” form?

Geez

Not necessarily. SQLite first attempts its usual affinity conversion. STRICT rejects a value when it cannot be losslessly converted to the declared type; it is not a rule that all differently represented input must fail. [S1]

For a small disposable test, try a value that should not be losslessly convertible to an integer:

INSERT INTO demo_item (label, quantity)
VALUES ('pencils', 'not-a-number');

That insert should be rejected for the INTEGER column. The example illustrates the type rule; it is not a substitute for testing the exact inputs your application sends. The documentation excerpt establishes the conversion and rejection rule, but does not specify the outcome for every possible input value. [S1]

Follow-up question

What is ANY, and are the usual type names like VARCHAR allowed?

Geez

In a STRICT table, ANY accepts values of any storage class without coercion. Use it only when accepting varied value types in that column is intentional; it does not mean the other typed columns stop being checked. [S1]

The permitted type names are exactly INT, INTEGER, REAL, TEXT, BLOB, and ANY; names such as VARCHAR are not on that list. [S1] If you are adapting an existing schema, check each declared type rather than assuming a familiar type name is accepted.

One key detail: INTEGER PRIMARY KEY aliases the rowid, but INT PRIMARY KEY does not. [S1] If that rowid behavior matters, use the exact INTEGER PRIMARY KEY spelling rather than changing it to INT PRIMARY KEY.

Follow-up question

How do I check compatibility before adding STRICT to a table?

Geez

Check the SQLite version used by the application that will open the database, not just the version installed on your development machine. STRICT is supported beginning with SQLite 3.37.0, released 2021-11-27; earlier versions do not normally recognize the STRICT table option. [S1]

A practical review is: confirm that the runtime is at least 3.37.0; change column declarations to the allowed names; decide whether each column should reject values that cannot be losslessly converted; and add any separate constraints your data needs. Then test a disposable schema with representative inputs. The source establishes the version threshold and type rules, but does not establish which SQLite runtime a particular application uses or whether a proposed schema fits your data.

For example, a minimal table using two allowed types is:

CREATE TABLE demo_note (
  note_id INTEGER PRIMARY KEY,
  body TEXT NOT NULL
) STRICT;

Keep INTEGER PRIMARY KEY if you intend the rowid alias behavior described above. [S1]

A practical checklist

  • Confirm the SQLite runtime that will open the database is version 3.37.0 or later.

  • Use only INT, INTEGER, REAL, TEXT, BLOB, or ANY for STRICT column types.

  • Check whether inputs can be losslessly converted to each declared type.

  • Use ANY only where accepting values of any storage class without coercion is intentional.

  • Add separate constraints such as NOT NULL or CHECK where needed.

  • Use INTEGER PRIMARY KEY, not INT PRIMARY KEY, when rowid alias behavior is intended.

A few more useful answers

Does STRICT automatically make every column NOT NULL?

No. NULL is allowed unless a separate rule, such as NOT NULL, prohibits it. [S1]

Can I turn on STRICT for just one column?

STRICT is a table option, so it applies to the table, not to an individual column. [S1]

Does STRICT replace CHECK or foreign-key constraints?

No. Those and other ordinary table behaviors continue to work as before; add the constraints your schema requires. [S1]

Sources

References used for this guide.

  1. [S1] STRICT Tables
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work