Download presentation
Presentation is loading. Please wait.
Published byJonathan Henry Modified over 9 years ago
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 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database will be structured, regardless of the DBMS 3. 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 1. Create a table to represent every entity Entity PK is table PK 2. Represent each ER model relationship using foreign keys
11
Representing 1:M Relationships Put the PK of the “1” side into the “M” table Dept_ ID Dept_NameDept_Office_ Loc 1AccountingSSS415 2ITSSS406 3ProductionSSS312 Emp_ ID Emp_ Last_ Name Emp_ Office Dept_ ID (FK) 100SmithSSS1121 200JonesSSS1161 300BrownSSS1072 ACME_DEPARTMENT(Dept_ID, Dept_Name, Dept_Office_Loc) ACME_EMPLOYEE(Emp_ID, Emp_Last_Name, Emp_Office, Dept_ID)
12
Why can’t you put the PK of the “M” side into the “1” table? A department has multiple employees… Dept_ ID Dept_ Name Dept_ Office Emp_ID 1AccountingSSS415100, 200 2MISSSS406300 3ProductionSSS312 Dept_ ID Dept_ Name Dept_ Office Emp_ ID 1AccountingSSS415100 1AccountingSSS415200 2MISSSS406300 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 Project_ ID Project_NameProject_ Start_ Date 1Help Desk Intranet01/05/12 2Cold Fusion Study01/15/12 3Oracle Conversion11/15/11 Emp_ ID Emp_Last _Name Emp_ Office 100SmithSSS112 200JonesSSS116 300BrownSSS107 Project_ID (FK)Emp_ID (FK) 1100 2 2200
15
Project_ID (FK)Emp_ID (FK) 1100 2 2200 What do you name the linking table? Typically, a compound word comprised of the two participating entities ACME_EMPLOYEE_PROJECT
16
What about composite M:M relationships? If the relationship has an attribute, place the attribute in the linking table Project_ID (FK) Emp_ID (FK)Emp_Proj_Hrs 110030 210015 220025
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 Treat just like any other 1:1, 1:M, or M:M relationship, except modify key names to avoid duplicate field names Student1_ID (FK) Student2_ID (FK) ACME_EMPLOYEE Employee_ID Employee_First_Name Employee_Last_Name Employee_DOB SupervisorID (FK) UWEC_STUDENT_ROOMS_WITH
20
Ternary Relationships Create a 3-way linking table Student_ID (FK) Advisor_ID (FK) Program_ID (FK) UWEC_STUDENT_ ADVISOR_PROGRAM
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 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 Supertype Subtypes
23
Generalization/Specialization Data Table Example Person_ IDPerson_FirstPerson_LastPerson_MIPerson_DOB 100JolineMorrisonP7/14/1958 101JudithMuellerR.8/12/1967 102JoshuaBatesJ.2/15/1990 UWEC_PERSON Person_IDStudent_MajorStudent_Minor 102Graphic Design Web Development UWEC_STUDENT Person_ID Instructor_ Title Instructor_ Office Instructor_ Salary 100ProfessorP 13855000 UWEC_INSTRUCTOR Person_IDStaff_Rank Staff_Hourly_ Rate 101Sr. Assoc. 21.55 UWEC_STAFF
24
Test Yourself: In the CANDY_PURCHASE table, PURCH_ID is an example of a: a. A primary key b. A foreign key c. Part of a composite key a. Both b and c b. None of the above CANDY_PURCHASE
25
Test Yourself: In the CANDY_PURCHASE table, PURCH_ID is an example of: a. A primary key b. A foreign key c. Part of a composite key a. Both b and c b. None of the above CANDY_PURCHASE
26
Test Yourself: In the CANDY_PURCHASE table, PROD_ID is an example of a: a. Primary key b. Foreign key c. Part of a composite key d. Both b and c e. None of the above CANDY_PURCHASE
27
Test Yourself: In the CANDY_PURCHASE table, PROD_ID is an example of a: a. Primary key b. Foreign key c. Part of a composite key d. Both b and c e. None of the above CANDY_PURCHASE
28
Test Yourself: How many database tables would you create for the following ER model? a. 3 b. 4 c. 5 d. None of the above
29
Test Yourself: How many database tables would you create for the following ER model? a. 3 b. 4 c. 5 d. 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))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.