er-to-relational mapping jan. 2008acs-3902 yangjun chen1 er-to-relational mapping principles...

22
Jan. 2008 ACS-3902 Yangjun Chen 1 ER-to-Relational Mapping ER-to-Relational Mapping Principles Specialization/Generalization - Superclass/Subclass Relationship Outline: ER-to-Relational Mapping

Upload: angelica-washington

Post on 04-Jan-2016

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 1

ER-to-Relational Mapping

• ER-to-Relational Mapping Principles

• Specialization/Generalization

- Superclass/Subclass Relationship

Outline: ER-to-Relational Mapping

Page 2: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 2

ER-to-Relational Mapping

Sec. 9.1, Sec. 4.1 and 4.2.

General process

1. Create a relation for each strong entity type

2. Create a relation for each weak entity type•include primary key of owner (an FK - foreign key)•owner’s PK + partial key becomes PK

3. For each binary 1:1 relationship choose an entity and include the other’s PK in it as an FK. Include any

attributes of the relationship

4. For each binary 1:n relationship, choose the n-side entity and include an FK w.r.t the other entity. Include any attributes of the relationship

Page 3: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 3

ER-to-Relational Mapping

5. For each binary M:N relationship, create a relation for therelationship

•include PKs of both participating entities and any attributesof the relationship•PK is the concatenation of the participating entity PKs

6. For each multivalued attribute create a new relation•include the PK attributes of the entity type•PK is the PK of the entity type and the multivalued attribute

7. For each n-ary relationship, create a relation for the relationship•include PKs of all participating entities and any attributes ofthe relationship•PK may be the concatenation of the participating entity PKs

Page 4: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 4

ER-to-Relational Mapping

1. Create a relation for each strong entity type

•include all simple attributes

•choose a primary key

Suppose we have:

course 1 Noffered in

Section noterm

meeting

course noname

credit hoursdescription

section

Page 5: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 5

ER-to-Relational Mapping

We create a relation for Course - four attributes, course_no is the PK.

course 1 Noffered in

Section noterm

meeting

course noname

credit hoursdescription

section

CourseCourse_no name credit_hours description

Page 6: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 6

ER-to-Relational Mapping

2. Create a relation for each weak entity type•include primary key of owner (an FK)•Owner’s PK + partial key become the PK

Suppose we have:

course 1 Noffered in

Section noterm

meeting

course noname

credit hoursdescription

section

Page 7: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 7

ER-to-Relational Mapping

We create a relation for Section

course 1 Noffered in

Section noterm

meeting

course noname

credit hoursdescription

section

Section

Course_no Section_no Term

•PK is {course_no, section_no}.

•course_no is an FK.

•meeting is not a simple attribute, so it’s not included.

Page 8: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 8

ER-to-Relational Mapping

3. For each binary 1:1 relationship choose an entity and include the other’s PK in it as an FK.

department instructorchair1 1

dept_no dname instr_no iname

There are two choices here

•choose department, or

•choose instructor

Which is the better choice?

Page 9: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 9

ER-to-Relational Mapping

Department is the better choice since it must participate in the relationship.

department instructorchair1 1

dept_no dname instr_no iname

Department

chairdept_no dname

If we choose department then instr_no is included as, of course, an FK. Note that instr_no must have a value.

Page 10: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 10

ER-to-Relational Mapping

4. For each binary 1:n relationship, choose the n-side entity and include an FK w.r.t the other entity.

department instructoremploys1 N

dept_no dname instr_no iname

We must choose instructor

We end up with:

instructorinstr_no iname dept_no

•PK is instr_no

•dept_no is an FK

Note that Step 1 would lead to the instructor relation - we have now augmented instructor with the dept_no attribute.

Page 11: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 11

ER-to-Relational Mapping

5. For each binary M:N relationship, create a relation for the relationship

•include PKs of both participating entities and any attributes of the relationship•PK is the catenation of the participating entities’ PKs

student courseenrollm ngrade

Enroll

student_no Course_no grade

•PK is {student_no, course_no}

•student_no is a FK

•course_no is a FK

•grade is an attribute of Enroll

course_nostudent_no

Page 12: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 12

ER-to-Relational Mapping

6. For each multi-valued attribute create a new relation•include the PK attributes of the entity type•PK is the PK of the entity type and the multi-valued attribute

course 1 Noffered in

Section noterm

meeting

course noname

credit hoursdescription

section

Meeting is a multi-valued attribute

Page 13: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 13

ER-to-Relational Mapping

Create a relation for meeting

Section was created because of Step 2 - its PK is {course_no, section_no}

meeting

Meeting

course_no section_no meeting•PK is {course_no, section_no, meeting}.

•Meeting is an all-key relation.

Page 14: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 14

ER-to-Relational Mapping

7. For each n-ary relationship, create a relation for the relationship

•include PKs of all participating entities and any attributes of the relationship•PK may be the catenation of the participating entity PKs (depends on cardinalities)

semester courseoffersm nroom course_nosemester_no

instructor

instr_no

p

Page 15: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 15

ER-to-Relational Mapping

We need one relation, offers, with PK of {semester_no, course_no, instr_no}

semester courseoffersm nroom no course_nosemester_no

instructor

instr_no

p

Offers

course_no instr_no semester_no Room_no

Page 16: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 16

ER-to-Relational Mapping

student

graduate undergraduate

Return to Entity-Relationship Modeling

Consider Section 4.2 on Specialization and Generalization

•Specialization is the process of defining a set of sub-entities of some entity type. Generalization is the opposite approach/process of determining a supertype based on certain entities having common characteristics.

•e.g. employees may be paid by the hour or a salary (part vs full-time)•e.g. students may be part-time or full-time; graduate or undergraduate

•these are similar to 1:1 relationships, but they always involve entities of one (super)type•these are ‘is-a’ relationships

d

Page 17: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 17

ER-to-Relational Mapping

student

graduate undergraduate

d

The arc implies graduate and undergraduate are subtypes of student

The bubble and the d imply disjoint subtypes(o - overlap subtypes)

A student must be a graduate or undergraduate

•Participation of supertype may be mandatory or optional

•Subtypes may be disjoint or overlapping

•a predicate (on an attribute) determines the subtype: e.g. attribute Student_class

Student_class = ‘graduate’; Student_class = ‘undergraduate’

Student_class

Subtype is determined by the student_class attribute

Page 18: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 18

ER-to-Relational Mapping

Mapping to a relational database - Section 9.2.1 (Step 8)

•4 choices:

1. Create separate relations for the supertype and each of the subtypes.

2. Create relations for the subtypes only - each contains attributes from the supertype.

3. (disjoint subtypes) Create only one relation - includes all of the attributes for the supertype and all for the subtypes, and one discriminator attribute.

4. (overlapping subtypes) Create only one relation - includes all of the attributes for the supertype and all for the subtypes, and one logical discriminator attribute per subtype.

PK is always the same - determined from the supertype

Page 19: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 19

ER-to-Relational Mapping

SECRETARY ENGINEER

d

Example for super- & sub-types: choice 1

TECHNICIAN

name

lnameminitfname

Ssn bDates Address JobType

TypingSpeed

TGradeEngType

fname, minit, lname, ssn, bdate, address, JobType

EMPLOYEE

Essn, TypingSpeed

SECRETARY

Essn, TGrade

TECHNICIAN

Essn, EngType

ENGINEER

EMPLOYEE

Page 20: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 20

ER-to-Relational Mapping

CAR TRUCK

d

Example for super- & sub-types: choice 2

VehicleId Price LicensePlate

TNoOfPassengersNoOfAxles

VehicleId, LicensePlate, Price, MaxSpeed, NoOfPassenger

CAR

VehicleId, LicensePlate, Price, NoOfAxles, Tonnage

TRUCK

MaxSpeed Tonnage

Vehicle

Page 21: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 21

ER-to-Relational Mapping

SECRETARY ENGINEER

d

Example for super- & sub-types: choice 3

TECHNICIAN

name

lnameminitfname

Ssn bDates Address JobType

TypingSpeed

TGradeEngType

fname, minit, lname, ssn, bdate, address, JobType, TypingSpeed, Tgrade, EngType

EMPLOYEE

EMPLOYEE

Page 22: ER-to-Relational Mapping Jan. 2008ACS-3902 Yangjun Chen1 ER-to-Relational Mapping Principles Specialization/Generalization -Superclass/Subclass Relationship

Jan. 2008 ACS-3902 Yangjun Chen 22

ER-to-Relational Mapping

Part

Manufacture_Part Purchased_Part

o

Example for super- & sub-types: choice 4

PartNo Description

manufactureDate

Supplier

PartNo, Desription, MFlag, Drawing, ManufactureDate, BatchNo, Pflag, Supplier, ListPrice

Part

BatchNo

DrawingNoListPrice