Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do SQLite storage classes differ from declared column types?

Quick answer

A declared column type and a value’s storage class are different things. In ordinary SQLite tables, the declared type determines a type affinity—a preference that may cause conversions, but generally does not restrict values to one storage class. Each value has its own storage class: NULL, INTEGER, REAL, TEXT, or BLOB. STRICT tables enforce types more rigidly, subject to their rules and limitations. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I declared a column as INTEGER. Does that mean every value in it is stored as an integer?

Geez

Not necessarily. In an ordinary, non-STRICT SQLite table, a declared column type determines its affinity: a storage preference that may cause conversions, but generally does not limit the column to values of one storage class. The value itself has a storage class, so “declared INTEGER” and “stored as INTEGER” are not interchangeable descriptions. [S1]

SQLite’s five storage classes are NULL, INTEGER, REAL, TEXT, and BLOB. [S1] For example, if you inspect a database and find a TEXT value in a column declared INTEGER, that describes the stored value; the declaration alone doesn’t prove every value has the same storage class.

Follow-up question

Can you give me a compact reference that keeps those terms separate?

Geez

Use this concept reference to distinguish the labels; it does not predict how a particular input will be stored:

  • Declared type: The type name written in the table definition, such as INTEGER or TEXT.
  • Affinity: The column’s storage preference, determined by its declared type in an ordinary table. SQLite lists TEXT, NUMERIC, INTEGER, REAL, and BLOB affinities. [S1]
  • Storage class: The category of an individual value: NULL, INTEGER, REAL, TEXT, or BLOB. [S1]

Disposable examples of observations:

  • score declared INTEGER → an inspected value is INTEGER.
  • score declared INTEGER → another inspected value is TEXT.
  • note declared TEXT → an inspected value is NULL.

These hypothetical observations show why the two labels should be recorded separately. They are not promises about which storage class SQLite will choose for a given input. The documentation establishes that affinity can cause conversions, but does not establish the outcome for every input-and-declaration combination. [S1]

Follow-up question

How should I describe a surprising value when I’m debugging a query or reviewing a database?

Geez

Write down both the column declaration and the storage class you actually observed, then state what you expected. For example:

Column: score | declared type: INTEGER | observed storage class: TEXT | expected: INTEGER

That description is more precise than saying “the column is text,” which could mean either that its declared type is TEXT or that a particular value is stored as TEXT. Keep the observation tied to the specific row or value you checked. Affinity may cause conversions, but the declaration generally does not enforce one storage class in ordinary tables. [S1]

Follow-up question

What changes if my table is STRICT, and what should I check before drawing a conclusion?

Geez

STRICT tables provide more rigid type enforcement than ordinary tables and have been available since SQLite 3.37.0. They still have rules and limitations, so don’t assume that STRICT means every declaration or input behaves exactly as you expect; consult the documentation for the specific rules that apply. [S1] The excerpted information does not list those detailed rules or establish the outcome for any particular value.

For a reliable note, record whether the table is ordinary or STRICT, the exact declared type, and the storage class of the value you observed. Keep expected behavior separate from observed behavior. That makes the distinction useful without treating a column’s declaration as proof of every value’s stored form.

A practical checklist

  • Record the exact declared type for the column.

  • Identify whether the table is ordinary or STRICT.

  • Record the observed storage class for the specific value, separately from the declared type.

  • Write down the expected and observed results as separate fields.

  • For STRICT tables, check the applicable documented rules before inferring how a value will be handled.

A few more useful answers

What are SQLite’s five storage classes?

NULL, INTEGER, REAL, TEXT, and BLOB. A storage class describes a value, not merely the type name written in a column definition. [S1]

Does affinity mean SQLite always converts a value to match the declared type?

No. Affinity expresses a storage preference and may cause conversions; in ordinary tables it generally does not restrict a column to a single storage class. [S1]

Does the information here give the full rules for STRICT tables?

No. It establishes that STRICT tables provide more rigid type enforcement and notes their availability since SQLite 3.37.0, but it does not give their detailed rules or limitations. [S1]

Sources

References used for this guide.

  1. [S1] SQLite Datatypes: storage classes and type affinity
    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