Site icon TestingDocs.com

SQLite CHECK Constraint

Overview

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

SQLite CHECK Constraint

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

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);

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:

https://www.testingdocs.com/sqlite-database-tutorials/

For more information on SQLite, visit the official website:

https://www.sqlite.org/

Exit mobile version