Download presentation
Presentation is loading. Please wait.
1
CS4222 Principles of Database System
6/16/2019 CS4222 Principles of Database System 9. ER/EER –to-Relational Mapping 2 Huiping Guo Department of Computer Science California State University, Los Angeles
2
Outline ER/EER -to-Relational Mapping
6/16/2019 Outline ER/EER -to-Relational Mapping Step 5: Map Relationship Sets with Participation Constraints Step 6: Mapping of Weak Entity Types Step 7: Mapping of Multivalued attributes Step 8: Mapping Specialization or Generalization Step 9: Mapping of Shared Subclasses (Multiple Inheritance) Step 10: Mapping of Union Types (Categories) 9. Mapping CS4222_Su17
3
Step 5: Map Relationship Sets with Participation Constraints
6/16/2019 Step 5: Map Relationship Sets with Participation Constraints since name dname ssn sex dno budget 1 Manage Department Employees Each department has exactly one manager 9. Mapping CS4222_Su17
4
Step 5: Map Relationship Sets with Participation Constraints
Combine Manage and Department to one Relation CREATE TABLE Mg_Dept( ssn CHAR(11) NOT NULL, since DATE, dno INTEGER, dname Char(20), budget REAL, PRIMARY KEY (dno), FOREIGN KEY (ssn) REFERENCES Employees (ssn) ON DELETE RESTRICT ) 9. Mapping CS4222_Su17
5
Step 6 Mapping of Weak Entity Types
6/16/2019 Step 6 Mapping of Weak Entity Types A weak entity can be identified uniquely only by considering the primary key of another (owner) entity. Owner entity set and weak entity set must participate in a one-to-many relationship set (1 owner, many weak entities). Weak entity set must have total participation in this identifying relationship set. 9. Mapping CS4222_Su17
6
Step 6 Mapping of Weak Entity Types
6/16/2019 Step 6 Mapping of Weak Entity Types For each weak entity type W in the ER schema with owner entity type E, create a relation R & include all simple attributes (or simple components of composite attributes) of W as attributes of R. Also, include as foreign key attributes of R the primary key attribute(s) of the relation(s) that correspond to the owner entity type(s). The 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, if any. 9. Mapping CS4222_Su17
7
Step 6 Mapping of Weak Entity Types
Weak entity set and the identifying relationship set are translated into a single table. sex name birthdate dname Dependents Employees ssn Dependents_of 1 N 9. Mapping CS4222_Su17
8
Step 6 Mapping of Weak Entity Types
When the owner entity is deleted, all owned weak entities must also be deleted. CREATE TABLE Dependents_Of ( ssn CHAR(11) NOT NULL dname CHAR(20), birthdate Date, PRIMARY KEY (dname, ssn), FOREIGN KEY (ssn) REFERENCES Employees(ssn) ON DELETE CASCADE ) 9. Mapping CS4222_Su17
9
Step 7: Mapping of Multivalued attributes
6/16/2019 Step 7: Mapping of Multivalued attributes For each multivalued attribute A, create a new relation R. This relation R will include an attribute corresponding to A, plus the primary key attribute K-as a foreign key in R-of the relation that represents the entity type of relationship type that has A as an attribute. The primary key of R is the combination of A and K. If the multivalued attribute is composite, we include its simple components. 9. Mapping CS4222_Su17
10
Step 7: Mapping of Multivalued attributes
Example: The relation DEPT_LOCATIONS is created. The attribute DLOCATION represents the multivalued attribute LOCATIONS of DEPARTMENT, while DNUMBER-as foreign key-represents the primary key of the DEPARTMENT relation. The primary key of R is the combination of {DNUMBER, DLOCATION}. 9. Mapping CS4222_Su17
11
Step 8: Mapping Specialization or Generalization
6/16/2019 Step 8: Mapping Specialization or Generalization Convert each specialization with m subclasses {S1, S2,….,Sm} and generalized superclass C, where the attributes of C are {k,a1,…an} and k is the (primary) key, into relational schemas using one of the four following options: Option 8A: Multiple relations-Superclass and subclasses Option 8B: Multiple relations-Subclass relations only Option 8C: Single relation with one type attribute Option 8D: Single relation with multiple type attributes 9. Mapping CS4222_Su17
12
Step 8: Mapping Specialization or Generalization
6/16/2019 Step 8: Mapping Specialization or Generalization Option 8A: Multiple relations-Superclass and subclasses Create a relation L for C with attributes Attrs(L) = {k,a1,…an} and PK(L) = k. Create a relation Li for each subclass Si, 1 < i < m, with the attributesAttrs(Li) = {k} U {attributes of Si} and PK(Li)=k. This option works for any specialization (total or partial, disjoint of over-lapping) 9. Mapping CS4222_Su17
13
6/16/2019 FIGURE 4.4 EER diagram notation for an attribute-defined specialization on JobType. 9. Mapping CS4222_Su17
14
6/16/2019 FIGURE 7.4 Options for mapping specialization or generalization. (a) Mapping the EER schema in Figure 4.4 using option 8A. 9. Mapping CS4222_Su17
15
Step 8: Mapping Specialization or Generalization
Option 8B: Multiple relations-Subclass relations only Create a relation Li for each subclass Si, 1 < i < m, with the attributes Attr(Li) = {attributes of Si} U {k,a1…,an} and PK(Li) = k. This option only works for a specialization whose subclasses are total (every entity in the superclass must belong to (at least) one of the subclasses). 9. Mapping CS4222_Su17
16
6/16/2019 FIGURE 4.3 Generalization. (b) Generalizing CAR and TRUCK into the superclass VEHICLE. 9. Mapping CS4222_Su17
17
6/16/2019 FIGURE 7.4 Options for mapping specialization or generalization. (b) Mapping the EER schema in Figure 4.3b using option 8B. Tonnage 9. Mapping CS4222_Su17
18
Step 8: Mapping Specialization or Generalization
Option 8C: Single relation with one type attribute Create a single relation L with attributes Attrs(L) = {k,a1,…an} U {attributes of S1} U…U {attributes of Sm} U {t} and PK(L) = k. The attribute t is called a type (or discriminating) attribute that indicates the subclass to which each tuple belongs Works for disjoint specialization 9. Mapping CS4222_Su17
19
6/16/2019 FIGURE 4.4 EER diagram notation for an attribute-defined specialization on JobType. 9. Mapping CS4222_Su17
20
6/16/2019 FIGURE 7.4 Options for mapping specialization or generalization. (c) Mapping the EER schema in Figure 4.4 using option 8C. Engtype 9. Mapping CS4222_Su17
21
Step 8: Mapping Specialization or Generalization
Option 8D: Single relation with multiple type attributes Create a single relation schema L with attributes Attrs(L) = {k,a1,…an} U {attributes of S1} U…U {attributes of Sm} U {t1, t2,…,tm} and PK(L) = k. Each ti, 1 < I < m, is a Boolean type attribute indicating whether a tuple belongs to the subclass Si. Works for overlapping subclasses 9. Mapping CS4222_Su17
22
6/16/2019 FIGURE 4.5 EER diagram notation for an overlapping (non-disjoint) specialization. 9. Mapping CS4222_Su17
23
6/16/2019 FIGURE 7.4 Options for mapping specialization or generalization. (d) Mapping Figure 4.5 using option 8D with Boolean type fields Mflag and Pflag. 9. Mapping CS4222_Su17
24
Step 9: Mapping of Shared Subclasses (Multiple Inheritance)
6/16/2019 Step 9: Mapping of Shared Subclasses (Multiple Inheritance) A shared subclass, such as STUDENT_ASSISTANT, is a subclass of several classes, indicating multiple inheritance. These classes must all have the same key attribute We can apply any of the options discussed in Step 8 to a shared subclass, subject to the restriction discussed in Step 8 of the mapping algorithm. Example both 8C and 8D are used for the shared class STUDENT_ASSISTANT. 9. Mapping CS4222_Su17
25
6/16/2019 FIGURE 4.7 A specialization lattice with multiple inheritance for a UNIVERSITY database. 9. Mapping CS4222_Su17
26
6/16/2019 FIGURE 7.5 Mapping the EER specialization lattice in Figure 4.6 using multiple options. course 9. Mapping CS4222_Su17
27
Step 10: Mapping of Union Types (Categories)
For mapping a category whose defining superclass have different keys, it is customary to specify a new key attribute, called a surrogate key, when creating a relation to correspond to the category. In the example below we can create a relation OWNER to correspond to the OWNER category and include any attributes of the category in this relation. The primary key of the OWNER relation is the surrogate key, which we called OwnerId. 9. Mapping CS4222_Su17
28
FIGURE 4.8 Two categories (union types): OWNER and REGISTERED_VEHICLE.
9. Mapping CS4222_Su17
29
FIGURE 9. 7 Mapping the EER categories (union types) in Figure 4
FIGURE 9.7 Mapping the EER categories (union types) in Figure 4.8 to relations. 9. Mapping CS4222_Su17
30
FIGURE 7.1 The ER conceptual schema diagram for the COMPANY database.
6/16/2019 FIGURE 7.1 The ER conceptual schema diagram for the COMPANY database. 9. Mapping CS4222_Su17
31
6/16/2019 FIGURE 7.2 Result of mapping the COMPANY ER schema into a relational schema. 9. Mapping CS4222_Su17
32
Exercise 1 Employees Reports_To supervisor subordinate 1 sex name ssn
9. Mapping CS4222_Su17
33
Exercise 1 Only ONE table is needed! CREATE TABLE Employees(
ssn CHAR(11), name CHAR(20), sex CHAR, ssn_super CHAR(11) NOT NULL, PRIMARY KEY (ssn) FOREIGN KEY (ssn_super) REFERENCES Employees(ssn) ON DELETE restricted ON UPDATE CASCADE) 9. Mapping CS4222_Su17
34
Exercise 2 1 1 name pname age ssn lot Dependents Employees Purchaser
6/16/2019 Exercise 2 name Employees ssn lot pname age Dependents 1 Purchaser Beneficiary 1 policyid cost Policies 9. Mapping CS4222_Su17 7
35
Exercise 2 policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL,
6/16/2019 Exercise 2 The cardinality constraints allow us to combine Purchaser with Policies and Beneficiary with Dependents. Participation constraints lead to NOT NULL constraints. CREATE TABLE Policies ( policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (ssn) REFERENCES Employees (ssn), ON DELETE CASCADE ) 9. Mapping CS4222_Su17 8
36
Exercise 2 pname CHAR(20), age INTEGER, policyid INTEGER,
6/16/2019 Exercise 2 CREATE TABLE Dependents ( pname CHAR(20), age INTEGER, policyid INTEGER, PRIMARY KEY (pname, policyid). FOREIGN KEY (policyid) REFERENCES Policies(policyid), ON DELETE CASCADE ) 9. Mapping CS4222_Su17 8
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.