Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C ONVERTING ERD S TO R ELATIONAL T ABLES Joe Meehean 1."— Presentation transcript:

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

2 T HE P LAYERS 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 2

3 E NTITY T YPE R ULE each entity becomes a table primary key of entity is primary key of table attributes become columns 3 Student Student ID Last name First name Student IDLast NameFirst Name

4 1-M R ELATIONSHIP R ULE 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 4

5 Teaches 1-M R ELATIONSHIP R ULE 5 Faculty Faculty ID Last name First name Offering Section # Room Time Section #RoomTime Offering

6 Enrolls M-N R ELATIONSHIP R ULE M-N relationship becomes it own table primary key is combined key formed from primary keys of participating entities 6 Offering Section # Room Time Student Student ID Last name First name Enrolls Student IDSection #

7 I DENTIFICATION D EPENDENCY R ULE 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 7

8 I DENTIFICATION D EPENDENCY R ULE 8 Has Course Number Name Credits Offering Section # Room Time NumberSection #RoomTimeFaculty ID Offering

9 Q UESTIONS ? 9

10 Has- Empl Has In 10 Parts Part# Description Quantity Distributor Customer# Name Address Motorcycle ID# Quantity Style Has- JobTitle Order Order# Date $Total Supplier Supplier# Name Address Employee Employee# Name Years Has Years JobTitle Position# Name BaseSalary Supplies In

11 O PTIONAL 1-M R ELATIONSHIPS R ULE 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 11

12 O PTIONAL 1-M R ELATIONSHIPS R ULE 12 Teaches 12 Faculty Faculty ID Last name First name Offering Section # Room Time Section #RoomTimeFaculty ID A4511NULL B6812Blem C581Daniels D352Blem Offering

13 O PTIONAL 1-M R ELATIONSHIPS R ULE 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 13

14 O PTIONAL 1-M R ELATIONSHIPS R ULE 14 Teaches 14 Faculty Faculty ID Last name First name Offering Section # Room Time Faculty ID BBlem CDaniels DBlem Teaches Section #RoomTime A4511 B6812 C581 D352 Offering

15 O PTIONAL 1-M R ELATIONSHIPS R ULE 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 3 rd option replace optional relationship with required relationship and default value 15

16 O PTIONAL 1-M R ELATIONSHIPS R ULE 16 Teaches 16 Faculty Faculty ID Last name First name Offering Section # Room Time Section #RoomTimeFaculty ID A4511Faculty B6812Blem C581Daniels D352Blem Offering

17 G ENERALIZATION H IERARCHY R ULE 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 17

18 G ENERALIZATION H IERARCHY R ULE 18 Student Major Grad Date Faculty Department Office College People College ID Last name First name College ID Last Name First Name College ID Depart- ment Office College People Faculty

19 1-1 R ELATIONSHIP R ULE 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 19

20 1-1 R ELATIONSHIP R ULE 20 Faculty Faculty ID Last name First name Department Dept. Name Funding Chairs 20 Faculty ID Last Name First Name Dept Name Fund -ing FacultyDepartment

21 1-1 R ELATIONSHIP R ULE 21 Faculty Faculty ID Last name First name Department Dept. Name Funding Chairs 21 Faculty ID Last Name First Name Dept Name Fund- ing Faculty Department

22 S ELF R EFERENCING E NTITIES 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 22

23 S ELF R EFERENCING E NTITIES 23 Employee Employee ID Last name First name Manages Employee ID Last NameFirst NameSupervisor Employee

24 Prerequisite S ELF R EFERENCING E NTITIES 24 Course Number Name Credits NumberNameCredits Course Prerequisites

25 C ONVERTING ERD R EVIEW 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 25

26 Q UESTIONS ? 26

27 27 Faculty Faculty ID Last name First name Student Student ID Last name First name Major Advisor Convert to tables

28 28 Convert to tables Hourly Hours Rate Salary Contract Expires Employee Employee ID Last name First name

29 29 Convert to tables In Building Building ID Name Address Room Room # Capacity

30 30 Convert to tables Married Faculty Faculty ID First Name Last Name


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

Similar presentations


Ads by Google