How to Allow Only Valid Values in a SQLite Column with CHECK
A column's type says what kind of value it holds, but not which values make sense. A quantity of 0 or -2 is still a number. A CHECK constraint adds a rule every row has to pass, and SQLite refuses the rows that don't. This guide makes a pantry table refuse a quantity of 0, with real output from SQLite 3.54.1.
Syntax
CREATE TABLE <table> (
<column> <type> CHECK (<condition>)
);
| Part | What it means |
|---|---|
CHECK (<condition>) | A rule for the column, such as quantity > 0 |
<condition> | Checked for every row that's inserted or changed. A row whose condition comes out false is refused |
SQLite's documentation on CHECK constraints: "Each time a new row is inserted into the table or an existing row is updated, the expression associated with each CHECK constraint is evaluated".
Prerequisites
- The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run
sqlite3 --versionto see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.
Step 1: Write the Script
Save this as check_constraint.sql:
CREATE TABLE items (
name TEXT NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0)
);
INSERT INTO items (name, quantity) VALUES ('Apples', 6);
INSERT INTO items (name, quantity) VALUES ('Rice', 0);
SELECT * FROM items;

CHECK (quantity > 0)is the rule: the quantity must be above zero.NOT NULLmakes the quantity required too. Common Mistakes shows why aCHECKneeds it.- The first
INSERTpasses the rule with 6, and the second breaks it with 0.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table pantry.db ".read check_constraint.sql"
sqlite3 opens the database file pantry.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.
Error near line 7 of check_constraint.sql: CHECK constraint failed: quantity > 0
+--------+----------+
| name | quantity |
+--------+----------+
| Apples | 6 |
+--------+----------+

Apples went in. Rice, with a quantity of 0, broke the rule, and the error quotes the rule it broke: CHECK constraint failed: quantity > 0. A negative quantity, such as -2, gets the same error.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A CHECK on a column that allows NULL | A row with no quantity went in: a condition on NULL isn't false, so it passes | Add NOT NULL when the value is required |
Writing the rule without parentheses, CHECK quantity > 0 | Parse error ...: near "quantity": syntax error | Wrap the condition: CHECK (quantity > 0) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CHECK constraint failed: quantity > 0 | The row's value breaks the rule | Fix the value, or the rule if the value is right |
Parse error near line 1 of check_constraint.sql: table items already exists | The script already ran on pantry.db | Delete pantry.db and run the script again |
FAQ
Can I give a CHECK a name?
Yes, with CONSTRAINT <name> in front: quantity INTEGER NOT NULL CONSTRAINT positive_quantity CHECK (quantity > 0). Then the error says CHECK constraint failed: positive_quantity instead of the condition.
Can a CHECK use more than one column?
Yes. Write it after the columns, as its own line, such as CHECK (end_date >= start_date). The documentation says a CHECK "may be attached to a column definition or specified as a table constraint. In practice it makes no difference."
Conclusion
CHECK (quantity > 0) let 6 Apples in and refused 0 Rice with CHECK constraint failed: quantity > 0, so the table only holds quantities that make sense.
Additional Resources
- CHECK constraints — how SQLite evaluates the rule.
- CREATE TABLE — every column and table constraint.
