CSE 480: Database Systems
description
Transcript of CSE 480: Database Systems
1
CSE 480: Database Systems
Lecture 6: ER to Relational Mapping
Reference:
Read Chapter 9 of the textbook
2
The ER diagram for COMPANY database
3
Mapping ER diagram into relational schema
4
Mapping ER diagram into relational schema
Map everything as separate tables
ProjNumber ProjName
ProjNumber ProjLocation
Query processing becomes expensive
E.g.: Find me the names of all projects located in Michigan
Need to perform many join operations
5
Mapping ER diagram into relational schema
Map everything into 1 table
1 1EMPLOYEE DEPARTMENT
MANAGES
WorksFor
1N
Too much redundancy
6
Outline
Mapping ER Constructs to Relations– Step 1: Mapping of Regular (Strong) Entity Types
– Step 2: Mapping of Weak Entity Types
– Step 3: Mapping of Binary 1:1 Relation Types
– Step 4: Mapping of Binary 1:N Relationship Types.
– Step 5: Mapping of Binary M:N Relationship Types.
– Step 6: Mapping of Multivalued attributes.
– Step 7: Mapping of N-ary Relationship Types.
The procedure– Avoids generating too many unnecessary tables
– Avoids too much redundancy in tables
– More details in Chapter 10 (normal forms)
7
Step 1: Mapping of Regular (Strong) Entity Types– For each strong entity type E, create a relation R
Include all the simple attributes of E as columns in R Include component attributes of a composite attribute as columns in R Ignore the derived attributes Choose one of the key attributes of E as the primary key for R.
– If the chosen key attribute of E is composite, the set of simple attributes that form it will together form the primary key of R.
ER-to-Relational Mapping Algorithm
E
ID B
C D
Entity type, E
ID C D
R
Relation, R
8
Example
PRIMARY KEY (Ssn)
9
Example
PRIMARY KEY (Dnumber)UNIQUE(Dname)
PRIMARY KEY (Pnumber)UNIQUE(Pname)
secondary keys(can be null)
10
ER-to-Relational Mapping Algorithm
Step 2: Mapping of Weak Entity Types– For each weak entity type W with owner entity type O, create a
relation R
Include all simple attributes of W as columns in R Include components of a composite attribute as columns in R Include primary key attribute(s) of the the owner entity type(s) O as
foreign key attributes of R Primary key of R is the combination of the primary key(s) of the
owner(s) and the partial key of the weak entity type W
O W1 N
Id
A
P
QR
S
11
ER-to-Relational Mapping Algorithm
Step 2: Mapping of Weak Entity Types
O W1 N
Id
A
P
QR
S
Oid P R
W
S
PRIMARY KEY (Oid, P)W(Oid) REFERENCES O(Id)
ON DELETE CASCADE
Id A
O
12
Example
EMPLOYEESsn
1
PRIMARY KEY(Essn, Dependent_name)
DEPENDENT(Essn) REFERENCES EMPLOYEE(Ssn)ON DELETE CASCADE
13
ER-to-Relational Mapping Algorithm
Step 3: Mapping of Binary 1-to-1 Relationship types
1 1A B
R
1 1A B
R
1 1A
R
Cross-reference approach: 3 Tables
Foreign key approach: 2 Tables
Merged relation approach: 1 Table
14
Cross-Reference Approach
Cross-reference approach:
1 1EMPLOYEE DEPARTMENT
MANAGES
StartDate
MANAGES
MANAGES(Dnumber) REFERENCES DEPARTMENT(Dnumber)MANAGES(Mgr_ssn) REFERENCES EMPLOYEE(Ssn)
15
Foreign Key Approach
1 1EMPLOYEE DEPARTMENT
MANAGES
StartDate
Foreign key approach:
DEPARTMENT(Mgr_ssn) REFERENCES EMPLOYEE(Ssn)
Mgr_ssn is NOTNULL
Total participation
16
Merged Relation Approach
Merged relation approach:
MANAGER
1 1MANAGER DEPARTMENT
MANAGES
StartDate
Total participation
DEPARTMENT_W_MANAGER
17
Summary (Binary 1-1 Relationships)
1 1S T
R
1 1S T
R
1 1S T
R
S
SID
R
SIDTIDP
T
TID
Cross-reference:
S
SID
T_R
TIDSIDP
Foreign key:
S_R_T
SIDTIDP
Merged relation:
P
P
P
18
ER-to-Relational Mapping Algorithm
Step 4: Mapping of Binary 1:N Relationship Types.
1 NS T
R
P
S
SID
T_R
TIDSIDP
T_R(SID) references S(SID)
19
Example
N 1EMPLOYEE DEPARTMENT
WORKS_FOR
SUPERVISES
Supervisor 1
Subordinate N
20
– For each binary 1:N relationship type R Identify the relation S that represents the participating entity type at
the N-side of the relationship type. Include as foreign key in S the primary key of the T Include any simple attributes of R as attributes of S.
Summary (Binary 1-N Relationships)
N 1S T
R
S
SIDTIDP
T
TID
P
21
ER-to-Relational Mapping Algorithm
Step 5: Mapping of Binary M:N Relationship Types.
M NS T
R
P
S
SID
R
SIDTIDP
T
TID
R(SID) references S(SID)R(TID) references T(TID)Primary key(SID, TID)
22
Example
M NEMPLOYEE PROJECT
WORKS_ON
Hours
Primary key(Essn, Pno)
23
Summary (Binary M-N Relationships)
– For each binary M:N relationship type R Create a new relation R Include as foreign key attributes in R the primary keys of the
relations that represent the participating entity types; their combination will form the primary key of R.
Include simple attributes of the relationship type R as attributes of the relation R
M NS T
R
24
ER-to-Relational Mapping Algorithm
Step 6: Mapping of Multivalued attributes.
– For each multivalued attribute A, create a new relation T. Include attribute corresponding to A Include primary key attribute K of relation S – as a foreign key in T Primary key of T is the combination of A and K
SA
K
S
KB
T
KA
B
25
Example
26
ER-to-Relational Mapping Algorithm
Step 7: Mapping of N-ary Relationship Types.
– For each n-ary relationship type R, create a new relation R Include primary keys of the relations participating in the relationship
type as foreign key attributes in R Include any simple attributes of the n-ary relationship type R as
attributes of R
S T
R
P
27
Ternary relationship types
28
Exercise