Table Relationships
-
Upload
teegan-macias -
Category
Documents
-
view
14 -
download
1
description
Transcript of Table Relationships
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 RelationshipsOne-to-One
Student ID
First Name
Last Name
A111
A112
A113
A114
A115
A116
Student ID
Social Security Number
Phone number
A111
A112
A113
A114
A115
A116
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 RelationshipsOne-to-Many
Student ID
First Name
Last Name
A111
A112
A113
A114
A115
A116
Student ID Class …
A111 FW893
A111 FW100
A111 FW205
A113 FW893
A113 FW205
A114 FW999
Table RelationshipsOne-to-Many Example-Many fish are caught at each site
Site Table Individual Fish TableStream Name (CPK) Stream Name (CPK/FK)Position (CPK) Position (CPK/FK)Site (CPK) Site (CPK/FK)Year (CPK) Year (CPK/FK)Month (CPK) Month (CPK/FK)Day (CPK) Day (CPK/FK)Width Fish_ID (CPK)Depth Species CodeTemperature LengthConductivity Age
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 Class TableStudent_ID (PK) Class_ID (PK)First Name Class NameLast Name Class DescriptionStreet Instructor_IDCity Max EnrollmentStateZipcodePhone
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 RelationshipsEnrollment
Student Table Table Class TableStudent_ID (PK) Student_ID (CPK/FK) Class_ID (PK)First Name Class_ID (CPK/FK) Class NameLast Name Class DescriptionStreet Instructor_IDCity Max EnrollmentState Enrollment (???)ZipcodePhone
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 RelationshipsRectifying Many-to-Many
Student ID
First Name
Last Name
A111
A112
A113
A114
A115
A116
Student ID
Class
A111 FW893
A111 FW100
A111 FW205
A113 FW893
A113 FW205
A114 FW999
Class Course Title
Instructor
FW100 Intro… Habron
FW205 Principals…
Riley
FW325 Ecosys… Sarnelle
FW893 Seminar.. Hayes
FW899 MS res… ????
FW999 PhD res ????
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 Au Gres
2 Rifle River
3 Miners Creek
4 Albany Creek
Stream ID Sample ID Date
1 1 10-10-2004
1 2 10-11-2004
1 3 10-12-2004
3 4 10-15-2004
3 5 10-16-2004
3 6 10-17-2004
Join Types Illustration Option 1- both equal
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
3 Miners Creek 4 10-15-2004
3 Miners Creek 5 10-16-2004
3 Miners Creek 6 10-17-2004
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