+ All Categories
Home > Documents > Translating ER Schema to Relational Model

Translating ER Schema to Relational Model

Date post: 13-Feb-2016
Category:
Upload: chin
View: 25 times
Download: 1 times
Share this document with a friend
Description:
Translating ER Schema to Relational Model. Basic Mapping So Far. Simple algorithms covers base case Idea: Each entity type = separate relation Each relationship type = separate relation. Simple Algorithm: Example 3. Part (pName, pNumber) Contains (superPart, subPart, quantity). - PowerPoint PPT Presentation
24
CS3431 1 Translating ER Schema to Relational Model
Transcript
Page 1: Translating ER Schema to Relational Model

CS3431 1

Translating ER Schema to Relational Model

Page 2: Translating ER Schema to Relational Model

cs3431 2

Basic Mapping So Far

Simple algorithms covers base case Idea:

Each entity type = separate relation Each relationship type = separate relation

Page 3: Translating ER Schema to Relational Model

cs3431 3

Simple Algorithm: Example 3

C onta ins

Part

pN am e pNum ber

subP artsuperPart

quantity

(0, 1 )(0 , *)

PRIMARY KEY (Part) = <pNumber>PRIMARY KEY (Contains) = <subPart,superPart>

FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber)FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber)

Part (pName, pNumber)Contains (superPart, subPart, quantity)

Page 4: Translating ER Schema to Relational Model

cs3431 4

Next

Let’s consider constraints Let’s reduce number of relations

Page 5: Translating ER Schema to Relational Model

cs3431 5

Refinement for Simple Mapping

Primary Key for R’ is defined as:

If the maximum cardinality of any Ei is 1, primary key for R’ = primary key for Ei

Page 6: Translating ER Schema to Relational Model

cs3431 6

Simple Algorithm: Example 3

C onta ins

Part

pN am e pNum ber

subP artsuperPart

quantity

(0, 1 )(0 , *)

PRIMARY KEY (Part) = <pNumber>PRIMARY KEY (Contains) = <subPart>FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber)FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber)

Part (pName, pNumber)Contains (superPart, subPart, quantity)

Page 7: Translating ER Schema to Relational Model

cs3431 7

Decreasing the Number of Relations

Technique 1 If the relationship type R contains an entity type,

say E, whose maximum cardinality is 1, then R may be represented as attributes of E.

Page 8: Translating ER Schema to Relational Model

cs3431 8

Example 1Student

sNumber

sName

Professor

pNumber

pName

HasAdvisor

(1,1) (0, *)

years

If the relationship type R contains an entity type, say E, whose maximum cardinality is 1, then R may be represented as attributes of E.

Page 9: Translating ER Schema to Relational Model

cs3431 9

Example 1Student

sNumber

sName

Professor

pNumber

pName

HasAdvisor

(1,1) (0, *)

years

Student (sNumber, sName, advisor, years)Professor (pNumber, pName)

PRIMARY KEY (Student) = <sNumber>PRIMARY KEY (Professor) = <pNumber>FOREIGN KEY Student (advisor) REFERENCES Professor (pNumber)

Question: Will Student.advisor attribute ever be NULL ?Answer: No !

Page 10: Translating ER Schema to Relational Model

cs3431 10

Example 2Person

pNumber

pName

Dept

dNumber

dName

WorksFor

(0,1) (0, *)

years

Person (pNumber, pName, dept, years)Dept (dNumber, dName)

PRIMARY KEY (Person) = <pNumber>PRIMARY KEY (Dept) = <dNumber>FOREIGN KEY Person (dept) REFERENCES Dept (dNumber)

What about NULL attributes ?Dept and years may be null for a person

Page 11: Translating ER Schema to Relational Model

cs3431 11

Remember the Simple Algorithm: Example 3

C onta ins

Part

pN am e pNum ber

subP artsuperPart

quantity

(0, 1 )(0 , *)

PRIMARY KEY (Part) = <pNumber>

PRIMARY KEY (Contains) = <subPart>FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber)FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber)

Part (pName, pNumber)Contains (superPart, subPart, quantity)

Page 12: Translating ER Schema to Relational Model

cs3431 12

Example 3

C onta ins

Part

pN am e pN um ber

subP artsuperPart

quantity

(0 , 1 )(0 , *)

Part (pNumber, pname, superPart, quantity)

PRIMARY KEY (Part) = <pNumber>FOREIGN KEY Part (superPart) REFERENCES Part (pNumber)

Note: superPart indicates the superpart of a part, and it may be null

Page 13: Translating ER Schema to Relational Model

cs3431 13

Decreasing the Number of Relations

Summary of Technique 1 If the relationship type R contains an entity type,

say E, whose maximum cardinality is 1, then R may be represented as attributes of E.

If cardinality of E is (1, 1), then no “new nulls” added If cardinality of E is (0, 1) then may add “new nulls”

Page 14: Translating ER Schema to Relational Model

cs3431 14

Decreasing the number of Relations

Technique 2

If relationship type R between E1 and E2 is one-to-one [1:1], and the cardinality of E1 or of E2 is (1, 1), then we can combine everything into 1 relation

Page 15: Translating ER Schema to Relational Model

cs3431 15

Decreasing the number of Relations

Technique 2 - Method Details

Let us assume the cardinality of E1 in R is (1, 1). Then we create one relation for entity E2 And, we move all attributes of E1 and for R to be

attributes of E2.

Page 16: Translating ER Schema to Relational Model

cs3431 16

Example 1

Student

sNumber

sName

Professor

pNumber

pName

HasAdvisor

(0,1) (1,1)

years

Student-BIG (sNumber, sName, pNumber, pName, years)

PRIMARY KEY (Student) = <sNumber>

CANDIDATE KEY (Student) = <pNumber>

Note: pNumber, pName, and years can be null for students with no advisor

Page 17: Translating ER Schema to Relational Model

cs3431 17

Example 2

Student

sNumber

sName

Professor

pNumber

pName

HasAdvisor

(1,1) (1,1)

years

Student (sNumber, sName, pNumber, pName, years)

PRIMARY KEY (Student) = <sNumber>CANDIDATE KEY (Student) = <pNumber>

Note: pNumber cannot be null for any student.

Page 18: Translating ER Schema to Relational Model

cs3431 18

Decreasing the Number of Relations

Technique 2

If relationship type R between E1 and E2 is one-to-one [1:1], and the cardinality of E1 or of E2 is (1, 1), then we can combine everything into 1 relation

Not always recommended! Thus use with care ! While very compact, semantically may not be clearest choice !

Page 19: Translating ER Schema to Relational Model

cs3431 19

ER Model: Complex AttributesComposite Attribute: address

sta testreet

address

city

Student

sNamesumer

sAge

statestreet

address

city

Page 20: Translating ER Schema to Relational Model

cs3431 20

Mapping details Composite attribute in ER

Include an attribute for every component of the composite attribute.

Page 21: Translating ER Schema to Relational Model

cs3431 21

ER Model: Complex Attributes

Multivalued Attribute: major

Student

sN am esN um ber

sA ge

m ajor

statestreet

address

city

Page 22: Translating ER Schema to Relational Model

cs3431 22

Mapping details Multi-valued attribute in ER

We need a separate relation for any multi-valued attribute.

Identify appropriate attributes, keys and foreign key constraints.

Page 23: Translating ER Schema to Relational Model

cs3431 24

Example: Composite and Multi-valued attributes in ER

Student

sNamesNumber

sAge

major

statestreet

address

city

Student (sNumber, sName, sAge, street, city, state)PRIMARY KEY (Student) = <sNumber>

StudentMajor (sNumber, major)PRIMARY KEY (StudentMajor) = <sNumber, major>FOREIGN KEY StudentMajor (sNumber) REFERENCES Student (sNumber)

Page 24: Translating ER Schema to Relational Model

cs3431 25

Summary

Simple algorithms covers base case

Refinements : Reduce number of relations

Refinements: Consider constraints (not NULL)

Consider other ER constructs like complex and multi-valued attributes


Recommended