0% found this document useful (0 votes)
128 views

Part3 - Ch6 - The Relational Database Constraints

The document outlines the key constraints that define relational database integrity, including domain constraints, key constraints, entity integrity constraints, referential integrity constraints, and semantic integrity constraints. It describes how each type of constraint works and can be violated through insert, delete, and update operations. Violations are typically handled by restricting or rejecting the operation, cascading related updates, or setting referenced values to null. The document provides examples of how specific operations could violate constraints in sample Employee and Department tables.

Uploaded by

Layan Alajo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Part3 - Ch6 - The Relational Database Constraints

The document outlines the key constraints that define relational database integrity, including domain constraints, key constraints, entity integrity constraints, referential integrity constraints, and semantic integrity constraints. It describes how each type of constraint works and can be violated through insert, delete, and update operations. Violations are typically handled by restricting or rejecting the operation, cascading related updates, or setting referenced values to null. The document provides examples of how specific operations could violate constraints in sample Employee and Department tables.

Uploaded by

Layan Alajo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Part3: The Relational Data Model and SQL

Chapter 6: The Relational Database Constraints.

Outline:

6.1 Relational Integrity Constraints


 Domain constraints
 Key constraints
 Entity integrity constraints
 Referential integrity constraints
 Semantic Integrity Constraints

6.2 Update Operations and Dealing with Constraint Violations


 Insert Operation
 Delete Operation
 Update Operation

Prepared by:Eng. Randa Al_Dallah and Sawsan Abu_Taleb

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

 There are five types of constraints in the relational model:


1. Domain constraints
2. Key constraints
3. Entity integrity constraints
4. Referential integrity constraints
5. Semantic Integrity Constraints

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 t1SK  t2SK (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.

Prepared by:Eng. Randa Al_Dallah and Sawsan Abu_Taleb

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]

 A referential integrity constraint can be displayed in a relational database schema as a directed


arc from R1 FK to R2 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)

Semantic Integrity Constraints

 Based on application semantics and cannot be expressed by the model.


 Example1: the max. no. of hours per employee for all projects he or she works on is 56 hrs per
week.
 Example2: the salary of an employee should not exceed the salary of the employee’s supervisor.
 SQL-99 allows triggers and ASSERTIONS to express for some of these

Prepared by:Eng. Randa Al_Dallah and Sawsan Abu_Taleb

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

 INSERT may violate any of the constraints:

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

Prepared by:Eng. Randa Al_Dallah and Sawsan Abu_Taleb

4
Update Operation

 It may violate any of the constraints.

Which relational constraints would be violated if the following operation were applied
to the COMPANY database snapshot illustrated bellow?

Employee

FName LName SSN BDate Salary SuperSSN DNo


Smith Jone 123 1/2/1979 2100JD NULL 10
Susan Adams 432 3/6/1982 1547JD 123 10
James Ford 587 20/9/1980 3650JD 983 20
Sali Miller 983 1/12/1970 890JD 123 30

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. Insert <400,'Planning'> into DEPARTMENT.

Prepared by:Eng. Randa Al_Dallah and Sawsan Abu_Taleb

5
6. Insert <123456, 'Rani', 123, 3/9/1985, '3500JD', 432, 10> into EMPLOYEE.

7. Insert <30,'Planning'> into DEPARTMENT.

8. Insert < 'Jasmine', 'Rani',NULL, 30/9/1977, '4000JD', 587, 10> into EMPLOYEE.

9. Insert <NULL,'Planning'> into DEPARTMENT.

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

2. Delete the EMPLOYEE tuple with FName ='Smith'

Prepared by:Eng. Randa Al_Dallah and Sawsan Abu_Taleb

6
3. Delete the EMPLOYEE tuple with SSN=432

Modify

1. Update the SALARY of the EMPLOYEE tuple with SSN=432 to 2800.

2. Update the DNO of the EMPLOYEE tuple with SSN=432 to 70.

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.

6. Update the SSN of the EMPLOYEE tuples to 566.

Prepared by:Eng. Randa Al_Dallah and Sawsan Abu_Taleb

You might also like