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

Post on 17-Dec-2015

219 views 0 download

Tags:

transcript

1

CONVERTING ERDS TO RELATIONAL TABLES

Joe Meehean

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

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

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

Teaches

1-M RELATIONSHIP RULE

5

Faculty

Faculty IDLast nameFirst name

Offering

Section #RoomTime

Section # Room Time <Faculty ID>

Offering

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 #

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

8

IDENTIFICATION DEPENDENCY RULE

HasCourse

Number NameCredits

Offering

Section #RoomTime

Number Section #

Room Time Faculty ID

Offering

QUESTIONS?

9

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!!!

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

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

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

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

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

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

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

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

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

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

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

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

23

SELF REFERENCING ENTITIES

Employee

Employee IDLast nameFirst name

Manages

Employee ID

Last Name

First Name

Supervisor<FK Employee>

Employee

24

Prerequisite

SELF REFERENCING ENTITIES

Course

Number NameCredits

Number

Name

Credits

Course

<Number> <Number>

Prerequisites

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

QUESTIONS?

26

27

QUIZ BREAK!!!

Faculty

Faculty IDLast nameFirst name

Student

Student IDLast nameFirst name

Major Advisor

Convert to tables

28

QUIZ BREAK!!! Convert to tables

Hourly

HoursRate

Salary

SalaryContract Expires

Employee

Employee IDLast nameFirst name

29

QUIZ BREAK!!! Convert to tables

InBuilding

Building IDNameAddress

Room

Room #Capacity

30

QUIZ BREAK!!! Convert to tables

Married

Faculty

Faculty IDFirst NameLast Name