Skip to main content

How to Limit a SQLite Column to a List of Values with CHECK and IN

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A status, a priority or a size usually comes from a short, fixed list. SQLite has no enum type, but a CHECK with IN does the same job: the column accepts only the values you list. This guide limits a ticket's priority to low, medium and high, with real output from SQLite 3.54.1.

Syntax​

Syntax
<column> <type> CHECK (<column> IN (<value 1>, <value 2>, <value 3>))
PartWhat it means
<column> IN (...)True when the column's value is one of the values in the list
<value 1>, <value 2>, ...The allowed values, separated by commas. Text goes in single quotes
CHECK (...)Refuses any row for which the IN is false

SQLite's documentation on IN: "The IN and NOT IN operators take an expression on the left and a list of values or a subquery on the right."

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 in_list.sql:

in_list.sql
CREATE TABLE tickets (
title TEXT NOT NULL,
priority TEXT NOT NULL CHECK (priority IN ('low', 'medium', 'high'))
);

INSERT INTO tickets (title, priority) VALUES ('Printer jam', 'low');
INSERT INTO tickets (title, priority) VALUES ('Server down', 'high');
INSERT INTO tickets (title, priority) VALUES ('Coffee machine', 'urgent');

SELECT * FROM tickets;

The in_list.sql script in a macOS-style code window: a tickets table whose priority column has CHECK (priority IN ('low', 'medium', 'high')), inserts with the priorities low, high and urgent, and a SELECT of every row.

  • priority IN ('low', 'medium', 'high') lists the three allowed priorities.
  • CHECK (...) turns that list into a rule for every row.
  • The first two tickets use allowed values. The third uses 'urgent', which isn't on the list.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table helpdesk.db ".read in_list.sql"

sqlite3 opens the database file helpdesk.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 8 of in_list.sql: CHECK constraint failed: priority IN ('low', 'medium', 'high')
+-------------+----------+
| title | priority |
+-------------+----------+
| Printer jam | low |
| Server down | high |
+-------------+----------+

The output in a terminal: an error near line 8 of in_list.sql, CHECK constraint failed: priority IN ('low', 'medium', 'high'), then the tickets table with Printer jam at low and Server down at high.

low and high are on the list, so those tickets went in. urgent isn't, so SQLite refused the coffee machine ticket and quoted the rule in the error.

Common Mistakes​

MistakeWhat happensDo this instead
Using a different case, 'High'Refused with the same error: 'High' isn't 'high', because text comparison is case-sensitiveUse the exact values from the list
A CHECK with IN on a column that allows NULLA ticket with no priority went in: NULL IN (...) isn't false, so it passesAdd NOT NULL when the value is required

Troubleshooting​

SymptomLikely causeFix
CHECK constraint failed: priority IN ('low', 'medium', 'high')The value isn't on the list, or differs in caseUse one of the listed values
Parse error near line 1 of in_list.sql: table tickets already existsThe script already ran on helpdesk.dbDelete helpdesk.db and run the script again

FAQ​

Can I use IN outside a CHECK?​

Yes, anywhere a condition goes. SELECT title FROM tickets WHERE priority IN ('medium', 'high'); returned Server down.

How do I say "not one of these"?​

With NOT IN. WHERE priority NOT IN ('medium', 'high') returned Printer jam.

How do I add a value to the list later?​

Give the check a name when you create the table, with CONSTRAINT <name> in front of CHECK: priority TEXT NOT NULL CONSTRAINT valid_priority CHECK (priority IN ('low', 'medium', 'high')). Since SQLite 3.53.0, ALTER TABLE can then drop the check by its name and add the new list:

SQL
ALTER TABLE tickets DROP CONSTRAINT valid_priority;
ALTER TABLE tickets ADD CONSTRAINT valid_priority
CHECK (priority IN ('low', 'medium', 'high', 'urgent'));

After that, a ticket with urgent went in. DROP CONSTRAINT needs the name, so it can't remove a check without one, like the one in this guide, and adding a named check leaves the old check in force: urgent was still refused. The 3.54 shell prints an empty table headed sqlite_fail('cons... after the ADD CONSTRAINT. It isn't an error: Fixed: "sqlite_fail('constraint valid_priority already exists', 1)" After ALTER TABLE ADD CONSTRAINT in SQLite explains it.

Conclusion​

CHECK (priority IN ('low', 'medium', 'high')) let the low and high tickets in and refused urgent, so every priority in the table comes from the list.

Additional Resources​