Upload
becca
View
33
Download
0
Tags:
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
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
StudentStudent 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
FacultyFaculty IDLast nameFirst name
OfferingSection #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 entitiesOffering
Section #RoomTime
StudentStudent 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
OfferingSection #RoomTime
<Number>
Section # Room Time
Offering
QUESTIONS?
9
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
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
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
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
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
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
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”
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
StudentMajorGrad Date
FacultyDepartmentOffice
College PeopleCollege IDLast nameFirst name
College ID
Last Name
First Name
<College ID>
Depart-ment
OfficeCollege 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 RULEFaculty
Faculty IDLast nameFirst name
DepartmentDept. IDFunding
Chairs20
Faculty ID
Last Name
First Name
<Dept ID>
Dept ID Fund-ing
<Faculty ID>
Faculty Department
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
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
EmployeeEmployee IDLast nameFirst name
Manages
Employee ID
Last Name
First Name
Supervisor<FK Employee ID>
Employee
24
PrerequisiteSELF REFERENCING ENTITIES
CourseNumber NameCredits
Number
Name
Credits
CourseCourse<FK Number>
Prereq<FK 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!!!
FacultyFaculty IDLast nameFirst name
StudentStudent IDLast nameFirst name
Major Advisor
Convert to tables
28
QUIZ BREAK!!! Convert to tables
HourlyHoursRate
SalarySalaryContract Expires
EmployeeEmployee IDLast nameFirst name
29
QUIZ BREAK!!! Convert to tables
InBuilding
Building IDNameAddress
RoomRoom #Capacity
30
QUIZ BREAK!!! Convert to tables
Married
FacultyFaculty IDFirst NameLast Name