Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 4701 Chapter 9-1 Chap 9 (6/5e): ER to Relational Transformation Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University.

Similar presentations


Presentation on theme: "CSE 4701 Chapter 9-1 Chap 9 (6/5e): ER to Relational Transformation Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University."— Presentation transcript:

1 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 Connecticut 191 Auditorium Road, Box U-155 Storrs, CT 06269-3155 steve@engr.uconn.edu http://www.engr.uconn.edu/~steve (860) 486 - 4818 n The Majority of these slides are being used with the permission of Dr. Ling Lui, Associate Professor, College of Computing, Georgia Tech. n Other slides (figures) have been adapted from the AWL web site for the textbook.

2 CSE 4701 Chapter 9-2 Designing a Relational DB Schema n Defining Relations l Deciding which Attributes belong Together in Each Relation l Choosing Appropriate Names for the Relations and Their Attributes l Specifying the Domains and Data Types of the Various Attributes l Identifying the Candidate Keys and Choosing a PK for Each Relation, and Specifying All Foreign Keys n Two Techniques for Relational Schema Design l Using ER-to-Relational Mapping (Chapter 9) l Relational Normalization Theory (Chapter 14)

3 CSE 4701 Chapter 9-3 Design Process - Where are we? Conceptual Design Conceptual Schema (ER Model) Logical Design Logical Schema (Relational Model) Step 1: ER-to-Relational Mapping Analysis of Schema Step 2: Normalization Normalized Schema

4 CSE 4701 Chapter 9-4 ER-to-Relational Mapping Algorithm n Step 1: For Each Regular Entity Type E l Create a Relation R E l Include only the Simple Attributes of a Composite Attribute n Step 2: For Each Weak Entity Type W with Owner Entity Type E l Create a Relation R W l Include as Attributes à All Simple Attributes of W à Primary Key attribute(s) of the Relation that Corresponds to W’s Owner Entity Type E

5 CSE 4701 Chapter 9-5 ER-to-Relational Mapping Algorithm n Step 3: For Each 1:1 Relationship l Identify the Relations R 1 and R 2 l Include as Foreign Key of one Relation the Primary Key of the Other Relation n Step 4: For each Regular 1:n Relationship l 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

6 CSE 4701 Chapter 9-6 ER-to-Relational Mapping Algorithm n Step 5: For Each Binary n:m Relationship l 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 n Step 6: For Each Multi-valued Attribute A l Create a New Relationship R that Includes à An Attribute Corresponding to A à The PK Attribute of the Relation Whose Corresponding Entity Type or Relationship Has A as an Attribute

7 CSE 4701 Chapter 9-7 ER-to-Relational Mapping Algorithm n Step 7: For Each n-ary Relationship R, n>2 l Create a New Relation to Represent R n Step 8: Convert Each Specialization for Superclass C with Attributes {k, A 1, …, A n } (k is the PK), where C has n Subclasses {S 1,..., S n } Create a Relation S i for each Subclass Entity (1<= i <= n) with Attributes Attrs(S i ) = {k}  {attributes of S i }, and PK{S i } = k l Note that the Relation for C was created in an Earlier Step l Note also that there are Three Other Options for Mapping Specialization Hierarchies

8 CSE 4701 Chapter 9-8 Recall our Ongoing Example EMPLOYEE PROJECT Responsibility Duration Budget Project Name Project No Employee No Employee Name SalaryTitle WORKS ON Address CityApt. # Street # Location 1 N MANAGES BALANCE Expenses 1 RECORDS ACCOUNT Income 1 DateAmount SUPPLY SUPPLIER Supplier No Supplier Name LocationCredit N M PART L Part No Part Name 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  

9 CSE 4701 Chapter 9-9 Can we Transition the Previous ER to... Note that there are no subclasses here!

10 CSE 4701 Chapter 9-10 Step 1 - Handling Entities n For Each Regular Entity Type E in the E-R Schema, Create a Relation R l Include as Attributes in R Only the Simple Attributes of E l For Composite Attributes of E, Include Only their Constituent Simple Attributes in R l 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 l For Multi-Valued Attributes – Create a new Table in Step 6

11 CSE 4701 Chapter 9-11 n Create the Following Strong Entity Types: l Create Four Corresponding Relations with Attribute Keys as PKs Step 1 – Example SUPPLIER Supplier No Supplier Name LocationCredit PROJECT Budget Project Name Project No Location PART Part No Part Name Weight Color EMPLOYEE Employee No Employee Name SalaryTitle Address CityApt. # Street #

12 CSE 4701 Chapter 9-12 n 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) n Notes: l LOCATION is Not Included in PROJECT – handled later in Step 6 l ACCOUNT is Weak and ENGINEER, SECRETARY, and SALESPERSON are Subclasses Step 1 – Example

13 CSE 4701 Chapter 9-13 Step 2 – Weak Entities n For Each Weak Entity Type W Associated with the Strong Entity Type E in the E-R Schema, Create a Relation R l Attributes of R are the Simple Attributes of W (or the Simplified Versions of Composite Attributes) l Include Among the Attributes of R all of the Key Attributes of Strong Entity E à These are the Foreign Keys of R l The PK of R is the Combination of the PK of E and the Partial Key of W

14 CSE 4701 Chapter 9-14 n Create relation ACCOUNT as follows ACCOUNT(PJNO, ACNO, INCOME, EXPENSES) n 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 #

15 CSE 4701 Chapter 9-15 If there was a Dependent … n Create a Dependent Table: l DEPENDENT(ENO, NAME, SEX, BIRTHDATE, RELATIOSHIP) l Primary Key from EMPLOYEE (ENO) PLUST DEPENDENT (NAME)

16 CSE 4701 Chapter 9-16 Step 3 – 1:1 Relationships n For Each 1:1 Relationship R in E-R Schema where the Two Related Entities are E1 and E2 n Let Relations S and T Correspond to E1 and E2 Respectively l 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 l If there are Attributes Associated With the Relationship R, Include Them in S l You May Want to Rename the Attributes When You Do This

17 CSE 4701 Chapter 9-17 n For 1:1 Relationship MANAGES between the EMPLOYEE and PROJECT Entities l Choose PROJECT as S, Since its Participation in the MANAGES relationship is Total Include in PROJECT the PK of EMPLOYEE PROJECT(PJNO, PNAME, BUDGET) Becomes PROJECT(PJNO, PNAME, BUDGET, MGR) Step 3 – Example MANAGES 1 1 PROJECT Budget Project Name Project No Location EMPLOYEE Employee No Employee Name SalaryTitle Address CityApt. # Street #

18 CSE 4701 Chapter 9-18 n For 1:1 Relationship RECORDS between PROJECT and ACCOUNT Entities: l Choose ACCOUNT as S (ACCOUNT is a Weak Entity, so this is the only choice that makes sense) l Include PJNO (done in step 2) and BALANCE ACCOUNT(PJNO, ACNO, INCOME, EXPENSES) Becomes ACCOUNT(PJNO, ACNO, INCOME, EXPENSES, BALANCE) Step 3 – Example RECORDS 11 PROJECT Budget Project Name Project No Location BALANCE Expenses ACCOUNT IncomeAcount #

19 CSE 4701 Chapter 9-19 n For Each Regular (Non-weak) Binary 1:N Relationship Type R in the E-R Schema l Identify the Relation S that Corresponds to the Entity Type at the N-side of the Relationship l Let the Other Relation on the 1-side be T à Include in S as a Foreign Key, the Primary Key of T à If There are Attributes Associated with the Relationship R, Include them in S as well Step 4 – 1:N Relationships

20 CSE 4701 Chapter 9-20 Step 4 – Example n We have only the WORKS ON relationship Defined between PROJECT and EMPLOYEE l N side of the Relationship is EMPLOYEE l 1 side of the Relationship is PROJECT WORKS_ON 1 N PROJECT Budget Project Name Project No Location EMPLOYEE Employee No Employee Name SalaryTitle Address CityApt. # Street # Responsibility Duration

21 CSE 4701 Chapter 9-21 Step 4 – Example n Include in EMPLOYEE l Primary Key (PJNO) of PROJECT l Attributes of the WORKS ON relationship (Duration & Responsibility) l This is Since EMPLOYEE WORKS ON Only 1 PROJECT n EMPLOYEE(ENO, ENAME, TITLE, SALARY, APT#, STREET, CITY) Becomes EMPLOYEE(ENO, ENAME, TITLE, SALARY, APT#, STREET, CITY, PJNO, DURATION, RESP)

22 CSE 4701 Chapter 9-22 Step 5 – M:N Relationships n For each Binary M:N Relationship Type R Connecting E1 and E2 in the ER schema, create a relation S: l Include as FKs of S, the PKs of the two relations that correspond to E1 and E2 à These Attributes, Together, Form the primary key of S à Also Include in S any Attributes of the Relationship R

23 CSE 4701 Chapter 9-23 Step 5 – M:N Relationships n We have one M:N Relationship: CONTAIN, which is a Recursive Relationship over the PART Entity l We create the following relation: CONTAIN(PNO, CPNO, QTY) n Recursive Relationship Requires us to Distinguish the Part No as PNO and CPNO PART Part No Part Name Weight Color CONTAIN M N Made-up ofConsists of QTY

24 CSE 4701 Chapter 9-24 n For each Multivalued Attribute A, Create a New Relation R l The Attributes of R are A (if composite, then use only the simple components) l Include in R the PK K of the entity that contained A l The PK of R then becomes K and A together n Recall that Multivalued Means that a Given Attribute has Multiple Values in the Entity l Previous Degrees of a Student {Bachelors, Masters} l Can have Neither, One, or Both for each Student Step 6 – Multivalued Attributes

25 CSE 4701 Chapter 9-25 n In our Example, we create One New Relation for the Multivalued Attribute LOCATION in PROJECT l This Relation is Created as Follows: LOC (PJNO, LOCATION) l That is, Storrs, Hartford, etc., for Project P1 Step 6 – Multivalued Attributes PROJECT Budget Project Name Project No Location The Same Project (Project No) can be Spread Across Multiple Locations

26 CSE 4701 Chapter 9-26 n For Each Higher Order Relationship Type R Connecting E 1, E 2, …, E n in the E-R schema, Create a relation S l Include in S the PKs of the Relations Corresponding to E 1, E 2, …, E n l Also Include in S any Attributes of R l The PK of S is the Combination of the PKs of the Relations Corresponding to E 1, E 2, …, E n Step 7 – Higher Order Relationships

27 CSE 4701 Chapter 9-27 PROJECT SUPPLY SUPPLIER N M PART L Step 7 – Example n The Only High-Order Relation is SUPPLY between SUPPLIER, PROJECT and PART l Create Relation SUPPLY where Amount and Date were Attributes Defined on Supply à SUPPLY(SNO, PJNO, PNO, AMOUNT, DATE) DateAmount

28 CSE 4701 Chapter 9-28 n For Each Specialization with m Subclasses {S 1, …, S m } and Generalized Superclass C, where the Attributes of C are {k, A 1, …, A n } (k is the PK), Convert According to the Following: Option 1: General Case: à Create a Relation T for C with Attributes {k, A 1, …, A n } and use k as the PK à Create one Relation U i for each S i Include in U i all the attributes of S i and k Use k as the primary key of U i. Step 8 – Specialization

29 CSE 4701 Chapter 9-29 Option 2: No Superclass Relation: à Create One Relation U i for each S i. à Include in U i all Attributes of S i and {k, A 1, …, A n } à Use k as the Primary Key of U i Option 3: For Disjoint Subclasses: à Create a Single Relation U which Contains all the Attributes of all S i and {k, A 1, …, A n } and t à Use k as the primary key of U i à The Attribute t Indicates the Type Attribute According to which Specialization is Performed Step 8 – Specialization (cont’d)

30 CSE 4701 Chapter 9-30 Option 4: For Overlapping Subclasses: à Create a single relation U which contains all Attributes of all S i and all Attributes of C ({k, A 1, …, A n }) and {t 1, …, t m } à Use k as the Primary Key of U i à The Attributes t i are Boolean Valued, Indicating if a Tuple Belongs to Subclass S i à Note: May Generate a Large Number of Null Values in the Relation Step 8 – Specialization (cont’d)

31 CSE 4701 Chapter 9-31 n Specialization of EMPLOYEE l EMPLOYEE Already Exists; option 2 is not valid l Specialization is Disjoint; option 4 is not valid l 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) l Option 3: TYPE EMPLOYEE(ENO, ENAME, TITLE, SALARY, APT#, STREET, CITY, PJNO, DURATION, RESP, TYPE, PROJECT, OFFICE, SPECIALTY, CAR, REGION) Step 8 – Example

32 CSE 4701 Chapter 9-32 Step 8 – Example (cont’d) n Specialization of PART l Relation PART Already Exists; Option 2 is not valid l Specialization is Overlapping;Option 3 is not valid l Option 1: à PART(PNO, PNAME, WGT, COLOR) à MANUFACTURED_PART(PNO, BATCH#, DRAWING#) à PURCHASED_PART(PNO, PRICE) l Option 4: à PART( PNO, PNAME, WGT, COLOR, MAN, BATCH#, DRAWING#, PURC, PRICE) à Note that MAN and PURC are Boolean Flags

33 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

34 CSE 4701 Chapter 9-34 Step 8 – Option 1 Example

35 CSE 4701 Chapter 9-35 Step 8 – Option 2 Example

36 CSE 4701 Chapter 9-36 Step 8 – Option 3 Example SecretaryTechEngr Where JobType Playing the Role of a Type Attribute

37 CSE 4701 Chapter 9-37 Step 8 – Option 4 Example Boolean Mflag - are the next three fields active - then Manufactured_Part Pflag - are the next three fields active - then Purchased_Part

38 CSE 4701 Chapter 9-38 Step 8 - Recall Specialization Lattice Person EmployeeAlumniStudent StaffFacultyStudAsst GradUnderGrad ResAsstTeachAsst

39 CSE 4701 Chapter 9-39 Step 8 - Corresponding Relations

40 CSE 4701 Chapter 9-40 Step 8 - Recall Categories - Figure 4.8

41 CSE 4701 Chapter 9-41 Step 8 - Recall Categories - Figure 4.8

42 CSE 4701 Chapter 9-42 Step 8 - Corresponding Relations

43 CSE 4701 Chapter 9-43 In Class Exercise n How would we Translate Solutions from Homework 1? l ER Solution for Problem 1.1? l EER Solution for Problem 1.2?

44 CSE 4701 Chapter 9-44 ER for Problem 1.1 Phone# Patient Drug Physician name address email SSN status price name expiration name email address specialty Primary N 1 DEA# Prescribes refills N N dosage date Fill requirement pattern Sold By Purchasing Contract start_date end_date

45 CSE 4701 Chapter 9-45 EER for Problem 1.2 Patient Drug Physician SSN status price name expiration specialty Primary N 1 Prescribes refills N N dosage date Fill requirement pattern Sold By Purchasing Contract start_date end_date Person   Drug Prescriber  DEA#

46 CSE 4701 Chapter 9-46 EER for Problem 1.2 n Notice Movement of Common Attributes to Person and DEA number to Presriber n “Person” is a parent of both Patient and Physician and Physician also a Sub-entity of Drug Prescriber Person phone name address email Drug Prescriber  Physician   DEA#

47 CSE 4701 Chapter 9-47 Patient Drug Physician SSN status price name expiration specialty Primary N 1 Prescribes refills N N dosage date Fill requirement pattern Sold By Purchasing Contract start_date end_date Person   Drug Prescriber  DEA# name email address

48 CSE 4701 Chapter 9-48 What are Steps n STEP 1: CONVERT EACH ENTITY n CREATE A TABLE n STEP 2: WEAK ENTITIES n CREATE TABLE LINK TO STRONG n STEP 3: 1-1 RELATIONSHIPS n INCLUDE FKEY IN ONE OF TWO n STEP 4: 1- MANY n INCLUDE REFERNCE FKEY TO MANY SIDE n STEP 5: MANY-MANY n NEW TABLE n STEP 6: MULTI-VALUED n NEW TABLE n STEP 7: N-ARY (3 or MORE) n NEW TABLE n STEP 8: INHERITANCE

49 CSE 4701 Chapter 9-49 Concluding Remarks n What have we Learned in Chapter 9? l Transition from a Conceptual Schema (ER) to Set of Relations l Transition is Necessary as First Step in the Relational Database Design Process l Result of Transition is a Set of Relations that Capture the Entities and “Relationships” in ER Diagram l Objective: Flatten out the ER into Relations n How is Chapter 9 Related to the Semester Project? l Phase II in the Semester Project is to Transition your ER Diagram into a Set of Relational Tables à Step 1: ER to Relational Transformation (Chapter 9) à Step 2: Relational Normalization (Chapter 14)


Download ppt "CSE 4701 Chapter 9-1 Chap 9 (6/5e): ER to Relational Transformation Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University."

Similar presentations


Ads by Google