SQLite CHECK Constraint
CHECK
CHECK makes sure that a value meets a condition. In this example, every person must be 18 or older:
Example
CREATE TABLE IF NOT EXISTS Persons (
PersonID INTEGER PRIMARY KEY,
Name TEXT,
Age INTEGER CHECK (Age >= 18)
);
Try it Yourself »
Breaking the Rule
Example
Try to insert a person who is 15:
CREATE TABLE IF NOT EXISTS Persons (
PersonID INTEGER PRIMARY KEY,
Name TEXT,
Age INTEGER CHECK (Age >= 18)
);
INSERT INTO Persons (Name, Age)
VALUES ('Sofie', 15);
Try it Yourself »
CHECK on More Than One Column
A table constraint can check more than one column. Give the constraint a name, so the error message tells you which rule was broken:
Example
CREATE TABLE IF NOT EXISTS Bookings (
BookingID INTEGER PRIMARY KEY,
StartDate TEXT,
EndDate TEXT,
CONSTRAINT ValidDates
CHECK (EndDate >= StartDate)
);
Try it Yourself »