1 / 33

Database Design 4: Deriving Tables From Data Models

Database Design 4: Deriving Tables From Data Models. CS 320. Review: Relationships in Tables. Created by shared key fields. Types of Database Key Fields. Primary keys Foreign keys Composite keys. Review: Primary Key. Uniquely identify a record

Download Presentation

Database Design 4: Deriving Tables From Data Models

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Database Design 4: Deriving Tables From Data Models CS 320

  2. Review: Relationships in Tables Created by shared key fields

  3. Types of Database Key Fields Primary keys Foreign keys Composite keys

  4. Review: Primary Key • Uniquely identify a record • Must be unique with respect to all other records in the table • You can create surrogate keys to serve as primary keys

  5. Foreign Key Field that is a primary key in another table Used to create a relationship

  6. Composite Key Primary key comprised of 2 or more fields

  7. Review: Levels of data models • Conceptual: describes WHAT data the system contains • Logical: describes HOW the database will be structured, regardless of the DBMS • Physical: describes HOW the system will be implemented using a specific DBMS

  8. Goal of Logical DB Design: Database Integrity • Entity integrity • Every record must have a unique primary key • The primary key value cannot be NULL • Referential integrity • Every foreign key must be the primary key of its parent table • Every foreign key value must exist in its parent table

  9. Examples of Integrity Problems ?

  10. Steps for Transforming an ER Model into a Sound Logical Database Design • Create a table to represent every entity • Entity PK is table PK • Represent each ER model relationship using foreign keys

  11. Representing 1:M Relationships • Put the PK of the “1” side into the “M” table ACME_EMPLOYEE(Emp_ID, Emp_Last_Name, Emp_Office, Dept_ID) ACME_DEPARTMENT(Dept_ID, Dept_Name, Dept_Office_Loc)

  12. Why can’t you put the PK of the “M” side into the “1” table? • A department has multiple employees… Approach 1: delimit multiple values using commas? Result: Non-relational tables (cell contains multiple values) • Approach 2: Repeat "1" side records for each "M" record? • Result: Redundant data!

  13. Representing M:M Relationships • Create a linking table • Also called a bridge table • PK is a composite key comprised of PK’s of both entities in the M:M relationship • These fields are part of the PK, and are also FK’s

  14. Representing M:M Relationships

  15. What do you name the linking table? ACME_EMPLOYEE_PROJECT Typically, a compound word comprised of the two participating entities

  16. What about composite M:M relationships? • If the relationship has an attribute, place the attribute in the linking table

  17. Deriving Tables for 1:1 Relationships • In general, move one entity's non-key attributes into the other entity's table ACME_EMPLOYEE Emp_ID Emp_Last_Name Emp_Office Emp_Spouse_First Emp_Spouse_Last

  18. Deriving Tables for 1:1 Relationships • How do you decide which entity to keep? • The "strong" entity • Typically in a 1:1 relationship, one entity's instance doesn't exist without the other's

  19. Unary Relationships ACME_EMPLOYEE Employee_ID Employee_First_Name Employee_Last_Name Employee_DOB SupervisorID (FK) UWEC_STUDENT_ROOMS_WITH Student1_ID (FK) Student2_ID (FK) Treat just like any other 1:1, 1:M, or M:M relationship, except modify key names to avoid duplicate field names

  20. Ternary Relationships UWEC_STUDENT_ ADVISOR_PROGRAM Student_ID (FK) Advisor_ID (FK) Program_ID (FK) Create a 3-way linking table

  21. Generalization/Specialization Create tables for the supertype and each of the subtypes Put PK of supertype into each subtype table as both a PK and FK

  22. Generalization/Specialization UWEC_Person Person_ID Person_First_Name Person_Last_Name Person_MI Person_DOB Supertype Subtypes UWEC_STUDENT Person_ID (FK) Student_Major_Program Student_Minor_Program UWEC_ INSTRUCTOR Person_ID (FK) Instructor_Title Instructor_Office Instructor_Salary UWEC_Staff Person_ID (FK) Staff_Rank Staff_Hourly_Rate

  23. Generalization/Specialization Data Table Example UWEC_STUDENT UWEC_PERSON UWEC_INSTRUCTOR UWEC_STAFF

  24. Test Yourself: In the CANDY_PURCHASE table, PURCH_ID is an example of a: CANDY_PURCHASE A primary key A foreign key Part of a composite key Both b and c None of the above

  25. Test Yourself: In the CANDY_PURCHASE table, PURCH_ID is an example of: CANDY_PURCHASE A primary key A foreign key Part of a composite key Both b and c None of the above

  26. Test Yourself: In the CANDY_PURCHASE table, PROD_ID is an example of a: CANDY_PURCHASE Primary key Foreign key Part of a composite key Both b and c None of the above

  27. Test Yourself: In the CANDY_PURCHASE table, PROD_ID is an example of a: CANDY_PURCHASE Primary key Foreign key Part of a composite key Both b and c None of the above

  28. Test Yourself: How many database tables would you create for the following ER model? 3 4 5 None of the above

  29. Test Yourself: How many database tables would you create for the following ER model? 3 4 5 None of the above

  30. Your Turn #1 • Derive the tables for the following ER model. Format the table structures using the format TABLE_NAME(Field1, Field2, …). Underline primary key fields, and label foreign keys using the designation (FK).

  31. Your Turn #1 Solution ALS_CUSTOMER(Customer_ID, Customer_Last_Name, Customer_First_Name, Customer_MI, Customer_Address, Customer _City, Customer_State, Customer_Zip, Customer_Phone) ALS_CAR(Car_VIN, Car_Make, Car_Model, Car_Year, Customer_ID (FK)) ALS_WORK_ORDER (WO_ID, WO_Date, Car_VIN (FK)) ALS_SERVICE (Service_ID, Service_Name, Service_Desc, Service_Charge) ALS_WORK_ORDER_SERVICE (Service_ID (FK), WO_ID (FK))

  32. Your Turn #2 • Derive the tables for the following ER model. Format the table structures using the format TABLE_NAME(Field1, Field2, …). Label all primary keys by underlining them, and label foreign keys using the designation (FK).

  33. Your Turn #2 Solution • UWEC_PERSON(Person_ID, Person_First_Name, Person_Last_Name, Person_MI, Person_Email) • UWEC_STUDENT(Person_ID (FK), Stu_Campus_Address, Stu_Campus_City, Stu_Campus_State, Stu_Campus_Zip, Stu_Campus_Phone, Stu_Perm_Address, Stu_Perm_City, Stu_Perm_State, Stu_Perm_Zip, Stu_Perm_Phone) • UWEC_INSTRUCTOR(Person_ID (FK), Instructor_Title, Instructor_Office, Instructor_Office_Phone) • UWEC_COURSE (Course_ID , Course_Dept, Course_Name, Course_Desc) • UWEC_COURSE_SECTION (Section_ID, Section_Term, Section_Day, Section_Time, Section_Location, Person_ID (FK), Course_ID (FK)) • UWEC_STUDENT_COURSE_SECTION(Person_ID (FK), Section_ID (FK), Enrollment_Grade) • UWEC_ADVISOR (Student_Person_ID (FK), Instructor_Person_ID (FK))

More Related