c onverting erd s to r elational t ables joe meehean 1

30
CONVERTING ERDS TO RELATIONAL TABLES Joe Meehean 1

Upload: everett-boone

Post on 17-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

1

CONVERTING ERDS TO RELATIONAL TABLES

Joe Meehean

Page 2: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

2

THE PLAYERS

ERDs easy to reason about express lots of information in limited space easy to create from business narrative

Relational Tables easy for DBMSs to store data in tables use SQL to ask lots of different questions about

data Need to convert ERDs to Relational tables

using a set of rules and some intuition

Page 3: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

3

ENTITY TYPE RULE

each entity becomes a table primary key of entity is primary key of table attributes become columns

Student

Student IDLast nameFirst name

Student ID

Last Name

First Name

Page 4: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

4

1-M RELATIONSHIP RULE

primary key of parent becomes a foreign key in table of child entity child entity is entity near Crow’s Foot symbol

if minimum cardinality on parent side is 1 (required), foreign key cannot accept null value

Page 5: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

Teaches

1-M RELATIONSHIP RULE

5

Faculty

Faculty IDLast nameFirst name

Offering

Section #RoomTime

Section # Room Time <Faculty ID>

Offering

Page 6: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

6

Enrolls

M-N RELATIONSHIP RULE

M-N relationship becomes it own table primary key is combined key formed from

primary keys of participating entities

Offering

Section #RoomTime

Student

Student IDLast nameFirst name

Enrolls

Student ID Section #

Page 7: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

7

IDENTIFICATION DEPENDENCY RULE

add a component to the primary key of the weak entity

primary key = primary key of weak entity (if any) + primary keys from independent entities

Page 8: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

8

IDENTIFICATION DEPENDENCY RULE

HasCourse

Number NameCredits

Offering

Section #RoomTime

Number Section #

Room Time Faculty ID

Offering

Page 9: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

QUESTIONS?

9

Page 10: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

10

Has-Empl

Has

In

In

Parts

Part#DescriptionQuantity

Distributor

Customer#NameAddress

Motorcycle

ID#QuantityStyle

Has-JobTitle

Order

Order#Date$Total

Supplier

Supplier#NameAddress

Employee

Employee#NameYears

Has

Years

JobTitle

Position#NameBaseSalary

Su

pp

lies

In

QUIZ BREAK!!!

Page 11: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

11

OPTIONAL 1-M RELATIONSHIPS RULE

Optional relationship: minimal cardinality of 0 on parent side (1-side)

Convert using the 1-M rule foreign key in child table (M-side) foreign key can be NULL can be problem for queries

Page 12: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

12

OPTIONAL 1-M RELATIONSHIPS RULE

Teaches

12

Faculty

Faculty IDLast nameFirst name

Offering

Section #RoomTime

Section # Room Time Faculty ID

A 45 11 NULL

B 68 12 Blem

C 58 1 Daniels

D 35 2 Blem

Offering

Page 13: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

13

OPTIONAL 1-M RELATIONSHIPS RULE

Optionally, can use Optional 1-M Relationship Rule relationship becomes its own table primary keys in both entities become foreign

keys primary key from child entity (M-side) becomes

primary key in new table

Page 14: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

14

OPTIONAL 1-M RELATIONSHIPS RULE

Teaches

14

Faculty

Faculty IDLast nameFirst name

Offering

Section #RoomTime

<Section #>

Faculty ID

B Blem

C Daniels

D Blem

TeachesSection # Room Time

A 45 11

B 68 12

C 58 1

D 35 2

Offering

Page 15: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

15

OPTIONAL 1-M RELATIONSHIPS RULE

When to used Optional 1-M Relationship Rule its optional

Optional rule makes more tables more complex more SQL operations (slower)

1-M rule makes NULL foreign keys can be difficult to deal with

3rd option replace optional relationship with required

relationship and default value

Page 16: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

16

OPTIONAL 1-M RELATIONSHIPS RULE

Teaches

16

Faculty

Faculty IDLast nameFirst name

Offering

Section #RoomTime

Section # Room Time Faculty ID

A 45 11 Faculty

B 68 12 Blem

C 58 1 Daniels

D 35 2 Blem

Offering

Page 17: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

17

GENERALIZATION HIERARCHY RULE

Each entity in a generalization hierarchy becomes a table

Includes only attributes in entity not its ancestors

Except it includes ancestors primary key uses it as its own primary key also a foreign key

Perform cascading deletes if ancestor is deleted so is subtype table entry

Page 18: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

18

GENERALIZATION HIERARCHY RULE

Student

MajorGrad Date

Faculty

DepartmentOffice

College People

College IDLast nameFirst name

College ID

Last Name

First Name

College ID

Depart-ment

Office

College People Faculty

Page 19: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

19

1-1 RELATIONSHIP RULE

Put a foreign key in each table in the relationship

Unless one table will have many NULL foreign keys

Then drop the foreign key in the table where it will be mostly NULL

Page 20: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

20

1-1 RELATIONSHIP RULE

Faculty

Faculty IDLast nameFirst name

Department

Dept. NameFunding

Chairs

20

Faculty ID

Last Name

First Name

<Dept Name>

Dept Name

Fund-ing

<Faculty ID>

Faculty Department

Page 21: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

21

1-1 RELATIONSHIP RULE

Faculty

Faculty IDLast nameFirst name

Department

Dept. NameFunding

Chairs

21

Faculty ID

Last Name

First Name

Dept Name

Fund-ing

<Faculty ID>

Faculty Department

Page 22: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

22

SELF REFERENCING ENTITIES

Apply same rules 1-M rule

add a new column with primary key as foreign key

M-N rule add a new table representing the relationship

Page 23: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

23

SELF REFERENCING ENTITIES

Employee

Employee IDLast nameFirst name

Manages

Employee ID

Last Name

First Name

Supervisor<FK Employee>

Employee

Page 24: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

24

Prerequisite

SELF REFERENCING ENTITIES

Course

Number NameCredits

Number

Name

Credits

Course

<Number> <Number>

Prerequisites

Page 25: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

25

CONVERTING ERD REVIEW

Every entity becomes a table Some relationships become tables Majority of conversion rules dictate where

foreign key goes foreign key links row in table to primary key in

another table M-N: new table with foreign keys from both

entities 1-M: foreign key in M entity optional 1-M: more complex

Page 26: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

QUESTIONS?

26

Page 27: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

27

QUIZ BREAK!!!

Faculty

Faculty IDLast nameFirst name

Student

Student IDLast nameFirst name

Major Advisor

Convert to tables

Page 28: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

28

QUIZ BREAK!!! Convert to tables

Hourly

HoursRate

Salary

SalaryContract Expires

Employee

Employee IDLast nameFirst name

Page 29: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

29

QUIZ BREAK!!! Convert to tables

InBuilding

Building IDNameAddress

Room

Room #Capacity

Page 30: C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1

30

QUIZ BREAK!!! Convert to tables

Married

Faculty

Faculty IDFirst NameLast Name