400 likes | 876 Views
CSE 480: Database Systems. Lecture 6: ER to Relational Mapping. Reference: Read Chapter 9 of the textbook. The ER diagram for COMPANY database. Mapping ER diagram into relational schema. Mapping ER diagram into relational schema. Map everything as separate tables.
E N D
CSE 480: Database Systems • Lecture 6: ER to Relational Mapping • Reference: • Read Chapter 9 of the textbook
Mapping ER diagram into relational schema • Map everything as separate tables Query processing becomes expensive E.g.: Find me the names of all projects located in Michigan Need to perform many join operations
Mapping ER diagram into relational schema • Map everything into 1 table DEPARTMENT EMPLOYEE 1 1 MANAGES N 1 WorksFor Too much redundancy
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)
C D R ID B ID C D E Entity type, E Relation, R ER-to-Relational Mapping Algorithm • 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.
Example PRIMARY KEY (Ssn)
Example PRIMARY KEY (Dnumber)UNIQUE(Dname) secondary keys(can be null) PRIMARY KEY (Pnumber)UNIQUE(Pname)
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 P O W 1 N R Id Q S A
ER-to-Relational Mapping Algorithm Step 2: Mapping of Weak Entity Types P O W 1 N Id R Q A S O W Id A Oid P R S PRIMARY KEY (Oid, P)W(Oid) REFERENCES O(Id) ON DELETE CASCADE
Example Ssn EMPLOYEE 1 PRIMARY KEY(Essn, Dependent_name) DEPENDENT(Essn) REFERENCES EMPLOYEE(Ssn) ON DELETE CASCADE
ER-to-Relational Mapping Algorithm • Step 3: Mapping of Binary 1-to-1 Relationship types B A 1 1 Cross-reference approach: 3 Tables R B A 1 1 Foreign key approach: 2 Tables R A 1 1 Merged relation approach: 1 Table R
MANAGES Cross-Reference Approach StartDate DEPARTMENT EMPLOYEE 1 1 MANAGES Cross-reference approach: MANAGES(Dnumber) REFERENCES DEPARTMENT(Dnumber)MANAGES(Mgr_ssn) REFERENCES EMPLOYEE(Ssn)
DEPARTMENT StartDate EMPLOYEE 1 1 MANAGES Foreign Key Approach Total participation Foreign key approach: DEPARTMENT(Mgr_ssn) REFERENCES EMPLOYEE(Ssn) Mgr_ssn is NOTNULL
DEPARTMENT_W_MANAGER Merged Relation Approach Total participation StartDate DEPARTMENT MANAGER 1 1 MANAGES MANAGER Merged relation approach:
Summary (Binary 1-1 Relationships) T T T S S S 1 1 1 1 1 1 R R R Cross-reference: P P Foreign key: P Merged relation:
ER-to-Relational Mapping Algorithm • Step 4: Mapping of Binary 1:N Relationship Types. P T S 1 N R T_R(SID) references S(SID)
Example WORKS_FOR DEPARTMENT EMPLOYEE Supervisor 1 N 1 Subordinate N SUPERVISES
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) T S N 1 R P
ER-to-Relational Mapping Algorithm • Step 5: Mapping of Binary M:N Relationship Types. P T S M N R R(SID) references S(SID)R(TID) references T(TID)Primary key(SID, TID)
Example Hours PROJECT EMPLOYEE M N WORKS_ON Primary key(Essn, Pno)
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 T S M N R
A 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 B K S
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 T S R P