query='CREATE TABLE students (\
student_id INTEGER PRIMARY KEY,\
name TEXT NOT NULL,\
age INTEGER CHECK(age>=5), \
email TEXT UNIQUE,\
class_id INTEGER)'
CREATE TABLE <table_name>(
column_name data_type check(expression),
column_name data_type)
While adding data the validation is done. Check this example.
query='''INSERT INTO `students` \
(`student_id`, `name`, `age`, `email`,`class_id`) VALUES \
(6,'abc',4,'abc6@example.com',9)'''
The output is here error: CHECK constraint failed: age>=5
We can increase the age value to 8, this is allowed.
query='''INSERT INTO `students` \
(`student_id`, `name`, `age`, `email`,`class_id`) VALUES \
(6,'abc',8,'abc6@example.com',9)'''
We will change the age column to include a range of allowed integer.
query='CREATE TABLE students (\
student_id INTEGER PRIMARY KEY,\
name TEXT NOT NULL,\
age INTEGER CHECK(age>=5 and age <=20), \
email TEXT UNIQUE,\
class_id INTEGER)'
This query will generate error
query='UPDATE students SET age = 22 WHERE student_id=2'
try:
r_set=my_conn.execute(query)
except sqlite3.Error as my_error:
print("error: ",my_error)
else:
print('No. of records added : ',r_set.rowcount)
error: CHECK constraint failed: age>=5 and age <=20
query='CREATE TABLE students (\
student_id INTEGER PRIMARY KEY,\
name TEXT NOT NULL,\
age INTEGER, \
email TEXT UNIQUE,\
class_id INTEGER,\
CONSTRAINT my_check1 CHECK(age>=5 and age <=20),\
CONSTRAINT my_check2 CHECK(class_id in (1,2,3)))'
Let us try to add record where value of class column is not within the accepted list
query='''INSERT INTO `students` \
(`student_id`, `name`, `age`, `email`,`class_id`) VALUES \
(7,'abc',8,'abc7@example.com',4)'''
try:
r_set=my_conn.execute(query)
except sqlite3.Error as my_error:
print("error: ",my_error)
else:
print('No. of records added : ',r_set.rowcount)
Output
error: CHECK constraint failed: my_check2
query='CREATE TABLE students (\
student_id INTEGER PRIMARY KEY,\
name TEXT NOT NULL,\
age INTEGER, \
email TEXT UNIQUE,\
class_id INTEGER )'
query='''INSERT INTO `students` \
(`student_id`, `name`, `age`, `email`,`class_id`) VALUES \
(7,'abc',8,'abc7@example.com','a')'''
try:
r_set=my_conn.execute(query)
except sqlite3.Error as my_error:
print("error: ",my_error)
else:
print('No. of records added : ',r_set.rowcount)
Output
No. of records added : 1
String data 'a' is allowed in a Integer coloumn in SQLite database. As per the SQLite manual it is not a bug.
query='CREATE TABLE students (\
student_id INTEGER PRIMARY KEY,\
name TEXT NOT NULL,\
age INTEGER, \
email TEXT UNIQUE,\
class_id INTEGER check(typeof(class_id)="integer") )'
query='''INSERT INTO `students` \
(`student_id`, `name`, `age`, `email`,`class_id`) VALUES \
(7,'abc',8,'abc7@example.com','a')'''
try:
r_set=my_conn.execute(query)
except sqlite3.Error as my_error:
print("error: ",my_error)
else:
print('No. of records added : ',r_set.rowcount)
error: CHECK constraint failed: typeof(class_id)="integer"
query='CREATE TABLE students (\
student_id INTEGER PRIMARY KEY,\
name TEXT NOT NULL,\
age INTEGER, \
email TEXT UNIQUE,\
class_id INTEGER check(typeof(class_id)="integer"),\
CONSTRAINT my_check1 CHECK(age>=5 and age <=20),\
CONSTRAINT my_check2 CHECK(class_id in (1,2,3)))'
Constraints in SQLite help maintain data integrity and consistency. Using constraints like PRIMARY KEY, UNIQUE, NOT NULL, CHECK, DEFAULT, and FOREIGN KEY ensures the accuracy and reliability of the data in your database.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.