You are on page 1of 17

CSC371 Database Systems 1 Lecture# 10

Credit Hours: 4 (3+1)


Teacher: Mr. Amjad Usman
CUI WAH Email: amjadusman@ciitwah.edu.pk

Relational Data Model


(Part 2)
Week # 05
Outline
CUI WAH

In this lecture, you will learn about:


What is a constraint?
Entity Integrity Constraints
Domain Constraints
Referential Integrity Constraints

Prepared By Mr. Amjad Usman 2


Relational Model (Recap)
CUI WAH

Relational Model
Relation, Tuple, Attribute, Domain
Record Identification Keys
Record Relationship Keys
Relational database (RDB)
Relational Database Management System (RDBMS)

Prepared By Mr. Amjad Usman 3


Keys in Relational Model
CUI WAH

Prepared By Mr. Amjad Usman 4


Superkey is StudID+RollNo+Email
CUI WAH

Prepared By Mr. Amjad Usman 5


Foreign Keys - Example
CUI WAH

Department Employee
DID DName EID EName DID
13 Marketing 15 John Smith 13
14 Accounts 16 Mary Brown 14
15 Personnel 17 Mark Jones 13
18 Jane Smith NULL

{DID} is a Candidate Key {DID} is a Foreign Key in Employee -


for Department - Each each Employee’s DID value is either
entry has a unique value NULL, or matches an entry in the
for DID Department relation. This links each
Employee to (at most) one Department

Prepared By Mr. Amjad Usman 6


Constraint Checking
CUI WAH

Automatically checked by DBMS


Protects database from errors / wrong data entry
Enforces enterprise rules

Prepared By Mr. Amjad Usman 7


Integrity Constraints
CUI WAH

The relational data model includes several types of constraints,


or rules limiting acceptable values and actions,
whose purpose is to facilitate maintaining the accuracy and integrity of
data in the database.
The major types of integrity constraints are
domain constraints
entity integrity
referential integrity

Prepared By Mr. Amjad Usman 8


Domain Constraints
CUI WAH

All of the values that appear in a column of a relation must be


from the same domain.
A domain is the set of values that may be assigned to an
attribute.
A domain definition usually consists of the following
components: domain name, meaning, data type, size (or
length), and allowable values or allowable range (if
applicable).

Prepared By Mr. Amjad Usman 9


Domain Constraints (cont…)
CUI WAH

Prepared By Mr. Amjad Usman 10


Domain Constraints (cont…)
CUI WAH

Domain constraint

Prepared By Mr. Amjad Usman 11


Entity Integrity
CUI WAH

The entity integrity rule is designed to ensure that every


relation has a primary key and that the data values for
that primary key are all valid.
It guarantees that every primary key attribute is non-null.

Prepared By Mr. Amjad Usman 12


Entity Integrity (cont…)
CUI WAH

Primary keys
can never have
NULL values

Prepared By Mr. Amjad Usman 13


Referential Integrity
CUI WAH

A referential integrity constraint is a rule that maintains


consistency among the rows of two relations.

The rule states that if there is a foreign key in one relation, either
each foreign key value must match a primary key value in
another relation or the foreign key value must be null.

Prepared By Mr. Amjad Usman 14


Referential Integrity Constraint (Example)
CUI WAH

A2
A1 v3
v1 v5
v2 v1
v3 v6
v4 v2
null v7
v3 v4
R1 R2
Foreign key
(or key reference) Primary key

Prepared By Mr. Amjad Usman 15


Foreign Key & Referential Integrity
CUI WAH

 It is not mandatory for a table to have a foreign key.


 A table may have more than one foreign key.
 Depends upon the relationships a table have with other tables.
 Value of Foreign key:
 It may be NULL or contain any value.
 The value of a foreign key MUST match with the primary key of related table.
 The foreign key may contain NULL, Unique or Duplicate values
 It depends upon the business rules

Prepared By Mr. Amjad Usman 16


THANK YOU SO MUCH
CUI WAH

Prepared By Mr. Amjad Usman 17

You might also like