Database Systems (ISYS1001/ISYS5008)
Database Systems (ISYS1001/ISYS5008)
Database Systems
( ISYS1001/ISYS5008)
Lecture 4
Discipline of Computing
School of Electrical Engineering, Computing and Mathematical Sciences (EECMS)
telephone
We place restrictions on data to help the DBMS keep the data valid.
Referential integrity constraint is specified between two relations and
is used to maintain the consistency among tuples in the two
relations.
Informally, 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.
Foreign keys enforce referential integrity constraints
Foreign key attributes in R1 referring to R2 have the following rules:
The FK attributes in R1 have the same domain(s) as the primary
key attributes if R2
The value of foreign key in tuple t1 in R1 must referrer to an
existing primary key value in tuple t2 of R2
SQL allows us to specify the referential integrity constraints on
tables.
Example:
firsttName 9
courseName
studentID
lastName duration
N 1
Student registers -in course
Student(studentID,firstName,secondName,courseName)
Course(courseName,duration)
N 1
Student registers -in Course
Student(studentID,firstName,secondName,courseName)
Course(courseName,duration)
empID Example 2:
manager 1
manages
name
N
subordinate
Employee
Employee(empID,name,manager_empID)
Steps to map ER model to relational schema 12
N N
Book Purchase Customer
pageCount
purchasing address
cost
Date
Book(isbn,title,pageCount)
Customer(customerName,address)
Purcahse(isbn,customerName,cost,purchasingDate)
Steps to map ER model to relational schema 14
Step 4 : Each 1:1 relationship can be combined with either side of
the entity type or can be treated like 1: N relationship type.
However, if both sides of the relationship are with partial
participation, two entities become two relations.
Example: firstName
reportID
studentID
lastName title
1 1
Student writes ProjectReport
Student(studentID,firstName,lastName, reportId,title)
or
ProjectReport(reportId,title,studentID,firstName,lastName)
N 1
Player PlaysOn Team
Identifying entity
Player(teamName,number,name)
Team(teamName,manager)
Steps to map ER model to relational schema 16
N N
Book Reads Author
pageCount
authorTel authName
date
Book(isbn,title,pageCount)
Customer(name,address)
Author(authNme, authorTel)
Reads(isbn,name,authName,date)
Relational mapping : more tips 18
In the examples above, we have mapped tables considering one type of relationship at
a time. However, when translating the ER to relational schema, ER diagram as a whole
needs to be considered.
Multi-values attributes:
Relational model does not support multi-values attributes in the concerned relation.
It can be handled either by:
a) creating a separate relation to keep the multi-valued attributes and link it to the other relation via a
foreign key.
Composite attributes:
Each composite attribute become attributes of the relation.
M
meets-in
Student take
N time
Unit
phone
studnetNo
name unitNo
crePts
title
20
Revising the relational schema
The above mapping process resulted in a set of relations, which are
the candidate relations to be used in actual database
implementation.
When entities, attributes and relationships among them are identified
to capture the concerned scenario and ER model is created
properly, and the ER to relational mapping is performed using
standard process, resulting relational schema would be in a
standard form which reduces duplicate data and avoid anomalies.
This standard form is called “normalized” database model.
The relations in normalized form aims at making sure redundant data are
avoided and maintaining integrity of data when update/ delete data (
i.e.anomalies are reduced) in related relations.
Generally, an ER model will lead to a normalized database model,
however there can be issues which we have not seen in the ER
modelling or mapping stages.
Why schema refinement is required? 21
In complex scenarios, identifying the attributes and keys to be kept in an
entity might be not done properly.
Sometimes, without following the ER design to relational schema
approach,relations would be defined.
Therefore, after identifying candidate relations ,(a) we may have to
check whether the relations are in a proper form, and (b) if any
weaknesses are identified, a schema refinement would be required.
Example:
dName dLocation
salary
eId
designation
eName
Employee(eId,eName,designation,salary,dName,dLoc,dId)
Sample table with data for the relation 22
Employee(eId,eName,designation,salary,dName,dLoc,dId)
Insertion Anomaly
E.g. Inserting a new employee to the Employee table
Department information is repeated
E.g. Inserting a department with no employees
Impossible since eID cannot be null ( key)
Deletion Anomaly
E.g. Deleting the last employee from the department
This will lead to loosing information about the department
Update Anomaly
E.g. Updating the department’s location
needs to be done for all employees working for that department
Functional dependencies 24
(studentID,courseID) dateStarted
Solution:
A → A (reflexivity rule)
A → B (given)
A → B and B → C then A → C (transitivity)
A → B and B → D then A → D (transitivity)
A → ABCD (Union rule)
A+={ABCD}
We can find that, B+= {BCD} , C+ = {C} , D+={D}
A B C D
Partial dependency
32
Definitions useful in normalization
A B C E
Step1:
Step3:
Verification
Step 2: Decomposition
A B C D E
A B C E transitive dependency
2NF
A B C D E
Unit
phone
studnetNo
name unitNo
crePts
title
Real world to database 48
Analysis of
Logical Physical
required data
database database
and data design
design
modelling Relational Relational
High level database database
design schema
Problem
domain