database design 4: deriving tables from data models cs 320
Post on 18-Jan-2018
225 Views
Preview:
DESCRIPTION
TRANSCRIPT
Database Design 4: Deriving Tables From Data Models
CS 320
Review: Relationships in TablesCUST_ID CUST_NAME CUST_TYPE CUST_ADDR CUST_ZIP CUST_PHONE USERNAME PASSWORD
1 Jones, Joe P 1234 Main St. 91212 434-1231 jonesj 12342 Armstrong,Inc. R 231 Globe Blvd. 91212 434-7664 armstrong 33333 Sw edish Burgers R 1889 20th N.E. 91213 434-9090 sw edburg 23534 Pickled Pickles R 194 CityView 91289 324-8909 pickpick 53335 The Candy Kid W 2121 Main St. 91212 563-4545 kidcandy 23516 Waterman, Al P 23 Yankee Blvd. 91234 w ateral 89007 Bobby Bon Bons R 12 Nichi Cres. 91212 434-9045 bobbybon 30118 Crow sh, Elias P 7 77th Ave. 91211 434-0007 crow el 10339 Montag, Susie P 981 Montview 91213 456-2091 montags 9633
10 Columberg Sw eets W 239 East Falls 91209 874-9092 columsw e 8399
PURCH_ID PROD_ID CUST_ID PURCH_DATE DELIVERY_DATE POUNDS STATUS1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID
PROD_ID PROD_DESC PROD_COSTPROD_PRICE1 Celestial Cashew Crunch 7.45$ 10.00$ 2 Unbrittle Peanut Paradise 5.75$ 9.00$ 3 Mystery Melange 7.75$ 10.50$ 4 Millionaire’s Macadamia Mix 12.50$ 16.00$ 5 Nuts Not Nachos 6.25$ 9.50$
Created byshared key fields
Types of Database Key Fields
Primary keys Foreign keys Composite keys
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
PROD_ID PROD_DESC PROD_COSTPROD_PRICE1 Celestial Cashew Crunch 7.45$ 10.00$ 2 Unbrittle Peanut Paradise 5.75$ 9.00$ 3 Mystery Melange 7.75$ 10.50$ 4 Millionaire’s Macadamia Mix 12.50$ 16.00$ 5 Nuts Not Nachos 6.25$ 9.50$
CUST_ID CUST_NAME CUST_TYPE CUST_ADDR CUST_ZIP CUST_PHONE USERNAME PASSWORD1 Jones, Joe P 1234 Main St. 91212 434-1231 jonesj 12342 Armstrong,Inc. R 231 Globe Blvd. 91212 434-7664 armstrong 33333 Sw edish Burgers R 1889 20th N.E. 91213 434-9090 sw edburg 23534 Pickled Pickles R 194 CityView 91289 324-8909 pickpick 53335 The Candy Kid W 2121 Main St. 91212 563-4545 kidcandy 23516 Waterman, Al P 23 Yankee Blvd. 91234 w ateral 89007 Bobby Bon Bons R 12 Nichi Cres. 91212 434-9045 bobbybon 30118 Crow sh, Elias P 7 77th Ave. 91211 434-0007 crow el 10339 Montag, Susie P 981 Montview 91213 456-2091 montags 9633
10 Columberg Sw eets W 239 East Falls 91209 874-9092 columsw e 8399
CUST_ID CUST_NAME CUST_TYPE CUST_ADDR CUST_ZIP CUST_PHONE USERNAME PASSWORD1 Jones, Joe P 1234 Main St. 91212 434-1231 jonesj 12342 Armstrong,Inc. R 231 Globe Blvd. 91212 434-7664 armstrong 33333 Sw edish Burgers R 1889 20th N.E. 91213 434-9090 sw edburg 23534 Pickled Pickles R 194 CityView 91289 324-8909 pickpick 53335 The Candy Kid W 2121 Main St. 91212 563-4545 kidcandy 23516 Waterman, Al P 23 Yankee Blvd. 91234 w ateral 89007 Bobby Bon Bons R 12 Nichi Cres. 91212 434-9045 bobbybon 30118 Crow sh, Elias P 7 77th Ave. 91211 434-0007 crow el 10339 Montag, Susie P 981 Montview 91213 456-2091 montags 9633
10 Columberg Sw eets W 239 East Falls 91209 874-9092 columsw e 8399
Foreign Key Field that is a primary key in another table Used to create a relationship
PROD_ID PROD_DESC PROD_COSTPROD_PRICE1 Celestial Cashew Crunch 7.45$ 10.00$ 2 Unbrittle Peanut Paradise 5.75$ 9.00$ 3 Mystery Melange 7.75$ 10.50$ 4 Millionaire’s Macadamia Mix 12.50$ 16.00$ 5 Nuts Not Nachos 6.25$ 9.50$
PURCH_ID PROD_ID CUST_ID PURCH_DATE DELIVERY_DATE POUNDS STATUS1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID
Composite Key Primary key comprised of 2 or more fields
PURCH_ID PROD_ID CUST_ID PURCH_DATE DELIVERY_DATE POUNDS STATUS1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID
Review: Levels of data models1. Conceptual: describes WHAT data the
system contains2. Logical: describes HOW the database will
be structured, regardless of the DBMS3. Physical: describes HOW the system will be
implemented using a specific DBMS
Goal of Logical DB Design: Database Integrity
Entity integrityEvery record must have a unique primary keyThe primary key value cannot be NULL
Referential integrityEvery foreign key must be the primary key of
its parent tableEvery foreign key value must exist in its
parent table
Examples of Integrity ProblemsCUST_ID CUST_NAME CUST_TYPE CUST_ADDR CUST_ZIP CUST_PHONE USERNAME PASSWORD
1 Jones, Joe P 1234 Main St. 91212 434-1231 jonesj 12342 Armstrong,Inc. R 231 Globe Blvd. 91212 434-7664 armstrong 33333 Sw edish Burgers R 1889 20th N.E. 91213 434-9090 sw edburg 23534 Pickled Pickles R 194 CityView 91289 324-8909 pickpick 53335 The Candy Kid W 2121 Main St. 91212 563-4545 kidcandy 23516 Waterman, Al P 23 Yankee Blvd. 91234 w ateral 8900
Bobby Bon Bons R 12 Nichi Cres. 91212 434-9045 bobbybon 3011Crow sh, Elias P 7 77th Ave. 91211 434-0007 crow el 1033
9 Montag, Susie P 981 Montview 91213 456-2091 montags 963310 Columberg Sw eets W 239 East Falls 91209 874-9092 columsw e 8399
PURCH_ID PROD_ID CUST_ID PURCH_DATE DELIVERY_DATE POUNDS STATUS1 100 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID
PROD_ID PROD_DESC PROD_COSTPROD_PRICE1 Celestial Cashew Crunch 7.45$ 10.00$ 2 Unbrittle Peanut Paradise 5.75$ 9.00$ 3 Mystery Melange 7.75$ 10.50$ 4 Millionaire’s Macadamia Mix 12.50$ 16.00$ 5 Nuts Not Nachos 6.25$ 9.50$
?
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
Representing 1:M Relationships Put the PK of the “1” side into the “M” table
Dept_ID
Dept_Name Dept_Office_Loc
1 Accounting SSS415
2 IT SSS406
3 Production SSS312
Emp_ID
Emp_Last_Name
Emp_Office
Dept_ID (FK)
100 Smith SSS112 1
200 Jones SSS116 1
300 Brown SSS107 2
ACME_DEPARTMENT(Dept_ID, Dept_Name, Dept_Office_Loc)
ACME_EMPLOYEE(Emp_ID, Emp_Last_Name, Emp_Office, Dept_ID)
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
1 Accounting SSS415 100, 200
2 MIS SSS406 300
3 Production SSS312
Dept_ID
Dept_Name
Dept_Office
Emp_ID
1 Accounting SSS415 100
1 Accounting SSS415 200
2 MIS SSS406 300
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!
Representing M:M Relationships
Create a linking tableAlso 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
Representing M:M Relationships
Project_ID
Project_Name Project_Start_Date
1 Help Desk Intranet 01/05/12
2 Cold Fusion Study 01/15/12
3 Oracle Conversion 11/15/11
Emp_ID
Emp_Last_Name
Emp_Office
100 Smith SSS112
200 Jones SSS116
300 Brown SSS107
Project_ID (FK) Emp_ID (FK)
1 100
2 100
2 200
Project_ID (FK) Emp_ID (FK)
1 100
2 100
2 200
What do you name the linking table? Typically, a compound word comprised of
the two participating entities
ACME_EMPLOYEE_PROJECT
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
1 100 30
2 100 15
2 200 25
Deriving Tables for 1:1 Relationships
In general, move one entity's non-key attributes into the other entity's table
ACME_EMPLOYEEEmp_IDEmp_Last_NameEmp_OfficeEmp_Spouse_FirstEmp_Spouse_Last
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
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_EMPLOYEEEmployee_IDEmployee_First_NameEmployee_Last_NameEmployee_DOBSupervisorID (FK)
UWEC_STUDENT_ROOMS_WITH
Ternary Relationships Create a 3-way linking table
Student_ID (FK)Advisor_ID (FK)Program_ID (FK)
UWEC_STUDENT_ADVISOR_PROGRAM
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
Generalization/SpecializationUWEC_PersonPerson_IDPerson_First_NamePerson_Last_NamePerson_MIPerson_DOB
UWEC_STUDENTPerson_ID (FK)Student_Major_ProgramStudent_Minor_Program
UWEC_INSTRUCTORPerson_ID (FK)Instructor_TitleInstructor_OfficeInstructor_Salary
UWEC_StaffPerson_ID (FK)Staff_RankStaff_Hourly_Rate
Supertype
Subtypes
Generalization/Specialization Data Table Example
Person_ID Person_First Person_Last Person_MI Person_DOB
100Joline Morrison P 7/14/1958
101Judith Mueller R. 8/12/1967
102Joshua Bates J. 2/15/1990
UWEC_PERSONPerson_ID Student_Major Student_Minor
102Graphic DesignWeb Development
UWEC_STUDENT
Person_IDInstructor_Title
Instructor_Office
Instructor_Salary
100Professor P 138 55000
UWEC_INSTRUCTOR
Person_ID Staff_RankStaff_Hourly_Rate
101Sr. Assoc. 21.55
UWEC_STAFF
Test Yourself: In the CANDY_PURCHASE table, PURCH_ID is an example of a:
PURCH_ID PROD_ID CUST_ID PURCH_DATE DELIVERY_DATE POUNDS STATUS1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID
a. A primary keyb. A foreign keyc. Part of a composite keya. Both b and cb. None of the above
CANDY_PURCHASE
Test Yourself: In the CANDY_PURCHASE table, PURCH_ID is an example of:
PURCH_ID PROD_ID CUST_ID PURCH_DATE DELIVERY_DATE POUNDS STATUS1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID
a. A primary keyb. A foreign keyc. Part of a composite keya. Both b and cb. None of the above
CANDY_PURCHASE
Test Yourself: In the CANDY_PURCHASE table, PROD_ID is an example of a:
a. Primary keyb. Foreign keyc. Part of a composite keyd. Both b and ce. None of the above
PURCH_ID PROD_ID CUST_ID PURCH_DATE DELIVERY_DATE POUNDS STATUS1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID
CANDY_PURCHASE
Test Yourself: In the CANDY_PURCHASE table, PROD_ID is an example of a:
a. Primary keyb. Foreign keyc. Part of a composite keyd. Both b and ce. None of the above
PURCH_ID PROD_ID CUST_ID PURCH_DATE DELIVERY_DATE POUNDS STATUS1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID
CANDY_PURCHASE
Test Yourself:How many database tables would you create for the following ER model?
a. 3b. 4c. 5d. None of the above
Test Yourself:How many database tables would you create for the following ER model?
a. 3b. 4c. 5d. None of the above
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).
Your Turn #1 SolutionALS_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))
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).
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))
top related