Part3 - Ch6 - The Relational Database Constraints
Part3 - Ch6 - The Relational Database Constraints
Outline:
1
6.1 Relational Integrity Constraints
Constraints
o Restrictions on the actual values in a database state. (or conditions that must hold on all
valid relation states)
o Derived from the rules in the miniworld that the database represents
Domain Constraints
Every value in a tuple must be from the domain of its attribute (or it could be null, if allowed
for that attribute). A data type or format is also specified for each domain.
Special case of this constraint Constraints on Null (not allowed null values for attribute)
Key Constraints
For any two distinct tuples t1 and t2 in a relation state r of R we have t1SK t2SK (the
subset of attributes SK is called a superkey of the relation schema R).
A superkey SK specifies a uniqueness constraint.
Entity Integrity
The primary key attributes PK of each relation schema R in S cannot have null values in any
tuple of r(R). t[PK] ≠ null for any tuple t in r(R)
o This is because primary key values are used to identify the individual tuples.
o If PK has several attributes(composite), null is not allowed in any of these attributes
Referential Integrity
A constraint involving two relations (the previous constraints involve a single relation). Used
to specify a relationship among tuples in two relations: the referencing relation and the
referenced relation.
Referential Integrity is used to maintain the consistency among tuples of the two relations.
The referential integrity constraint states that a tuple in one relation that refers to another
relation must refer to an existing tuple in that relation.
2
Tuples in the referencing relation R1 have attributes FK (called foreign key attributes) that
reference the primary key attributes PK of the referenced relation R2. A tuple t1 in R1 is said to
reference a tuple t2 in R2 if
t1[FK] = t2[PK]
The value in the foreign key column (or columns) FK of the referencing relation R1 can be
either:
1. A value of an existing primary key value of acorresponding primary key PK in the
referenced relation R2, or
2. A null (if it is not part of its own primary key)
3
6.2 Update Operations and Dealing with Constraint Violations
Operations of the relational model:
1. Updates:
o Insert - to insert a new tuple or tuples in a relation,
o Delete - to delete tuples from a relation, and
o Update (or Modify) - to change the values of some attributes in existing tuples.
2. Retrievals - these operations do not change the current state of the databases.
Integrity constraints should not be violated by the update operations: INSERT, DELETE,
MODIFY
In case of integrity violation, several actions can be taken, such as:
o Cancel the operation that causes the violation (RESTRICT or REJECT option)
o Trigger additional updates so the violation is corrected (CASCADE option, SET NULL
option)
Insert Operation
o Domain constraint: if one of the attribute values provided for the new tuple is not of
the specified attribute domain.
o Key constraint: if the value of a key attribute in the new tuple already exists in another
tuple in the relation
o Entity integrity: if the primary key value is null in the new tuple
o Referential integrity: if a foreign key value in the new tuple references a primary key
value that does not exist in the referenced relation
o If an insertion violates one or more constraints, the default option is to reject the
insertion.
Delete Operation
DELETE may violate only referential integrity: If the primary key value of the tuple being
deleted is referenced from other tuples in the database
Can be remedied by several actions: RESTRICT, CASCADE ,SET NULL
o RESTRICT option: reject the deletion
o CASCADE option: delete the tuples that reference the tuple that is being deleted
o SET NULL option: set the foreign keys of the referencing tuples to NULL
One of the above options must be specified during database design for each foreign key
constraint
4
Update Operation
Which relational constraints would be violated if the following operation were applied
to the COMPANY database snapshot illustrated bellow?
Employee
Department
DNo DName
10 Headquarter
20 Accounting
30 IT
Insert
2. Insert < 'Jasmine', 'Rani', 321, 3/9/1982, 3000, NULL, 30> into EMPLOYEE.
3. Insert < 'Jasmine', 'Rani', 923, 3/19/1980, '3000JD', NULL, 30> into EMPLOYEE.
4. Insert <123456, 'Rani', 321, 3/9/1985, '3000$', 432, 10> into EMPLOYEE.
5
6. Insert <123456, 'Rani', 123, 3/9/1985, '3500JD', 432, 10> into EMPLOYEE.
8. Insert < 'Jasmine', 'Rani',NULL, 30/9/1977, '4000JD', 587, 10> into EMPLOYEE.
10. Insert < 'Jasmine', 'Rani',222, 3/2/1980, 3000, 587, 50> into EMPLOYEE .
11. Insert < 'Alice', 'Rani',222, 30/2/1976, 3000, 687, 10> into EMPLOYEE.
12. Insert < 'Jasmine', 'Rani',NULL, 3/22/1976, '1000JD', 587, 50> into EMPLOYEE.
13. Insert < 'Sali', 'Rani', 'NULL', 3/2/1969, '6000JD', 587, 40> into EMPLOYEE.
14. Insert < 'Jasmine', 'Rani', 123, 3/2/76, '3200JD', 587, 60> into EMPLOYEE.
Delete
1. Delete the DEPARTMENT tuple with DNo =20
6
3. Delete the EMPLOYEE tuple with SSN=432
Modify
3. Update the DNO of the DEPARTMENT tuple with DName ='IT' to 40.
4. Update the DNO of the DEPARTMENT tuple with DName ='IT' to 10.
5. Update the SSN of the EMPLOYEE tuples with FName ='Smith' to NULL.