4 types of constraints are:
1. Domain constraint
2. Key constraint
3. Entity Integrity constraint
4. Referential Integrity Constraint
Consider the examples below and state which constraints were violated and why? [All primary
keys(pk) are underlined and foreign keys(fk) are shown with arrows from fk to pk]
Example 1
Student_ID Name Age
001 Zainab 19
002 Hazzaz A
003 Faiza 20
004 Amanat 18
Answer: Domain constraint, because age should have integers, second row has a character.
Example 2
Student_ID Name Age
001 Azibun 19
002 1.5 20
null Partho 20
004 Jabed 18
Answer: Domain and Entity Integrity. Name has a double value so domain constraint
violated and student_id is primary key but a null value exists so entity integrity
constraint has been violated.
Example 3
Student_ID Name Age
001 Wasema 19
002 Junaid 20
001 Afia 20
004 Hasibul 18
Answer: Key constraint as duplicate student_id exists even though student_id is primary
key.
Example 4
Student_ID Course_Code Grade
001 CSE370 3.7
002 CSE221 4.0
003 CSE110 null
004 CSE220 3.0
Answer: no constraint has been violated.
Example 5
(foreign key)
Student_ID Course_Code Grade
001 CSE370 3.7
002 CSE221 4.0
003 null 3.3
004 CSE220 3.0
Course_Code Course_Title
CSE370 Database
CSE221 Algorithm
CSE220 Data Structure
Answer: Entity Integrity violated. course_code is part of the primary key, therefore it
cannot be null.
Example 6
Student_Grades (foreign key)
Student_ID Course_Code Grade
001 CSE370 3.7
002 CSE221 4.0
001 CSE110 null
004 CSE220 3.0
Course
Course_Code Course_Title
CSE370 Database
CSE221 Algorithm
CSE220 Data Structure
Answer: Referential Integrity violated. Course_code in “Student_Grades” is a foreign
key, the value CSE110 does not exist in the referenced table “Course”
Example 7
1. Update the course_code of Database course from CSE370 to CSE390 in the Course
table in Example 6.
2. Insert a row with the following values (1.5, ‘Discrete’) in the Course table in Example 6.
3. Delete the row from Student_Grades table in Example 6 where course_code=
‘CSE110’.
Answer:
1. Referential Integrity, cse370 es referenced in the foreign key column in
student_grades table
2. Domain constraint because course_code should be char/varchar/text, not
decimal/double
3. no violation.