30
CONVERTING ERDS TO RELATIONAL TABLES Joe Meehean 1

Converting ERDs to Relational Tables

  • Upload
    becca

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

Converting ERDs to Relational Tables. Joe Meehean. 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Converting ERDs to Relational Tables

1

CONVERTING ERDS TO RELATIONAL TABLES

Joe Meehean

Page 2: Converting ERDs to Relational Tables

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: Converting ERDs to Relational Tables

3

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

StudentStudent IDLast nameFirst name

Student ID

Last Name

First Name

Page 4: Converting ERDs to Relational Tables

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: Converting ERDs to Relational Tables

Teaches

1-M RELATIONSHIP RULE

5

FacultyFaculty IDLast nameFirst name

OfferingSection #RoomTime

Section # Room Time <Faculty ID>

Offering

Page 6: Converting ERDs to Relational Tables

6

Enrolls

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

primary keys of participating entitiesOffering

Section #RoomTime

StudentStudent IDLast nameFirst name

Enrolls

<Student ID> <Section #>

Page 7: Converting ERDs to Relational Tables

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: Converting ERDs to Relational Tables

8

IDENTIFICATION DEPENDENCY RULE

HasCourse

Number NameCredits

OfferingSection #RoomTime

<Number>

Section # Room Time

Offering

Page 9: Converting ERDs to Relational Tables

QUESTIONS?

9

Page 10: Converting ERDs to Relational Tables

10

Has-Empl

HasIn

In

PartsPart#DescriptionQuantity

DistributorCustomer#NameAddress

Motorcycle

ID#FlamesStyle

Has-JobTitle

OrderOrder#Date$Total

SupplierSupplier#NameAddress

EmployeeEmployee#NameYears

HasYears

JobTitlePosition#NameBaseSalary

Supp

lies

In

QUIZ BREAK!!!Order Quantity

Page 11: Converting ERDs to Relational Tables

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: Converting ERDs to Relational Tables

12

OPTIONAL 1-M RELATIONSHIPS RULE

Teaches12

FacultyFaculty IDLast nameFirst name

OfferingSection #RoomTime

Section # Room Time <Faculty ID>S1 45 11 NULLS2 68 12 F2S3 58 1 F5S4 35 2 F2

Offering

Page 13: Converting ERDs to Relational Tables

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: Converting ERDs to Relational Tables

14

OPTIONAL 1-M RELATIONSHIPS RULE

Teaches14

FacultyFaculty IDLast nameFirst name

OfferingSection #RoomTime

<Section #>

<Faculty ID>

S2 F2S3 F5S4 F5

TeachesSection # Room TimeS1 45 11S2 68 12S3 58 1S4 35 2

Offering

Page 15: Converting ERDs to Relational Tables

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: Converting ERDs to Relational Tables

16

OPTIONAL 1-M RELATIONSHIPS RULE

Teaches16

FacultyFaculty IDLast nameFirst name

OfferingSection #RoomTime

Section # Room Time <Faculty ID>

S1 45 11 F0*S2 68 12 F2S3 58 1 F5S4 35 2 F2

Offering

* Assume F0 is PK for “Default Faculty”

Page 17: Converting ERDs to Relational Tables

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: Converting ERDs to Relational Tables

18

GENERALIZATION HIERARCHY RULE

StudentMajorGrad Date

FacultyDepartmentOffice

College PeopleCollege IDLast nameFirst name

College ID

Last Name

First Name

<College ID>

Depart-ment

OfficeCollege People Faculty

Page 19: Converting ERDs to Relational Tables

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: Converting ERDs to Relational Tables

20

1-1 RELATIONSHIP RULEFaculty

Faculty IDLast nameFirst name

DepartmentDept. IDFunding

Chairs20

Faculty ID

Last Name

First Name

<Dept ID>

Dept ID Fund-ing

<Faculty ID>

Faculty Department

Page 21: Converting ERDs to Relational Tables

21

1-1 RELATIONSHIP RULEFaculty

Faculty IDLast nameFirst name

DepartmentDept. IDFunding

Chairs21

Faculty ID

Last Name

First Name

Dept ID Fund-ing

<Faculty ID>

Faculty Department

Page 22: Converting ERDs to Relational Tables

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: Converting ERDs to Relational Tables

23

SELF REFERENCING ENTITIES

EmployeeEmployee IDLast nameFirst name

Manages

Employee ID

Last Name

First Name

Supervisor<FK Employee ID>

Employee

Page 24: Converting ERDs to Relational Tables

24

PrerequisiteSELF REFERENCING ENTITIES

CourseNumber NameCredits

Number

Name

Credits

CourseCourse<FK Number>

Prereq<FK Number>

Prerequisites

Page 25: Converting ERDs to Relational Tables

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: Converting ERDs to Relational Tables

QUESTIONS?

26

Page 27: Converting ERDs to Relational Tables

27

QUIZ BREAK!!!

FacultyFaculty IDLast nameFirst name

StudentStudent IDLast nameFirst name

Major Advisor

Convert to tables

Page 28: Converting ERDs to Relational Tables

28

QUIZ BREAK!!! Convert to tables

HourlyHoursRate

SalarySalaryContract Expires

EmployeeEmployee IDLast nameFirst name

Page 29: Converting ERDs to Relational Tables

29

QUIZ BREAK!!! Convert to tables

InBuilding

Building IDNameAddress

RoomRoom #Capacity

Page 30: Converting ERDs to Relational Tables

30

QUIZ BREAK!!! Convert to tables

Married

FacultyFaculty IDFirst NameLast Name