19
CS3431 1 Translating ER Schema to Relational Model

CS34311 Translating ER Schema to Relational Model

Embed Size (px)

Citation preview

Page 1: CS34311 Translating ER Schema to Relational Model

CS3431 1

Translating ER Schema to Relational Model

Page 2: CS34311 Translating ER Schema to Relational Model

cs3431 2

Basic Mapping So Far

Simple algorithm covers base case Each entity type = separate relation Each relationship type = separate relation

Page 3: CS34311 Translating ER Schema to Relational Model

cs3431 4

Next

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

Page 4: CS34311 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 5: CS34311 Translating ER Schema to Relational Model

cs3431 8

Example 1

Student

sNumber

sName

Professor

pNumber

pName

HasAdvisor

(1,1) (0, *)

years

If R contains entity type E whose max. cardinality is 1, then R may be represented as attributes of E.

Page 6: CS34311 Translating ER Schema to Relational Model

cs3431 9

Example 1

Student

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 ! So add the NOT NULL constraint.

Page 7: CS34311 Translating ER Schema to Relational Model

cs3431 10

Example 2

Person

pNumber

pName

Dept

dNumber

dName

WorksFor

(0,1) (0, *)

years

Person (pNumber, pName, works-in, years)Dept (dNumber, dName)

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

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

Page 8: CS34311 Translating ER Schema to Relational Model

cs3431 11

Remember the Simple Algorithm: Example 3

C onta ins

P art

pN am e pN um ber

subP artsuperP art

quantity

(0 , 1 )(0 , *)

PRIMARY KEY (Part) = <pNumber>PRIMARY KEY (Contains) = <superPart, subPart> 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 9: CS34311 Translating ER Schema to Relational Model

cs3431 12

Example 3: The Reduced Case.

C onta ins

P art

pN am e pN um ber

subP artsuperP art

quantity

(0 , 1 )(0 , *)

Part (pNumber, pname, hasSuperPart, quantity)

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

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

Page 10: CS34311 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 add “not null”

Page 11: CS34311 Translating ER Schema to Relational Model

cs3431 14

Decreasing Number of Relations

Technique 2: Even more radical :

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 one 1 relation.

Page 12: CS34311 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 make all attributes of E1 and of R to

become attributes of E2.

Page 13: CS34311 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>ANY OTHER KEY ?CANDIDATE KEY (Student) = <pNumber>

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

Page 14: CS34311 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 15: CS34311 Translating ER Schema to Relational Model

cs3431 18

Decreasing Number of Relations

Technique 2

For one-to-one relationship R with E cardinality (1, 1), then combine everything into 1 relation.

While very compact, semantically may not be clearest choice. Therefore, not always recommended !

Page 16: CS34311 Translating ER Schema to Relational Model

cs3431 19

ER Model: Complex Attributes

Composite Attribute: address

sta testree t

address

city

Student

sNamesumer

sAge

statestreet

address

city

Composite attribute in ER Include an attribute for every component

of the composite attribute.

Page 17: CS34311 Translating ER Schema to Relational Model

cs3431 20

ER Model: Complex Attributes

Multivalued Attribute: major

S tudent

sN am esN um ber

sA ge

m ajor

sta testree t

address

city

Mapping Multi-valued attribute in ER:

•Need separate relation for multi-valued attribute.•Identify appropriate attributes, keys & foreign key constraints.

Page 18: CS34311 Translating ER Schema to Relational Model

cs3431 21

Multi-valued attribute

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 19: CS34311 Translating ER Schema to Relational Model

cs3431 22

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