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(E ) i 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
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}
relation attributes on
primary key for E2,
for entity the 9
is now a foreign key to
E1 relationship
E2
Mapping Many:one Relationship
Types
to Relations: Example
given family name
slot
roomno
studno name
m 1
STUDENT TUTOR STAFF
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:
1
STUDENT REG SCHOOL
SCHOOL
hons faculty No student is registered for
ac accountancy “mi”, so “mi” doesn’t occur
is information systems as a foreign key value
cs computer science (but that’s no
ce computer science problem)
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 NULL NULL NULL watson IT212
s6 jill peters kahn IT206 12A woods IT204
capon A14
lindsey 2.10
barringer 2.125
19
Mapping One:one Relationship Types
to Relations
• Post the primary key YEAR
yea
r 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
capon A14 3
Which constraint lindsey 2.10 NULL
holds in this case? barringer 2.125 NULL20
Multi-Valued Attributes
For each multi-valued attribute of Ei, create a relation with
the attributes
STUDENT
STUDENT_CONTACT
studno contact
s1 Mr. Jones
contact s1 Mrs Jones
s2 Bill Brown
s2 Mrs Jones 21
s2 Billy-Jo Woods
Mapping Roles and Recursive
Relationships
name
STAFF
roomno
appraiser appraise
e
APPRAISAL
APPRAISAL
STAFF(name, roomno,
appraiser, approomno )
22
Multiple Relationships between
Entity Types
1. Treat each relationship type separately
2. Represent distinct relationships by different foreign keys
drawing on the same relation
given family
name
1 SUPERVISE m name
studno
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
25
Mapping Weak Entities to
Relations
Create a relation with the attributes:
n
primary_key(E0) U U discriminator(Ei) U {a1,…,an}
i=1
Primary key of Discriminators of Attributes of the
identifying identifying weak weak entity
strong entity type entity types type
c-name
CUSTOMER
address
1
CUST-ORDER
m orderid
ORDER
26
date
Association Entity Types
An entity type that represents a relationship
type:
given family
courseno
equip
name
m studno
STUDENT COURSE
1 subje 1
ct
m m
STUD_ENROL ENROL COURSE_ENROL
labmark exammark
27
Association Entity Types
We have:
• COURSE(courseno, subject, equip)
• STUDENT(studno, givenname, familyname)
given family
courseno
equip
name
m studno
STUDENT COURSE
1 subje 1
ct
m m
STUD_ENROL ENROL COURSE_ENROL
labmark exammark
Then:
• ENROL(courseno, studno, labmark, exammark) 28
Translation of the University
Diagram
given family 1 STUDENT
SCHOOL
(studno, givenname,
studno name REG
hons familyname, hons,
faculty
STUDENT
tutor, tutorroom, slot,
year
year)
m YEARREG
1
ENROL(studno, courseno,
labmark YEAR
labmark,exammark)
1
ENROL TUTOR
COURSE(courseno, subject,
YEARTUTOR
exammark
slot equip)
1 1
name STAFF(lecturer,roomno,
courseno
n
m
n STAFF appraiser,
COURSE
TEACH 1 m roomno approom)
subject appraisee
appraiser TEACH(courseno,
equip APPRAISAL
lecturer,lecroom )
YEAR(year, yeartutor,
29
yeartutorroom)
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.
Tasks:
year
year
Disjointness constraint:
UNDERGRADUATE POSTGRADUATE
33
key(subclassi) key(subclassj) =
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)
that are
not in a subclass are lost
• Overlapping classes: d
thesis title
redundancy year
year
subclass the tuple
represents UNDERGRADUATE POSTGRADUATE
35
• Overlapping coverage: class
Applying the Three Translations
(Overlapping Coverage)
A. STAFF(payrollno, name,
lengthOfService)
payroll no ACADEMIC(payrollno, level)
name TECHNICAL(payrollno, project)
STAFF
ADMIN(payrollno, grade)
length of service
B. ACADEMIC(payrollno, name,
lengthOfService,
o
level)
level TECHNICAL(payrollno, name,
grade lengthOfService,
project)
ACADEMIC TECHNICAL ADMIN
ADMIN ADMIN(payrollno, name,
lengthOfService, grade)
project
C. STAFF(payrollno, name,
lengthOfService, level,
36
project, grade, class1,
class2, class3)
Specialisation Lattice with Shared
Subclass
payroll no
Staff
no_hours
d
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
38