cse 4701 chapter 9-1 chap 9 (6/5e): er to relational transformation prof. steven a. demurjian, sr....
TRANSCRIPT
CSE 4701
Chapter 9-1
Chap 9 (6/5e): ER to Relational Transformation
Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department
The University of Connecticut191 Auditorium Road, Box U-155
Storrs, CT [email protected]
http://www.engr.uconn.edu/~steve(860) 486 - 4818
The Majority of these slides are being used with the permission of Dr. Ling Lui, Associate Professor, College of Computing, Georgia Tech.
Other slides (figures) have been adapted from the AWL web site for the textbook.
CSE 4701
Chapter 9-2
Designing a Relational DB Schema Defining Relations
Deciding which Attributes belong Together in Each Relation
Choosing Appropriate Names for the Relations and Their Attributes
Specifying the Domains and Data Types of the Various Attributes
Identifying the Candidate Keys and Choosing a PK for Each Relation, and Specifying All Foreign Keys
Two Techniques for Relational Schema Design Using ER-to-Relational Mapping (Chapter 9) Relational Normalization Theory (Chapter 14)
CSE 4701
Chapter 9-3
Design Process - Where are we?Conceptual
Design
ConceptualSchema
(ER Model)
LogicalDesign
Logical Schema(Relational Model)
Step 1: ER-to-Relational Mapping
Analysisof Schema
Step 2: Normalization
Normalized Schema
CSE 4701
Chapter 9-4
ER-to-Relational Mapping Algorithm
Step 1: For Each Regular Entity Type E Create a Relation RE
Include only the Simple Attributes of a Composite Attribute
Step 2: For Each Weak Entity Type W with Owner Entity Type E Create a Relation RW
Include as Attributes All Simple Attributes of W Primary Key attribute(s) of the Relation that Corresponds
to W’s Owner Entity Type E
CSE 4701
Chapter 9-5
ER-to-Relational Mapping Algorithm
Step 3: For Each 1:1 Relationship Identify the Relations R1 and R2
Include as Foreign Key of one Relation the Primary Key of the Other Relation
Step 4: For each Regular 1:n Relationship Include as Foreign Key in the Entity Type at the n-
side of the Relationship, the Primary Key of the Entity Type at the 1-side of the Relationship
CSE 4701
Chapter 9-6
ER-to-Relational Mapping Algorithm
Step 5: For Each Binary n:m Relationship Create a New Relation, whose Attributes Include
All Simple Attributes of the n:m Relationship as Non-key Attributes
PKs of the Relations that Represent the Participating Entity Types, as FK Attributes in this New Relation
Step 6: For Each Multi-valued Attribute A Create a New Relationship R that Includes
An Attribute Corresponding to AThe PK Attribute of the Relation Whose Corresponding
Entity Type or Relationship Has A as an Attribute
CSE 4701
Chapter 9-7
ER-to-Relational Mapping Algorithm
Step 7: For Each n-ary Relationship R, n>2 Create a New Relation to Represent R
Step 8: Convert Each Specialization for Superclass C with Attributes {k, A1, …, An} (k is the PK), where C has n Subclasses {S1, ..., Sn} Create a Relation Si for each Subclass Entity
(1<= i <= n) with Attributes Attrs(Si) = {k} {attributes of Si}, and PK{Si} = k
Note that the Relation for C was created in an Earlier Step
Note also that there are Three Other Options for Mapping Specialization Hierarchies
CSE 4701
Chapter 9-8
Recall our Ongoing Example
EMPLOYEE
PROJECT
Responsibility
Duration
BudgetProjectName
Project No
Employee No
EmployeeName
SalaryTitle
WORKS ON
Address
CityApt. #
Street #
Location
1
N
MANAGES
BALANCEExpenses
1RECORDSACCOUNT
Income
1
DateAmount
SUPPLYSUPPLIER
Supplier No SupplierName
LocationCredit
N M
PART
LPart No
PartName
Weight
Color
CONTAIN
M N
Made-up of Consists of
1
1
QTY
SALESPERSON
Car
Region
SECRETARY
Specialty
Office
ENGINEER
Project
Office
Acount #
d
o
MANUFACTURED_PART PURCHASED_PART
Batch No Drawing No Price
CSE 4701
Chapter 9-9
Can we Transition the Previous ER to ...
Note that there areno subclasses here!
CSE 4701
Chapter 9-10
Step 1 - Handling Entities
For Each Regular Entity Type E in the E-R Schema, Create a Relation R Include as Attributes in R Only the Simple
Attributes of E For Composite Attributes of E, Include Only their
Constituent Simple Attributes in R The Key of E Becomes the PK of R
If There is More Than One Key Attributes of E, Then Choose One as the Primary Key of R
For Multi-Valued Attributes – Create a new Table in Step 6
CSE 4701
Chapter 9-11
Create the Following Strong Entity Types:
Create Four Corresponding Relations with Attribute Keys as PKs
Step 1 – Example
SUPPLIER
Supplier No SupplierName
LocationCredit
PROJECT
BudgetProjectName
Project No
Location
PART
Part No
PartName
Weight
Color
EMPLOYEEEmployee No
EmployeeName
SalaryTitle
Address
CityApt. #
Street #
CSE 4701
Chapter 9-12
The Keys are Underlined
EMPLOYEE(ENO, ENAME, TITLE, SALARY, APT#, STREET, CITY)
PROJECT(PJNO, PNAME, BUDGET)
SUPPLIER(SNO, SNAME, CREDIT, LOCATION)
PART(PNO, PNAME, WGT, COLOR)
Notes:
LOCATION is Not Included in PROJECT – handled later in Step 6
ACCOUNT is Weak and ENGINEER, SECRETARY, and SALESPERSON are Subclasses
Step 1 – Example
CSE 4701
Chapter 9-13
Step 2 – Weak Entities
For Each Weak Entity Type W Associated with the Strong Entity Type E in the E-R Schema, Create a Relation R Attributes of R are the Simple Attributes of W (or
the Simplified Versions of Composite Attributes) Include Among the Attributes of R all of the Key
Attributes of Strong Entity E These are the Foreign Keys of R
The PK of R is the Combination of the PK of E and the Partial Key of W
CSE 4701
Chapter 9-14
Create relation ACCOUNT as follows
ACCOUNT(PJNO, ACNO, INCOME, EXPENSES)
Note that ACNO Corresponds to Account# on ACCOUNT
PJNO Corresponds to ProjectNo on PROJECT
We’ll Handle Records Relationship in Separate Step
foreign key
Step 2 – Example
Expenses
ACCOUNT
IncomeAcount #
CSE 4701
Chapter 9-15
If there was a Dependent … Create a Dependent Table:
DEPENDENT(ENO, NAME, SEX, BIRTHDATE, RELATIOSHIP)
Primary Key from EMPLOYEE (ENO) PLUST DEPENDENT (NAME)
CSE 4701
Chapter 9-16
Step 3 – 1:1 Relationships
For Each 1:1 Relationship R in E-R Schema where the Two Related Entities are E1 and E2
Let Relations S and T Correspond to E1 and E2 Respectively Choose One of the Relations, Preferably One Whose
Participation in R is Total (Say S) Include in S as a FK, the PK of T
If there are Attributes Associated With the Relationship R, Include Them in S
You May Want to Rename the Attributes When You Do This
CSE 4701
Chapter 9-17
For 1:1 Relationship MANAGES between the EMPLOYEE and PROJECT Entities Choose PROJECT as S, Since its Participation in
the MANAGES relationship is TotalInclude in PROJECT the PK of EMPLOYEE
PROJECT(PJNO, PNAME, BUDGET) BecomesPROJECT(PJNO, PNAME, BUDGET, MGR)
Step 3 – Example
MANAGES1
1PROJECT
BudgetProjectName
Project No
Location
EMPLOYEEEmployee No
EmployeeName
SalaryTitle
Address
CityApt. #
Street #
CSE 4701
Chapter 9-18
For 1:1 Relationship RECORDS between PROJECT and ACCOUNT Entities: Choose ACCOUNT as S (ACCOUNT is a Weak
Entity, so this is the only choice that makes sense) Include PJNO (done in step 2) and BALANCE
ACCOUNT(PJNO, ACNO, INCOME, EXPENSES) BecomesACCOUNT(PJNO, ACNO, INCOME, EXPENSES,
BALANCE)
Step 3 – Example
RECORDS1 1
PROJECT
BudgetProjectName
Project No
Location BALANCE
Expenses
ACCOUNT
IncomeAcount #
CSE 4701
Chapter 9-19
For Each Regular (Non-weak) Binary 1:N Relationship Type R in the E-R Schema Identify the Relation S that Corresponds to the
Entity Type at the N-side of the Relationship Let the Other Relation on the 1-side be T
Include in S as a Foreign Key, the Primary Key of TIf There are Attributes Associated with the Relationship
R, Include them in S as well
Step 4 – 1:N Relationships
CSE 4701
Chapter 9-20
Step 4 – Example
We have only the WORKS ON relationshipDefined between PROJECT and EMPLOYEE N side of the Relationship is EMPLOYEE 1 side of the Relationship is PROJECT
WORKS_ON1
N
PROJECT
BudgetProjectName
Project No
Location
EMPLOYEEEmployee No
EmployeeName
SalaryTitle
Address
CityApt. #
Street #
Responsibility
Duration
CSE 4701
Chapter 9-21
Step 4 – Example
Include in EMPLOYEE Primary Key (PJNO) of PROJECT Attributes of the WORKS ON relationship
(Duration & Responsibility) This is Since EMPLOYEE WORKS ON Only 1
PROJECT
EMPLOYEE(ENO, ENAME, TITLE, SALARY, APT#, STREET, CITY)
BecomesEMPLOYEE(ENO, ENAME, TITLE, SALARY, APT#,
STREET, CITY, PJNO, DURATION, RESP)
CSE 4701
Chapter 9-22
Step 5 – M:N Relationships
For each Binary M:N Relationship Type R Connecting E1 and E2 in the ER schema, create a relation S: Include as FKs of S, the PKs of the two relations
that correspond to E1 and E2These Attributes, Together, Form the primary key of SAlso Include in S any Attributes of the Relationship R
CSE 4701
Chapter 9-23
Step 5 – M:N Relationships
We have one M:N Relationship: CONTAIN, which is a Recursive Relationship over the PART Entity
We create the following relation:
CONTAIN(PNO, CPNO, QTY)
Recursive Relationship Requires us to Distinguish the Part No as PNO and CPNO
PART
Part No
PartName
Weight
Color
CONTAIN
M N
Made-up of Consists of
QTY
CSE 4701
Chapter 9-24
For each Multivalued Attribute A, Create a New Relation R The Attributes of R are A (if composite, then use
only the simple components) Include in R the PK K of the entity that contained A The PK of R then becomes K and A together
Recall that Multivalued Means that a Given Attribute has Multiple Values in the Entity Previous Degrees of a Student {Bachelors, Masters} Can have Neither, One, or Both for each Student
Step 6 – Multivalued Attributes
CSE 4701
Chapter 9-25
In our Example, we create One New Relation for the Multivalued Attribute LOCATION in PROJECT
This Relation is Created as Follows:
LOC (PJNO, LOCATION)
That is, Storrs, Hartford, etc., for Project P1
Step 6 – Multivalued Attributes
PROJECT
BudgetProjectName
Project No
Location
The Same Project (Project No) can be Spread Across Multiple Locations
CSE 4701
Chapter 9-26
For Each Higher Order Relationship Type R Connecting E1, E2, …, En in the E-R schema, Create a relation S Include in S the PKs of the Relations Corresponding
to E1, E2, …, En Also Include in S any Attributes of R The PK of S is the Combination of the PKs of the
Relations Corresponding to E1, E2, …, En
Step 7 – Higher Order Relationships
CSE 4701
Chapter 9-27
PROJECTSUPPLYSUPPLIERN M
PART
L
Step 7 – Example
The Only High-Order Relation is SUPPLY between SUPPLIER, PROJECT and PART Create Relation SUPPLY where Amount and Date
were Attributes Defined on SupplySUPPLY(SNO, PJNO, PNO, AMOUNT, DATE)
DateAmount
CSE 4701
Chapter 9-28
For Each Specialization with m Subclasses {S1, …, Sm} and Generalized Superclass C, where the Attributes of C are {k, A1, …, An} (k is the PK), Convert According to the Following:
Option 1: General Case:Create a Relation T for C with Attributes
{k, A1, …, An} and use k as the PK
Create one Relation Ui for each SiInclude in Ui all the attributes of Si and kUse k as the primary key of Ui.
Step 8 – Specialization
CSE 4701
Chapter 9-29
Option 2: No Superclass Relation:Create One Relation Ui for each Si.
Include in Ui all Attributes of Si and {k, A1, …, An}
Use k as the Primary Key of Ui
Option 3: For Disjoint Subclasses:Create a Single Relation U which Contains all the
Attributes of all Si and {k, A1, …, An} and t
Use k as the primary key of Ui
The Attribute t Indicates the Type Attribute According to which Specialization is Performed
Step 8 – Specialization (cont’d)
CSE 4701
Chapter 9-30
Option 4: For Overlapping Subclasses:Create a single relation U which contains all
Attributes of all Si and all Attributes of C ({k, A1, …, An}) and {t1, …, tm}
Use k as the Primary Key of Ui
The Attributes ti are Boolean Valued, Indicating if a Tuple Belongs to Subclass Si
Note: May Generate a Large Number of Null Values in the Relation
Step 8 – Specialization (cont’d)
CSE 4701
Chapter 9-31
Specialization of EMPLOYEE EMPLOYEE Already Exists; option 2 is not valid Specialization is Disjoint; option 4 is not valid Option 1:
EMPLOYEE(ENO, ENAME, TITLE, SALARY, APT#, STREET, CITY, PJNO, DURATION,
RESP)
ENGINEER (ENO, PROJECT, OFFICE)
SECRETARY(ENO, OFFICE, SPECIALTY)
SALESPERSON(ENO, CAR, REGION)
Option 3:EMPLOYEE(ENO, ENAME, TITLE, SALARY, APT#,
STREET, CITY, PJNO, DURATION, RESP, TYPE, PROJECT, OFFICE, SPECIALTY, CAR, REGION)
Step 8 – Example
CSE 4701
Chapter 9-32
Step 8 – Example (cont’d) Specialization of PART
Relation PART Already Exists; Option 2 is not valid Specialization is Overlapping;Option 3 is not valid Option 1:
PART(PNO, PNAME, WGT, COLOR) MANUFACTURED_PART(PNO, BATCH#,
DRAWING#)PURCHASED_PART(PNO, PRICE)
Option 4:PART( PNO, PNAME, WGT, COLOR,
MAN, BATCH#, DRAWING#, PURC, PRICE)
Note that MAN and PURC are Boolean Flags
CSE 4701
Chapter 9-33
EMPLOYEE(ENO, ENAME, TITLE, SALARY, APT#, STREET, CITY, PJNO, DURATION, RESP)
PROJECT(PJNO,PNAME,BUDGET,MGR)
SUPPLIER(SNO,SNAME,CREDIT,LOCATION)
PART(PNO, PNAME, WGT, COLOR, MAN, PURC, BATCH#, DRAWING#, PRICE)
ENGINEER(ENO, PROJECT,OFFICE)
SECRETARY(ENO, OFFICE, SPECIALTY)
SALESPERSON(ENO, CAR, REGION)
SUPPLY(SNO, PJNO, PNO, AMOUNT,DATE)
LOC(PJNO, LOCATION)
CONTAIN(PNO, CPNO,QTY)
ACCOUNT(PJNO, ACNO, INCOME, EXPENSES, BALANCE)
Final Set of Relations
CSE 4701
Chapter 9-34
Step 8 – Option 1 Example
CSE 4701
Chapter 9-35
Step 8 – Option 2 Example
CSE 4701
Chapter 9-36
Step 8 – Option 3 Example
Secretary Tech Engr
Where JobType Playing theRole of a Type Attribute
CSE 4701
Chapter 9-37
Step 8 – Option 4 Example
Boolean
BooleanMflag - are thenext three fields
active - thenManufactured_Part
Pflag - are thenext three fields
active - thenPurchased_Part
CSE 4701
Chapter 9-38
Step 8 - Recall Specialization Lattice
Person
Employee Alumni Student
Staff Faculty StudAsst Grad UnderGrad
ResAsst TeachAsst
CSE 4701
Chapter 9-39
Step 8 - Corresponding Relations
CSE 4701
Chapter 9-40
Step 8 - Recall Categories - Figure 4.8
CSE 4701
Chapter 9-41
Step 8 - Recall Categories - Figure 4.8
CSE 4701
Chapter 9-42
Step 8 - Corresponding Relations
CSE 4701
Chapter 9-43
In Class Exercise
How would we Translate Solutions from Homework 1? ER Solution for Problem 1.1? EER Solution for Problem 1.2?
CSE 4701
Chapter 9-44
ER for Problem 1.1
Phone#
Patient
Drug
Physician
name
address email
SSN
status
price
name
expiration
name
address
specialtyPrimary
N 1
DEA#
Prescribes
refills N
N
dosage
dateFill
requirementpattern
Sold By
Purchasing Contract
start_date end_date
CSE 4701
Chapter 9-45
EER for Problem 1.2
Patient
Drug
PhysicianSSN
status
price
name
expiration
specialtyPrimary
N 1
Prescribes
refills N
N
dosage
dateFill
requirementpattern
Sold By
Purchasing Contract
start_date end_date
Person
Drug Prescriber
DEA#
CSE 4701
Chapter 9-46
EER for Problem 1.2
Notice Movement of Common Attributes to Person and DEA number to Presriber
“Person” is a parent of both Patient and Physician and Physician also a Sub-entity of Drug Prescriber
Person
phone
name
address
Drug Prescriber
Physician
Physician
DEA#
CSE 4701
Chapter 9-47
Patient
Drug
PhysicianSSN
status
price
name
expiration
specialtyPrimary
N 1
Prescribes
refills N
N
dosage
dateFill
requirementpattern
Sold By
Purchasing Contract
start_date end_date
Person
Drug Prescriber
DEA#
name
emailaddress
CSE 4701
Chapter 9-48
What are Steps STEP 1: CONVERT EACH ENTITY
CREATE A TABLE STEP 2: WEAK ENTITIES
CREATE TABLE LINK TO STRONG STEP 3: 1-1 RELATIONSHIPS
INCLUDE FKEY IN ONE OF TWO STEP 4: 1- MANY
INCLUDE REFERNCE FKEY TO MANY SIDE STEP 5: MANY-MANY
NEW TABLE STEP 6: MULTI-VALUED
NEW TABLE STEP 7: N-ARY (3 or MORE)
NEW TABLE STEP 8: INHERITANCE
CSE 4701
Chapter 9-49
Concluding Remarks What have we Learned in Chapter 9?
Transition from a Conceptual Schema (ER) to Set of Relations
Transition is Necessary as First Step in the Relational Database Design Process
Result of Transition is a Set of Relations that Capture the Entities and “Relationships” in ER Diagram
Objective: Flatten out the ER into Relations How is Chapter 9 Related to the Semester Project?
Phase II in the Semester Project is to Transition your ER Diagram into a Set of Relational TablesStep 1: ER to Relational Transformation (Chapter 9)Step 2: Relational Normalization (Chapter 14)