You are on page 1of 16

Translating Entity-Relationship to

Relational Tables
Grant Weddell
David R. Cheriton School of Computer Science
University of Waterloo

CS 348
Introduction to Database Management
Spring 2012

CS 348 (Intro to DB Mgmt)

ER to Relational

Fall 2011

1 / 16

E-R Diagram to Relational Schema

Main ideas:
Each entity set maps to a new table
Each attribute maps to a new table column
Each relationship set maps to either new table columns or to a

new table

CS 348 (Intro to DB Mgmt)

ER to Relational

Fall 2011

2 / 16

Representing Strong Entity Sets


Entity set E with attributes a1
attributes a1
an

an translates to table E with

Entity of type E
row in table E
primary key of table
Primary key of entity set
Example:
Student

StudentNum

Major

StudentName

Student
StudentNum
CS 348 (Intro to DB Mgmt)

StudentName
ER to Relational

Major
Fall 2011

3 / 16

Representing Weak Entity Sets

Weak entity set E translates to table E

Columns of table E should include


Attributes of the weak entity set
Attributes of the identifying relationship set
Primary key attributes of entity set for dominating entities

Primary key of weak entity set

CS 348 (Intro to DB Mgmt)

primary key of table

ER to Relational

Fall 2011

4 / 16

Representing Weak Entity Sets (cont.)


Example:
Balance

Account

AccNum

Log

Transaction

Date

TransNum

Amount

Account
AccNum

Balance

Transaction
TransNum
CS 348 (Intro to DB Mgmt)

AccNum

Date

ER to Relational

Amount
Fall 2011

5 / 16

Representing Relationship Sets

If the relationship set is an identifying relationship set for a weak

entity set then no action needed


If we can deduce the general cardinality constraint (1,1) for a

component entity set E then add following columns to table E


Attributes of the relationship set
Primary key attributes of remaining component entity sets

Otherwise: relationship set R

CS 348 (Intro to DB Mgmt)

table R

ER to Relational

Fall 2011

6 / 16

Representing Relationship Sets (cont.)

Columns of table R should include


Attributes of the relationship set
Primary key attributes of each component entity set
Primary key of table R determined as follows
If we can deduce the general cardinality constraint (0,1) for a
component entity set E , then take the primary key attributes for E
Otherwise, choose primary key attributes of each component entity

CS 348 (Intro to DB Mgmt)

ER to Relational

Fall 2011

7 / 16

Representing Relationship Sets (cont.)


Example:
Score

Address

Match

Location

HomeTeam
Team
Visitor

TeamName

Team
TeamName

LocName

Location
LocName

Address

Match
HomeTeamName

VisitorTeamName

LocName

Score

Note that the role name of a component entity set should be


prepended to its primary key attributes, if supplied.
CS 348 (Intro to DB Mgmt)

ER to Relational

Fall 2011

8 / 16

Representing Aggregation

Tabular representation of aggregation of R


tabular representation for relationship set R

To represent relationship set involving aggregation of R, treat the


aggregation like an entity set whose primary key is the primary key
of the table for R

CS 348 (Intro to DB Mgmt)

ER to Relational

Fall 2011

9 / 16

Representing Aggregation (cont.)


Example:

CourseNum

Student

EnrolledIn

Course

StudentNum
CourseAccount

Account

Student
StudentNum

Course
CourseNum

ExpirationDate

UserId

Account
UserId

EnrolledIn
StudentNum

CourseNum

CourseAccount
UserId StudentNum CourseNum ExpirationDate
CS 348 (Intro to DB Mgmt)

ER to Relational

Fall 2011

10 / 16

Representing Specialization
Create table for higher-level entity set, and treat specialized entity
subsets like weak entity sets (without discriminators)
Example:
StudentNumber

Student

Student

StudentNumber StudentName

StudentName

Graduate
StudentNumber ProfessorName
Degree

Graduate

(1, 1)

SupervisedBy

(0, N)

Professor

StudentNumber Degree
Professor

Degrees

CS 348 (Intro to DB Mgmt)

ProfessorName

ER to Relational

ProfessorName

Fall 2011

11 / 16

Representing Generalization (Approach #1)

Create a table for each lower-level entity set only

Columns of new tables should include


Attributes of lower level entity set
Attributes of the superset

The higher-level entity set can be dened as a view on the tables for
the lower-level entity sets

CS 348 (Intro to DB Mgmt)

ER to Relational

Fall 2011

12 / 16

Representing Generalization (Approach #1)


Example:

MakeAndModel

LicenceNum

Vehicle

Price

COVERS

Tonnage

Truck

Car

AxelCount

MaxSpeed

PassengerCount

Truck
LicenceNum MakeAndModel

Price

Tonnage

Price

MaxSpeed

AxelCount

Car
LicenceNum MakeAndModel
CS 348 (Intro to DB Mgmt)

ER to Relational

PassengerCount
Fall 2011

13 / 16

Representing Generalization (Approach #2)

Treat generalization the same as specialization.


Example:
MakeAndModel

Vehicle
LicenceNum

Vehicle

Price

LicenceNum

MakeAndModel

Price

COVERS

Truck
LicenceNum
Tonnage

Truck

Car

MaxSpeed

CS 348 (Intro to DB Mgmt)

AxelCount

Car
LicenceNum

AxelCount

Tonnage

MaxSpeed

PassengerCount

PassengerCount

ER to Relational

Fall 2011

14 / 16

Example Translation: ER Diagram


Course

CourseNum

CourseName

(0, N)
SectionOf
Term

SectionNum

(1, 1)
Section

(1, 1)

(6, 50)
EnrolledIn

TaughtBy

Professor

OffSite
Section

ProfName
ProfNum

CS 348 (Intro to DB Mgmt)

Student

Location

Mark

GPA

StudentName

StudentNum

ER to Relational

Fall 2011

15 / 16

Example Translation: Relational Diagram


Course

Student

CourseNum

StudentNum

CourseName

StudentName
GPA

Section

EnrolledIn
CourseNum
SectionNum
StudentNum
Term

CourseNum
SectionNum
Term
ProfNum

Mark
OffSite Section

Professor

CourseNum
SectionNum
Term

ProfNum
ProfName

Location
CS 348 (Intro to DB Mgmt)

ER to Relational

Fall 2011

16 / 16

You might also like