• No results found

Chap7-Relational Database Design by ER- and EERR-to-Relational Mapping

N/A
N/A
Protected

Academic year: 2021

Share "Chap7-Relational Database Design by ER- and EERR-to-Relational Mapping"

Copied!
32
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Chapter 7

Chapter 7

Relational Database Design by

Relational Database Design by

ER- and

ER- and

EERR-to-Relation

EERR-to-Relation

al

al

Mapping

(3)

Chapter Outline

Chapter Outline

ER-to-Relational Mapping Algorithm

ER-to-Relational Mapping Algorithm

Step 1: Mapping of Regular Entity Types Step 1: Mapping of Regular Entity Types Step 2: Mapping of Weak

Step 2: Mapping of Weak Entity TypesEntity Types Step 3: Mapping of Binary 1:1

Step 3: Mapping of Binary 1:1 Relation TypesRelation Types Step 4: Mapping of Binary 1:N

Step 4: Mapping of Binary 1:N Relationship Types.Relationship Types. Step 5: Mapping of Binary M:N Relationship Types. Step 5: Mapping of Binary M:N Relationship Types. Step 6: Mapping of Multivalued attributes.

Step 6: Mapping of Multivalued attributes. Step 7: Mapping of N-ary Relationship Types. Step 7: Mapping of N-ary Relationship Types.

Mapping EER Model Constructs to Relations

Mapping EER Model Constructs to Relations

Step 8: Options for Mapping S

Step 8: Options for Mapping Specialization or Generalization.pecialization or Generalization. Step 9: Mapping of Union

(4)

ER-to-Relational Mapping

ER-to-Relational Mapping

Algorithm

Algorithm

Step 1: Mapping of Step 1: Mapping of Regular Entity Types.Regular Entity Types.

 – 

 –  For each regular (strong) entity type E in For each regular (strong) entity type E in the ER schema, create athe ER schema, create a relation R that includes all the simple attributes of E.

relation R that includes all the simple attributes of E.  – 

 –  Choose one of the key attributes of Choose one of the key attributes of E as the primary key for R. If E as the primary key for R. If thethe chosen key of E is

chosen key of E is composite, the set of simple attributes that form itcomposite, the set of simple attributes that form it will together form the primary key of R.

will together form the primary key of R.

Example:

Example: We create the relations EMPLOYEE, DEPARTMENT, andWe create the relations EMPLOYEE, DEPARTMENT, and PROJECT in the relational schema corresponding to the regular entities PROJECT in the relational schema corresponding to the regular entities in the ER diagram. SSN, DNUMBER, and PNUMBER are the primary in the ER diagram. SSN, DNUMBER, and PNUMBER are the primary keys for the relations

keys for the relations EMPLOYEE, DEPARTMENT, and PROJECT asEMPLOYEE, DEPARTMENT, and PROJECT as shown.

(5)

FIGURE 7.1

FIGURE 7.1

The ER

The ER

conceptual

conceptual

schema

schema

diagram for 

diagram for 

the

the

COMPANY

COMPANY

database.

database.

(6)

FIGURE 7.2

FIGURE 7.2

Result of 

Result of 

mapping the

mapping the

COMPANY

COMPANY

ER schema

ER schema

into a

into a

relational

relational

schema.

schema.

(7)

ER-to-Relational Mapping

ER-to-Relational Mapping

Algorithm (cont)

Algorithm (cont)

Step 2: Mapping of Weak Entity TypesStep 2: Mapping of Weak Entity Types

 – 

 –  For each weak entity type W in For each weak entity type W in the ER schema with owner entity typethe ER schema with owner entity type E, create a relation R and include all simple attributes (or simple

E, create a relation R and include all simple attributes (or simple components of composite attributes) of W as attributes of R. components of composite attributes) of W as attributes of R.  – 

 –  In addition, include as foreign key attributes of In addition, include as foreign key attributes of R the primary keyR the primary key attribute(s) of the relation(s) that correspond to the o

attribute(s) of the relation(s) that correspond to the owner entitywner entity type(s).

type(s).  – 

 –  The primary key of R is theThe primary key of R is the combination of combination of the primary key(s) of thethe primary key(s) of the owner(s) and the partial key of

owner(s) and the partial key of the weak entity type W, if any.the weak entity type W, if any.

Example:

Example: Create the relation DEPENDENT in this step to correspondCreate the relation DEPENDENT in this step to correspond to the weak entity type DEPENDENT. Include the primary key SSN of  to the weak entity type DEPENDENT. Include the primary key SSN of  the EMPLOYEE relation as a foreign key attribute of DEPENDENT the EMPLOYEE relation as a foreign key attribute of DEPENDENT (renamed to ESSN).

(renamed to ESSN).

The primary key of the DEPENDENT relation is the

The primary key of the DEPENDENT relation is the combinationcombination

{ESSN, DEPENDENT_NAME} because DEPENDENT_NAME is the {ESSN, DEPENDENT_NAME} because DEPENDENT_NAME is the  partial key of DEPENDENT.

(8)

ER-to-Relational Mapping

ER-to-Relational Mapping

Algorithm (cont)

Algorithm (cont)

Step 3: Mapping of Step 3: Mapping of Binary 1:1 Relation TypesBinary 1:1 Relation Types

For each binary 1:1 relationship type R

For each binary 1:1 relationship type R in the ER schema, identify the relationsin the ER schema, identify the relations S and T that correspond

S and T that correspond to the entity types participating in R. There are threeto the entity types participating in R. There are three  possible approaches:

 possible approaches:

(1) Foreign Key approach: Choose one of

(1) Foreign Key approach: Choose one of the relations-S, say-and include a foreign key in S the relations-S, say-and include a foreign key in S thethe  primary key of T. It is better to choose an entity type with

 primary key of T. It is better to choose an entity type with total participationtotal participation in R in the role of S.in R in the role of S. Example

Example: 1:1 relation MANAGES is mapped by choosing the participating entity type: 1:1 relation MANAGES is mapped by choosing the participating entity type DEPARTM

DEPARTMENT to serve in tENT to serve in the role of he role of S, because its S, because its participation in the participation in the MANAGEMANAGES relationshipS relationship type is total.

type is total.

(2) Merged relation option: An alternate mapping of a 1:1

(2) Merged relation option: An alternate mapping of a 1:1 relationship type is possible by mergingrelationship type is possible by merging the two entity types and the relationship

the two entity types and the relationship into a single relation. This may be appropriate wheninto a single relation. This may be appropriate when bothboth  participations are total.

 participations are total.

(3) Cross-reference or relationship relation option: The third alternative is to set up

(3) Cross-reference or relationship relation option: The third alternative is to set up a third relation R a third relation R  for the purpose of

for the purpose of cross-referenccross-referencing the primary keys of the two relations S and ing the primary keys of the two relations S and T representing theT representing the entity types.

(9)

ER-to-Relational Mapping

ER-to-Relational Mapping

Algorithm (cont)

Algorithm (cont)

Step 4: Mapping of Binary 1:N Relationship Types.Step 4: Mapping of Binary 1:N Relationship Types.

 – 

 –  For each regular binary 1:N relationship type R, identify the For each regular binary 1:N relationship type R, identify the relation Srelation S that represent the participating entity type at the N-side of the

that represent the participating entity type at the N-side of the relationship type.

relationship type.  – 

 –  Include as foreign key in S Include as foreign key in S the primary key of the relation T thatthe primary key of the relation T that represents the other entity type participating in R.

represents the other entity type participating in R.  – 

 –  Include any simple attributes of the 1:N relation type as attributes of Include any simple attributes of the 1:N relation type as attributes of S.S.

Example:

Example: 1:N relationship types WORKS_FOR, CONTROLS, and1:N relationship types WORKS_FOR, CONTROLS, and SUPERVISION in the figure. For W

SUPERVISION in the figure. For WORKS_FOR we include theORKS_FOR we include the  primary key DNUMBER of the DEPARTMENT relation as foreign  primary key DNUMBER of the DEPARTMENT relation as foreign

key in the EMPLOYEE relation and call it DNO. key in the EMPLOYEE relation and call it DNO.

(10)

ER-to-Relational Mapping

ER-to-Relational Mapping

Algorithm (cont)

Algorithm (cont)

Step 5: Mapping of Step 5: Mapping of Binary M:N Relationship Types.Binary M:N Relationship Types.

 – 

 –  For each regular binary M:N relationship type R,For each regular binary M:N relationship type R, create a new relationcreate a new relation SS to represent R.

to represent R.  – 

 –  Include as foreign key attributes in S Include as foreign key attributes in S the primary keys of the relations thatthe primary keys of the relations that represent the participating entity types;

represent the participating entity types; their combination will form thetheir combination will form the  primary key

 primary key of S.of S.  – 

 –  Also include any simple attributes of the M:N relationship type (or simpleAlso include any simple attributes of the M:N relationship type (or simple components of composite attributes) as attributes of S.

components of composite attributes) as attributes of S.

Example:

Example: The M:N relationship tyThe M:N relationship type WORKS_ON from pe WORKS_ON from the ER the ER diagramdiagram is mapped by creating a relation WORKS_ON in the relational database is mapped by creating a relation WORKS_ON in the relational database schema. The primary keys of the PROJECT and EMPLOYEE relations are schema. The primary keys of the PROJECT and EMPLOYEE relations are included as foreign keys in WORKS_ON

included as foreign keys in WORKS_ON and renamed PNO and ESSN,and renamed PNO and ESSN, respectively.

respectively.

Attribute HOURS in WORKS_ON represents the HOURS attribute of Attribute HOURS in WORKS_ON represents the HOURS attribute of thethe

relation type. The primary key of the WORKS_ON relation is the relation type. The primary key of the WORKS_ON relation is the combination of the foreign key attributes {ESSN, PNO}.

(11)

ER-to-Relational Mapping

ER-to-Relational Mapping

Algorithm (cont)

Algorithm (cont)

Step 6: Mapping of Multivalued attributes.Step 6: Mapping of Multivalued attributes.

 – 

 –  For each multivalued attribute A, create a new relation R. This relation R For each multivalued attribute A, create a new relation R. This relation R  will include an attribute corresponding to A, p

will include an attribute corresponding to A, plus the primary key attributelus the primary key attribute K-as a foreign key in

K-as a foreign key in R-of the relation that represents the entity type of R-of the relation that represents the entity type of  relationship type that has A as an

relationship type that has A as an attribute.attribute.  – 

 –  The primary key of R is the The primary key of R is the combination of A and K. If the combination of A and K. If the multivaluedmultivalued attribute is composite, we include its

attribute is composite, we include its simple components.simple components.

Example:

Example: The relation DEPT_LOCATIONS is created. The attributeThe relation DEPT_LOCATIONS is created. The attribute DLOCATION represents the multivalued attribute LOCATIONS of  DLOCATION represents the multivalued attribute LOCATIONS of 

DEPARTMENT, while DNUMBER-as foreign key-represents the primary DEPARTMENT, while DNUMBER-as foreign key-represents the primary key of the DEPARTMENT relation. The primary key of R is

key of the DEPARTMENT relation. The primary key of R is thethe combination of {DNUMBER, DLOCATION}.

(12)

ER-to-Relational Mapping

ER-to-Relational Mapping

Algorithm (cont)

Algorithm (cont)

Step 7: Mapping of

Step 7: Mapping of N-ary Relationship Types.

N-ary Relationship Types.

 – 

 – 

For each n-ary relationship type R, where n>2, create a new

For each n-ary relationship type R, where n>2, create a new

relationship S to represent R.

relationship S to represent R.

 – 

 – 

Include as foreign key attributes in

Include as foreign key attributes in S the primary keys of the

S the primary keys of the

relations that represent the participating entity types.

relations that represent the participating entity types.

 – 

 – 

Also include any simple attributes of the n-ary relationship

Also include any simple attributes of the n-ary relationship

type (or simple components of composite attributes) as

type (or simple components of composite attributes) as

attributes of S.

attributes of S.

Example:

Example: The relationship type SUPPY in the ER below. This The relationship type SUPPY in the ER below. This can becan be mapped to the relation SUPPLY shown in the

mapped to the relation SUPPLY shown in the relational schema, whoserelational schema, whose  primary key is the combination of the three foreign keys {SNAME,

 primary key is the combination of the three foreign keys {SNAME, PARTNO, PROJNAME}

(13)

FIGURE 4.11

FIGURE 4.11

Ternary relationship types. (a)

(14)

FIGURE 7.3

FIGURE 7.3

Mapping the

Mapping the

nn

-ary relationship type SUPPLY from

-ary relationship type SUPPLY from

Figure 4.11a.

(15)

Summary of Mapping constructs

Summary of Mapping constructs

and constraints

and constraints

Table 7.1 Correspondence between ER and Relational Models

Table 7.1 Correspondence between ER and Relational Models

E

ER R MMooddeell RReellaattiioonnaal l MMooddeell

E

Ennttiitty y ttyyppee ““EEnnttiittyy” ” rreellaattiioonn 1:1 o

1:1 or 1:N rr 1:N relatelationionshiship typep type ForForeigeign key (on key (or “rr “relatelationionshiship” relp” relatioation)n) M

M::N N rrelelatatiioonnsshhip ip ttypypee ““RRelelatatiioonnsshhipip” ” rreelalattiioon n aannd d twtwo o ffoorreieiggn n kkeeysys n

n--arary ry reellatatioionnsshhip ip tytyppee ““RRelelatatiioonnsshhipip” r” reelalattiioon an annd n d n ffoorreieiggn kn keeysys S

Siimmpplle e aattttrriibbuuttee AAttttrriibbuuttee C

Coommppoossiitte e aattttrriibbuuttee SSeet t oof f ssiimmpplle e ccoommppoonneennt t aattttrriibbuutteess M

Muullttiivvaalluueed d aattttrriibbuuttee RReellaattiioon n aannd d ffoorreeiiggn n kkeeyy V

Vaalluue e sseett DDoommaaiinn K

(16)

Mapping EER Model Constructs to

Mapping EER Model Constructs to

Relations

Relations

Step8: Options for Mapping Step8: Options for Mapping Specialization or Generalization.Specialization or Generalization.

Convert each specialization with m subclasses {S

Convert each specialization with m subclasses {S11, S, S22,….,S,….,Smm} and generalized} and generalized superclass C, where the attributes of C are

superclass C, where the attributes of C are {k,a{k,a11,…a,…ann} and k is the (p} and k is the (primary)rimary) key, into relational schemas using one of the four

key, into relational schemas using one of the four following options:following options:

Option 8A: Multiple relations-Superclass a

Option 8A: Multiple relations-Superclass and subclasses.nd subclasses. Create a relation L for C with attributes Attrs(L) = {k,a

Create a relation L for C with attributes Attrs(L) = {k,a11,…a,…ann} and PK(L) = k. Create a} and PK(L) = k. Create a relation L

relation Lii for each subclass Sfor each subclass Sii, 1 < i < m, with the attributesAttrs(L, 1 < i < m, with the attributesAttrs(Lii) = {k} U) = {k} U {attributes of S

{attributes of Sii} and PK(L} and PK(Lii)=k. This option works)=k. This option works for any specializationfor any specialization (total or (total or   partial, disjoint of over-lapping).

 partial, disjoint of over-lapping).

Option 8B: Multiple relations-Subclass relations only Option 8B: Multiple relations-Subclass relations only Create a relation L

Create a relation Lii for each subclass Sfor each subclass Sii, 1 < i < m, with the attributes Attr(L, 1 < i < m, with the attributes Attr(Lii) =) = {attributes of S

{attributes of Sii} U {k,a} U {k,a11…,a…,ann} and PK(L} and PK(Lii) = k. This option only ) = k. This option only works for aworks for a specialization whose subclasses are

specialization whose subclasses are totaltotal (every entity in the superclass must belong to(every entity in the superclass must belong to (at least) one of the subclasses).

(17)

FIGURE 4.4

FIGURE 4.4

EER diagram

EER diagram

notation for an

notation for an

attribute-defined

defined

specialization

specialization

on JobType.

on JobType.

(18)

FIGURE 7.4

FIGURE 7.4

Options for m

Options for m

apping specialization or generalization.

apping specialization or generalization.

(a) Mapping the EER schema in Figure 4.4 using option

(a) Mapping the EER schema in Figure 4.4 using option

8A.

(19)

FIGURE 4.3

FIGURE 4.3

Generalizati

Generalizati

on. (b)

on. (b)

Generalizing CAR and TRUCK into the

Generalizing CAR and TRUCK into the

superclass VEHICLE.

(20)

FIGURE 7.4

FIGURE 7.4

Options for m

Options for m

apping specialization or generalization.

apping specialization or generalization.

(b) Mapping the EER schema in Figure 4.3b using

(b) Mapping the EER schema in Figure 4.3b using

option 8B.

(21)

Mapping EER Model Constructs to

Mapping EER Model Constructs to

Relations (cont)

Relations (cont)

Option 8C: Single relation with one type attribute. Option 8C: Single relation with one type attribute.

Create a single relation L with attributes Attrs(L) = {k,a

Create a single relation L with attributes Attrs(L) = {k,a11,…a,…ann} U {attributes} U {attributes of S

of S11} U…U {attributes of S} U…U {attributes of Smm} U {t} and PK(L) = k. } U {t} and PK(L) = k. The attribute t is calledThe attribute t is called a type (or 

a type (or discriminatingdiscriminating) attribute that indicates the subclass to which each) attribute that indicates the subclass to which each tuple belongs

tuple belongs

Option 8D: Single relation with multiple ty

Option 8D: Single relation with multiple type attributes.pe attributes.

Create a single relation schema L with attributes Attrs(L) = {k,a

Create a single relation schema L with attributes Attrs(L) = {k,a11,…a,…ann} U} U {attributes of S

{attributes of S11} U…U {attributes of S} U…U {attributes of Smm} U {t} U {t11, t, t22,…,t,…,tmm} and PK(L) = k.} and PK(L) = k. Each t

Each tii, 1 < I < m, is a Boolean type attribute indicating whether a tuple, 1 < I < m, is a Boolean type attribute indicating whether a tuple  belongs to the subclass S

(22)

FIGURE 4.4

FIGURE 4.4

EER diagram

EER diagram

notation for an

notation for an

attribute-defined

defined

specialization

specialization

on JobType.

on JobType.

(23)

FIGURE 7.4

FIGURE 7.4

Options for m

Options for m

apping specialization or generalization.

apping specialization or generalization.

(c) Mapping the EER schema in Figure 4.4 using option

(c) Mapping the EER schema in Figure 4.4 using option

8C.

(24)

FIGURE 4.5

FIGURE 4.5

EER diagram notation for

EER diagram notation for

an overlapping (nondisjoint)

an overlapping (nondisjoint)

specialization.

(25)

FIGURE 7.4

FIGURE 7.4

Options for m

Options for m

apping specialization or generalization.

apping specialization or generalization.

(d) Mapping Figure 4.5 using

(d) Mapping Figure 4.5 using

option 8D with Boolean

option 8D with Boolean

type fields Mflag and Pflag.

(26)

Mapping EER Model Constructs to

Mapping EER Model Constructs to

Relations (cont)

Relations (cont)

Mapping of Shared Subclasses (Multiple Inheritance)Mapping of Shared Subclasses (Multiple Inheritance)

A shared subclass, such as STUDENT_ASSISTANT, is a

A shared subclass, such as STUDENT_ASSISTANT, is a subclass of subclass of 

several classes, indicating multiple inheritance. These classes must all have several classes, indicating multiple inheritance. These classes must all have the same key attribute; otherwise, the shared subclass would be modeled as a the same key attribute; otherwise, the shared subclass would be modeled as a category.

category.

We can apply any of the options discussed in Step 8 to a shared subclass, We can apply any of the options discussed in Step 8 to a shared subclass, subject to the restriction discussed in Step 8

subject to the restriction discussed in Step 8 of the mapping algorithm.of the mapping algorithm. Below both 8C and 8D are used for the shared class

Below both 8C and 8D are used for the shared class STUDENT_ASSISTANT.

(27)

FIGURE 4.7

FIGURE 4.7

A specialization

A specialization

lattice with multiple

lattice with multiple

inheritance for a

inheritance for a

UNIVERSITY

UNIVERSITY

database.

database.

(28)

FIGURE 7.5

FIGURE 7.5

Mapping the EER specialization lattice in Figure 4.6

Mapping the EER specialization lattice in Figure 4.6

using multiple options.

(29)

Mapping EER Model Constructs to

Mapping EER Model Constructs to

Relations (cont)

Relations (cont)

Step 9: Mapping of Union Types (Categories).Step 9: Mapping of Union Types (Categories).

 – 

 –  For mapping a category whose defining superclass have For mapping a category whose defining superclass have different keys, itdifferent keys, it is customary to specify a new key attribute, called a

is customary to specify a new key attribute, called a surrogate keysurrogate key,, when creating a relation to correspond to

when creating a relation to correspond to the category.the category.  – 

 –  In the example below we can create a rIn the example below we can create a relation OWNER to correspond toelation OWNER to correspond to the OWNER category and include any attributes of

the OWNER category and include any attributes of the category in thisthe category in this relation. The primary key of the OWNER relation is the

relation. The primary key of the OWNER relation is the surrogate key,surrogate key, which we called OwnerId.

(30)

FIGURE 4.8

FIGURE 4.8

Two categories (union

Two categories (union

types): OWNER and

types): OWNER and

REGISTERED_VEHICLE.

(31)

FIGURE 7.6

FIGURE 7.6

Mapping the EER

Mapping the EER

categories (union

categories (union

types) in Figure 4.7

types) in Figure 4.7

to relations.

to relations.

(32)

Mapping Exercise

Mapping Exercise

Exercise 7.4. Exercise 7.4. FIGURE 7.7 FIGURE 7.7 An ER schema for

References

Related documents

The er diagram depend on data types of relationships between three tables to other conventions, er schema to subclass are all relationships within a transaction between entities

Relational DBMS Entity-Relationship model is used in the conceptual design of various database conceptual level conceptual schema Design is independent.. We can have the schema

lyrata lyrata established a spatial pattern of predominantly outcrossing populations in the west and predominantly selfing populations in the east (Mable &amp; Adam 2007; Mable et

◼ For each weak entity type W in the ER schema with owner entity type E, create a relation R &amp; include all simple attributes (or.. simple components of composite attributes) of

In our solution, we created a weak entity type ATTENDANCE; each (weak) entity in ATTENDANCE represents a period in which a STUDENT attended a particular COLLEGE, and is identified

Choose from top universities and eer diagrams manually label entities from relational schema generation of people, which student classes, and data model, it should be two..

Entity Type Label: Database of the Mines and prospects data Entity Type Definition: Point locations for inventory of mining activity and production in the state of Idaho. Entity

§ Owner entity set and weak entity set must participate in a one-to- many relationship set (one owner, many weak entities). § Weak entity set must have total participation in