Skip to main content

How to Allow Only Valid Values in a SQLite Column with CHECK

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
CREATE TABLE <table> (
<column> <type> CHECK (<condition>)
);
PartWhat 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 --version to 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:

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;

The check_constraint.sql script in a macOS-style code window: an items table whose quantity column is INTEGER NOT NULL CHECK (quantity > 0), inserts of 6 Apples and 0 Rice, and a SELECT of every row.

  • CHECK (quantity > 0) is the rule: the quantity must be above zero.
  • NOT NULL makes the quantity required too. Common Mistakes shows why a CHECK needs it.
  • The first INSERT passes the rule with 6, and the second breaks it with 0.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
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.

Output
Error near line 7 of check_constraint.sql: CHECK constraint failed: quantity > 0
+--------+----------+
| name | quantity |
+--------+----------+
| Apples | 6 |
+--------+----------+

The output in a terminal: an error near line 7 of check_constraint.sql, CHECK constraint failed: quantity > 0, then the items table with one row, Apples with a quantity of 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​

MistakeWhat happensDo this instead
A CHECK on a column that allows NULLA row with no quantity went in: a condition on NULL isn't false, so it passesAdd NOT NULL when the value is required
Writing the rule without parentheses, CHECK quantity > 0Parse error ...: near "quantity": syntax errorWrap the condition: CHECK (quantity > 0)

Troubleshooting​

SymptomLikely causeFix
CHECK constraint failed: quantity > 0The row's value breaks the ruleFix the value, or the rule if the value is right
Parse error near line 1 of check_constraint.sql: table items already existsThe script already ran on pantry.dbDelete 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​