Sqlite

SQLite CHECK Constraint

SQLite CHECK Constraint

In this tutorial, we will learn about SQLite CHECK constraint. The constraint specifies a condition that must be satisfied for the column data.

A CHECK constraint can be defined for a column to ensure valid data values are inserted or updated.

SQLite CHECK Example

Let’s understand the concept with the help of an example. The CREATE TABLE statement defines the simple voter table with three columns. The age column has a CHECK constraint with a condition.

CREATE TABLE voter(
voter_id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER CHECK ( age > 21 and age < 100));

 

Consider the two insert statements for the table:

INSERT INTO voter VALUES(1,’Emma’,52);
INSERT INTO voter VALUES(1,’John’,15);

SQLite CHECK Constraint

The following INSERT statement violates the CHECK constraint defined in the age column.

INSERT INTO voter VALUES(1,’John’,15);

The valid range for the voter’s age is age > 21 and age < 100. Since John’s age is less than 21, his record violates the check constraint, and his record is not permitted.

 

SQLite Tutorials

SQLite tutorials on this website: