5 DB erToRel
5 DB erToRel
5 DB erToRel
Database Systems
Mapping ER Models to
Relational Schemas
Werner Nutt
1
Conceptual and Logical Design
name
Relational Model:
2
Mapping an E-R Diagram to a
Relational Schema
We cannot store date in an ER schema
(there are no ER database management systems)
3
Translation: Principles
• Maps
– ER schemas to relational schemas
– ER instances to relational instances
• Difficulties:
– what to do with ER-instances that have identical
attribute values, but consist of different entities?
– in which way do we want to preserve information?
4
Mapping Entity Types to Relations
• For every entity type create a relation
• Every atomic attribute of the entity type becomes a relation attribute
• Composite attributes: include all the atomic attributes
• Derived attributes are not included
(but remember their derivation rules)
• Relation instances are subsets of the cross product
of the domains of the attributes
• Attributes of the entity key make up the primary key of the relation
given family
courseno
no. of
students equip
name
STUDENT COURSE
studno subject
5
Mapping Entity Types to Relations (cntd.)
given family
courseno
no. of
students equip
name
STUDENT COURSE
studno subject
6
Mapping Many:many Relationship Types
to Relations
Create a relation with the following set of attributes:
N (degree of relationship)
U primary_key(Ei) U {a1,…,aM}
i=1
primary keys of each
entity type participating attributes of the
in the relationship relationship type (if any)
given family
courseno
no. of
name labmark students equip
given family
courseno
no. of
name labmark students equip
8
Mapping Many:one Relationship Types to Relations
given family name
slot
roomno
studno name
m 1
STUDENT TUTOR STAFF
E1 U primary_key(E2) U {a1,…,an}
The relation
STUDENT(studno, givenname, familyname)
is extended to
STUDENT(studno, givenname, familyname, tutor, roomno, slot)
and the constraint
Foreign Key STUDENT(tutor,roomno) references STAFF(name,roomno)
10
Mapping Many:one Relationship Types
to Relations (cntd.)
STUDENT STAFF
studno given family tutor roomno slot name roomno
s1 fred jones bush 2.26 12B kahn IT206
s2 mary brown kahn IT206 12B bush 2.26
s3 sue smith goble 2.82 10A goble 2.82
s4 fred bloggs goble 2.82 11A zobel 2.34
s5 peter jones zobel 2.34 13B watson IT212
s6 jill peters kahn IT206 12A woods IT204
capon A14
lindsey 2.10
barringer 2.125
11
Mapping Many:one Relationship Types
to Relations (cntd.)
given family name
slot
roomno
studno name
m 1
STUDENT TUTOR STAFF
Another Idea: If
• the relationship type is optional to both entity types, and
• an instance of the relationship is rare, and
• there are many attributes on the relationship then…
… create a new relation with the set of attributes:
given family
hons faculty
studno name
1
STUDENT REG SCHOOL
SCHOOL
hons faculty No student is registered for “mi”,
ac accountancy so “mi” doesn’t occur
is information systems as a foreign key value
cs computer science (but that’s no problem)
ce computer science
mi medicine
16
ma mathematics
Optional Participation of the
Determinant Entity (‘many end’)
17
Optional Participation of the
Determinant Entity
1. STUDENT (studno, givenname, familyname, tutor, roomno, slot)
STAFF(name, roomno)
Integrity constraint:
STUDENT STAFF
studno given family tutor roomno slot name roomno
s1 fred jones bush 2.26 12B kahn IT206
s2 mary brown kahn IT206 12B bush 2.26
s3 sue smith goble 2.82 10A goble 2.82
s4 fred bloggs goble 2.82 11A zobel 2.34
s5 peter jones zobel 2.34 13B watson IT212
s6 jill peters kahn IT206 12A woods IT204
capon A14
lindsey 2.10
barringer 2.125
19
Optional Participation of the
Determinant Entity (cntd.)
STUDENT STAFF
studno given family tutor roomno slot name roomno
s1 fred jones bush 2.26 12B kahn IT206
s2 mary brown kahn IT206 12B bush 2.26
s3 sue smith goble 2.82 10A goble 2.82
s4 fred bloggs goble 2.82 11A zobel 2.34
s5 peter jones NULL NULL NULL watson IT212
s6 jill peters kahn IT206 12A woods IT204
capon A14
lindsey 2.10
barringer 2.125
20
Mapping One:one Relationship Types
to Relations
• Post the primary key YEAR
year
of one of the entity year yeartutor
YEAR types into the other 1 zobel
entity type as a 2 bush
1
foreign key, including 3 capon
YEARTUTOR any relationship
attributes with it STAFF
1
name
STAFF
name roomno year
or kahn IT206 NULL
roomno
bush 2.26 2
• Merge the entity goble 2.82 NULL
zobel 2.34 1
types together watson IT212 NULL
woods IT204 NULL
Which constraint capon A14 3
lindsey 2.10 NULL
holds in this case? barringer 2.125 NULL21
Multi-Valued Attributes
For each multi-valued attribute of Ei, create a relation with the attributes
STUDENT
studno given family
given family s1 fred jones
s2 mary brown
studno name
STUDENT
STUDENT_CONTACT
studno contact
s1 Mr. Jones
contact s1 Mrs Jones
s2 Bill Brown
s2 Mrs Jones 22
s2 Billy-Jo Woods
Mapping Roles and Recursive Relationships
name
STAFF
roomno
appraiser appraisee
APPRAISAL
APPRAISAL
STAFF STUDENT
1 m
EXAMINE
roomno
STAFF(name, roomno)
m orderid
• Weak entity type
• Identifying entity for ORDER
LINE_ITEM date
1
ORDER-MAKEUP
m
lineno
• Weak entity type LINE_
ITEM quantity
26
Mapping Weak Entities to Relations
Create a relation with the attributes:
n
primary_key(E0) U U discriminator(Ei) U {a1,…,an}
i=1
c-name
CUSTOMER
address
1
CUST-ORDER
m orderid
ORDER
27
date
Association Entity Types
An entity type that represents a relationship type:
given family
courseno
equip
name
m studno
STUDENT COURSE
1 subject 1
m m
STUD_ENROL ENROL COURSE_ENROL
labmark exammark
28
Association Entity Types
We have:
• COURSE(courseno, subject, equip)
• STUDENT(studno, givenname, familyname)
given family
courseno
equip
name
m studno
STUDENT COURSE
1 subject 1
m m
STUD_ENROL ENROL COURSE_ENROL
labmark exammark
Then:
• ENROL(courseno, studno, labmark, exammark)
29
Translation of the University Diagram
ENROL
1 COURSE(courseno, subject, equip)
TUTOR
YEARTUTOR
exammark
slot STAFF(lecturer,roomno,
1 1
appraiser, approom)
name
n n STAFF
courseno
m
TEACH(courseno, lecturer,lecroom )
TEACH 1 m roomno
COURSE
subject
YEAR(year, yeartutor, yeartutorroom)
appraisee
appraiser
30
Exercise: Supervision of PhD Students
A database needs to be developed that keeps track of PhD students:
• For each lecturer store the name, staff ID and office number. Staff ID's
are unique.
31
Exercise: Supervision of PhD Students
• For each research topic store the title and a short description. Titles
are unique.
Tasks:
32
Translating of Hierarchies: Options
To store information about these classes, given family
We have to define appropriate relations.
name
∩
∩
year
three options:
UNDERGRADUATE POSTGRADUATE
∩
∩
Disjointness constraint: year
πkey(subclassi) ∩ πkey(subclassj) = ∅
UNDERGRADUATE POSTGRADUATE
34
for i ≠ j
Translation into Relations: Option B
Create only relations for subclasses. Each subclass becomes a
relation over the set of attributes:
attributes of superclass U attributes of subclass
The key for each subclass relation is: primary_key(superclass)
∩
∩
year
relations
Codd: union that extends the UNDERGRADUATE POSTGRADUATE
35
schema to all common attributes
Translation into Relations: Option C
Create a single relation over the set of attributes
n
attributes of superclass U ∪ attributes of subclassi U { class }
i=1
∩
∩
• Overlapping coverage: class year
has to represent a set of classes
• Partial coverage: class is null UNDERGRADUATE POSTGRADUATE
36
∴ entity is from superclass
Applying the Three Translations
(Overlapping Coverage)
A. STAFF(payrollno, name, lengthOfService)
ACADEMIC(payrollno, level)
payroll no TECHNICAL(payrollno, project)
name
ADMIN(payrollno, grade)
STAFF
∩
dept
d task ∩
title Manager Hourly Staff
∩
∩ ∩ salary
∩
Academic Technical Admin Salaried Staff
∩
∩
phone
expertise
Admin Manager
Books:
• A First Course in Database Systems, by J. Ullman and J. Widom
• Fundamentals of Database Systems, by R. Elmasri and S. Navathe
39