21
HNDComputing – DeMontfort University Ó DeMontfort University 2011 Handling Many to Many relationships wk5 D a t a b a s e D e s i g n C o n c e p t Database Design Concepts IMAT1408 Lecture 5 Handling Many to Many relationships

Database Design Concepts IMAT1408

  • Upload
    tuvya

  • View
    26

  • Download
    0

Embed Size (px)

DESCRIPTION

Database Design Concepts IMAT1408. Lecture 5 Handling Many to Many relationships. Aims: To demonstrate how to decompose many:many (M:M) relationships To explain why M:M relationships need to be decomposed. Handling Many:Many Relationships. - PowerPoint PPT Presentation

Citation preview

Page 1: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

Database Design ConceptsIMAT1408

Lecture 5

Handling Many to Many relationships

Page 2: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

2

Handling Many:Many RelationshipsAims:

–To demonstrate how to decompose many:many (M:M) relationships–To explain why M:M relationships need to be decomposed.

Page 3: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

3

M:M Relationships• A M:M relationship between 2 entity types must be decomposed into two 1:M relationships.

Page 4: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

4

Student Modulechooses

M M

Becomes

ModuleStudent ModuleChoice

MM1 1

makes isfor

Page 5: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

5

The Decomposition Rule

A Br

M M

Becomes

A B

MM1 1

Page 6: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

6

Or -

A Br

M M

Becomes

A B

MM1 1

Page 7: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

7

Naming• Naming the new entity type and the new relationships is sometimes not easy

• Often the old relationship name will help you name the new entity. In our example “chooses” became “choice”

• If all else fails, concatenate (join) the names of the 2 original entity types (e.g. Student Module).

Page 8: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

8

Exercise• Decompose this M:M relationship to form two 1:M relationships:

• Assign the new entity and relationship types suitable names.

Doctor Patientexamines

MM

Page 9: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

9

Solution

Doctor Patientexamines Patient at

M1Appointment attends

M 1

Page 10: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

10

Identifiers• We have seen that an entity must have an identifier• The new entity type created by decomposition needs an identifier• Start with a composite of the identifiers of the 2 original entity types

– Need to consider carefully whether this will uniquely identify every occurrence of the new entity type.

Page 11: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

11

Identifiers cont.

• For the second example:Doctor (doctor#, . . . . )Patient (patient#, . . . )

So try Examination (doctor#, patient#, …..)Notice that doctor# and patient# are foreign keys (marked in bold type) as well as forming the new primary key

• Is this a suitable identifier?.

Page 12: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

12

Identifiers cont.

• To decide if an identifier is suitable:

• Think of some other attributes for the entity

• Is one pair of doctor#, patient# values associated with just one value of each of these attributes?

Page 13: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

13

• Could a patient see the same doctor more than once?

• Examination (doctor#, patient#, ……………………..)

Page 14: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

14

• Could a patient see the doctor more than once in a day?

• Examination (doctor#, patient#,

………………………..)

Page 15: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

15

• This is getting a little complicated - maybe we should add a new key field: examination number

• Examination (examination#, doctor#, patient#, date, time, ..)

• What does the underlining and bold type mean?

Page 16: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

16

Why Decompose?

Student (student#, name, . . .)Module (module#, description, . . .)How do we know which students are taking which modules?We don’t.

Student Modulechooses

M M

Back to the first exampleLook at the original M:M relationship:

Page 17: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

17

Why Decompose? cont.Decomposing gives us a new table:

Student Module (student#, module#, ...................)Is this a suitable identifier ?Now we can list which student haschosen which module.

Page 18: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

18

Exercise

Actor (actor#, name, . . .)Film (film#, title, . . .)

• Decompose this M:M relationship• Assign the new entity type an appropriate name and think

of some additional attributes for it• Assign the new entity type a suitable identifier.

Actor Filmappearsin

M M

Page 19: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

19

Solution

Actor FilmMember of

M1Cast List Actors in

M 1

Page 20: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

20

Summary• We have looked at decomposition of M:M relationships• Discussed how to identify a unique identifier.

Page 21: Database Design Concepts IMAT1408

HNDComputing – DeMontfort University Ó DeMontfort University 2011

Handling Many to Many relationships wk5Database Design Concepts

21

ReferenceData Analysis for Database Designby D R Howe