Table Relationships • Establishing table relationships is the way we draw together data from separate tables. This minimizes redundant data and maintains higher levels of database reliability. • Types of relationships – One-to-One – One-to-Many – Many-to-Many Homework Reminder • First step was to develop list of tables and fields and have a partner look it over – As part of your database project, I will be looking for a minimum of three tables – As you develop and refine the list of tables and fields, it is useful to have copies of the intermediate steps with some annotation describing why you did particular refinements. This will help you in the project to describe why you made particular choices • Second step is to create your tables in access. I won’t be collecting this as homework, but this is part of the process of developing your database project Table Relationships One-to-One Student ID First Name Last Name Student ID A111 A111 A112 A112 A113 A113 A114 A114 A115 A115 A116 A116 Social Security Number Phone number Table Relationships • One-to-one relationships are relatively rare in most database structures. Further, one-to-one relationships can be thought of as a specialized case of one-to-many. • The most common type of “true” one-to-one relationship is when one table is a SUBSET TABLE of another. Example- faculty are a subset of all university staff members. Graduate students are also a subset of all university staff. Thus, one way of implementing a personnel database would be to have a staff table (with information common to all staff members such as social security number), and separate faculty and graduate student tables to hold data that applies only to each of these specialized type of staff members. • When you have a one-to-one relationship, the linkage is achieved by having the same primary key in both tables (e.g., social security number in the staff table, and in the faculty or graduate student table). Table Relationships One-to-Many Student ID First Name Last Name Student ID Class A111 A111 FW893 A112 A111 FW100 A113 A111 FW205 A114 A113 FW893 A115 A113 FW205 A116 A114 FW999 … Table Relationships One-to-Many Example-Many fish are caught at each site Site Table Stream Name Position Site Year Month Day Width Depth Temperature Conductivity Individual Fish Table Stream Name (CPK/FK) Position (CPK/FK) Site (CPK/FK) Year (CPK/FK) Month (CPK/FK) Day (CPK/FK) Fish_ID (CPK) Species Code Length Age (CPK) (CPK) (CPK) (CPK) (CPK) (CPK) Linkage is established by taking primary key (PK, or composite primary key CPK) from “One” side of relationship, and inserting that into the “Many” side of the relationship. This now becomes what is known as a “FOREIGN KEY”. Many-to-Many Relationships Student Table Student_ID (PK) First Name Last Name Street City State Zipcode Phone Class Table Class_ID (PK) Class Name Class Description Instructor_ID Max Enrollment Problem here is how to link Students with classes. Each student can take multiple classes, and each class has multiple students. Rectifying Many-to-Many Relationships Student Table Student_ID (PK) First Name Last Name Street City State Zipcode Phone Enrollment Table Student_ID (CPK/FK) Class_ID (CPK/FK) Class Table Class_ID (PK) Class Name Class Description Instructor_ID Max Enrollment Enrollment (???) Notice that the Enrollment Table serves to link the Student Table and the Class Table. As such, it has both of the primary keys from each of these tables as foreign keys. The relationship between Student Table and Enrollment Table is One-to-Many (because each student can take many classes, but there is only one Student_ID, Class_ID combination in the Student-Class Table for each student Class Table and Enrollment Table is One to Many because each student can be enrolled in a class only once Table Relationships Rectifying Many-to-Many Student ID Class Class Course Title Instructor A111 A111 FW893 FW100 Intro… Habron A112 A111 FW100 FW205 Principals … Riley A113 A111 FW205 FW325 Ecosys… Sarnelle A114 A113 FW893 FW893 Seminar.. Hayes A115 A113 FW205 FW899 MS res… ???? A116 A114 FW999 FW999 PhD res ???? Stude nt ID First Name Last Name Creating Relationships in Access • Open Relationship Window (has three little boxes connected with lines) • Go to menu and select Relationships, then Show Table • Select Tables to add (usually all tables in database), then close Show Table • It’s handy to stretch each table display so you can see all the fields Creating Relationships in Access • Usually I start with “biggest” tables meaning the table with the largest unit of observation. In this example, it is the stream list and the sample list tables • To create relationship between two tables using a single key, simply click on field and drag to same field in connected table Creating Relationships in Access • Note that in this example, Access “knows” that the relationship is one to many • Can click check box to Enforce Referential Integrity – This insures that a record must exist in the “One” table before a corresponding record in the “Many” table can be entered – “Cascade” options help enforce referential integrity Creating Relationships in Access • Cascade options – “Cascade Update Related Fields” automatically changes the primary key field values in the “Many” table if you change the primary key field values in the “One” table (show example) – “Cascade Delete Related Records” automatically deletes related records in the “Many” table if you delete a record in the “One” table Creating Relationships in Access • Select “Join Type” – Note that this is a critical decision and depends on your “business rules”!!! – Option 1 includes only records where both are equal EQUIJOIN – Option 2 includes only records where “One” side exists – Option 3 includes only records where “Many” side exists Join Types Illustration Stream ID Stream Name 1 … Stream ID Sample ID Date Au Gres 1 1 10-102004 2 Rifle River 1 2 10-112004 3 Miners Creek 1 3 10-122004 4 Albany Creek 3 4 10-152004 3 5 10-162004 3 6 10-172004 Join Types Illustration Option 1- both equal Stream ID Stream Name Sample ID Date 1 Au Gres 1 10-102004 1 Au Gres 2 10-112004 1 Au Gres 3 10-122004 3 Miners Creek 4 10-152004 3 Miners Creek 5 10-162004 3 Miners Creek 6 10-172004 Join Types Illustration Option 2- All Records from Primary Table Stream ID Stream Name Sample ID Date 1 Au Gres 1 10-10-2004 1 Au Gres 2 10-11-2004 1 Au Gres 3 10-12-2004 2 Rifle River 3 Miners Creek 4 10-15-2004 3 Miners Creek 5 10-16-2004 3 Miners Creek 6 10-17-2004 4 Albany Creek Join Types Illustration Option 3- Doesn’t make sense when enforce referential integrity because can’t have a record in the “Many” table without a corresponding record in “One” table