Microsoft Access 2003 Relationships Email: training@health.ufl.edu Web site: http://training.health.ufl.edu Microsoft Access: Relationships 2.0 hours This workshop requires completion of "Access: Basics" and "Access: Tables" highly recommended. Topics include one-to-one and one-to-many relationships; junction tables; primary and foreign keys; cascade update and cascade delete; and join properties. Topics: - Relationship Presentation - Relational Databases - Design Rules - Primary Keys - Enforcing referential integrity - One to One - One to Many Many to Many (indeterminate) Cascade Update Cascade Delete Relationship Issues Worksheets: Relationships ............................................................................................................................. 1 One to Many ......................................................................................................................... 1 One to One ............................................................................................................................ 1 Many to Many ....................................................................................................................... 2 Edit Relationships ..................................................................................................................... 3 Enforce Referential Integrity ................................................................................................ 3 Cascade Update Related Records ......................................................................................... 4 About Relationships in Databases (Access Help) ..................................................................... 6 How do relationships work? ................................................................................................. 6 A One-to-Many Relationship ................................................................................................ 7 A Many-to-Many Relationship ............................................................................................. 7 A one-to-one relationship ..................................................................................................... 8 Defining relationships ........................................................................................................... 8 Pandora Rose Cowart Training Specialist Information Technology Center Health Science Center PO Box 100152 Gainesville, FL 32610-0152 352-273-5051 prcowart@ufl.edu http://training.health.ufl.edu Updated: 1/4/06 Page 1 Relationships You can view the relationships of your database by clicking on the Relationships ( ) button while looking at the database window, or by choosing Relationships... from the Tools menu. One to Many The most common relationship you will create will be a one-to-many relationship. This will reflect a link between a Primary Key field and a non Primary Key field. Primary Keys are discussed in the PowerPoint presentation. Example: One person can visit many times. (Jack Johnson visited on 1/23/03, 1/31/03, and 3/10/03.) Person ID P1 P2 P3 First Name Jack Julie Jeffery Person ID First Name Last Name Address City Zip Home Phone Work Phone 1 Last Name Johnson Jacobs Jackson Address PO Box 123 PO Box 456 PO Box 789 Person ID Visit ID Visit Date Comments Person ID P1 P2 P1 P1 P3 VisitID V1 V2 V3 V4 V5 VisitDate 1/23/03 1/23/03 1/31/03 3/10/03 2/15/03 Comments First Contact First Contact Gave Card Likes hot tea Spouse Joan Each visit has its own unique comments, so one unique person, has many unique visits. One to One The other relationship you can create in Access is a one-to-one relationship. This will reflect a link between a Primary Key field and another Primary Key field. Example: One person can have one spouse. (Jack Johnson is married to Mary Johnson.) Person ID P1 P2 P3 First Name Jack Julie Jeffery Person ID First Name Last Name Address City Zip Home Phone Work Phone 1 Last Name Johnson Jacobs Jackson 1 Address PO Box 123 PO Box 456 PO Box 789 PersonID Spouse First Spouse Last Person ID P1 P3 Spouse First Mary Mark Spouse Last Johnson Jacobs One-to-one relationships can be merged into one table. When the tables become too large, or the data you want to track does not apply to a majority of your records, you may wish to pull that data out into its own table, as with this example. Updated: 1/4/06 Page 2 Many to Many Often you will find that you have a many-to-many relationship. Example: One student can have many tutors (Jack Johnson is tutored by Mary McGuire, Mark Madden, and Misty Martin) Student ID S1 S2 S3 S4 First Name Jack Julie Jeffery John Last Name Johnson Jacobs Jackson Jenson Address PO Box 123 PO Box 456 PO Box 789 PO Box 021 Tutor ID T1 T2 T3 T4 T5 First Name Mary Matt Misty Melanie Mark Last Name McGuire Madden Martin Masters Madden Office# 3-045 3-041 3-048 3-053 3-054 Last Name McGuire Madden Martin Masters Madden Office# 3-045 3-041 3-048 3-053 3-054 but each tutor can support many students (Mary McGuire tutors Jack Johnson, Julie Jacobs, and John Jenson) Student ID S1 S2 S3 S4 First Name Jack Julie Jeffery John Last Name Johnson Jacobs Jackson Jenson Address PO Box 123 PO Box 456 PO Box 789 PO Box 021 This kind of relationship is too complicated. It requires the intervention of a junction table to keep track of individual occurrences. The session table pulls the students and the tutors into unique records by keeping track of the date and time of each session. 1 Student, Many Tutors Jack Johnson Mary, 2/14, 2pm Mark, 2/14, 5pm Misty, 2/15, 5pm 1 Tutor, Many Students Mary McGuire Jack, 2/14, 2pm Julie, 2/14, 4pm John, 2/14, 6pm Tutor ID T1 T2 T3 T4 T5 Student ID First Name Last Name Address City Student ID First Name Last Name Address City First Name Jack Julie Jeffery John Updated: 01/04/06 Stud ID S1 S2 S1 S4 S1 Sess ID 1 2 3 4 5 Date/Time 2/14, 2pm 2/14, 4pm 2/14, 5pm 2/14, 6pm 2/15, 5pm TutorID First Name Last Name Office# 1 1 Student ID S1 S2 S3 S4 First Name Mary Matt Misty Melanie Mark Session ID Student ID Tutor ID Date/Time Tutor ID T1 T1 T5 T1 T3 TutorID First Name Last Name Office# Tutor ID T1 T2 T3 T4 T5 First Name Mary Matt Misty Melanie Mark Page 3 Edit Relationships You will see this Edit Relationships window when you create the relationship, by dragging a field from one table to the field of another table or when you double-click on an existing relationship. The relationship type will depend on the status of the original fields. - If you drag a primary key field to a non-primary key field, you will see One-To-Many. - If you drag a primary key field to another primary key field, you will get a One-To-One relationship. - If you drag a non-primary key field to another non-primary key field you will see Indeterminate. You cannot Enforce Referential integrity with an indeterminate relationship type. Enforce Referential Integrity Access Help – "Referential integrity is a system of rules that Microsoft Access uses to ensure that relationships between records in related tables are valid and that you don't accidentally delete or change related data. Select this check box to enforce referential integrity for this relationship only if all the following apply: - The matching field from the primary table is a primary key or has a unique index - The related fields have the same data type - and Both tables are stored in the same Access database Clear this check box to allow changes to related tables that would break referential integrity rules." Enforcing referential integrity applies a set of rules to your tables that will help make your tables have the correct information. This ensures you will not be able to sell anything to Mr. Smith, unless he exists in your customer table. Once you have enforced the referential integrity, you have two new choices, Cascade Update Related Records and Cascade Delete Related Records. Updated: 1/4/06 Page 4 Cascade Update Related Fields Once you enforce referential integrity, Access won’t allow you to change the linked field in either table. If you check the “Cascade Update Related Records”, you will be allowed to change the data in the main table (the table where the field is a primary key). The only field that should be in both tables is the Primary Key from the main table. So the Cascade Update we are talking about is effecting the Primary Key fields only. Example: We need to change Johnson to Johnston. One-To-Many relationship, with enforced referential integrity but no cascade update. VISIT TABLE Last Name Johnson Jacobs Johnson Johnson Jackson VisitID V1 V2 V3 V4 V5 Person Table VisitDate 1/23/03 1/23/03 1/31/03 3/10/03 2/15/03 Comments First Contact First Contact Gave Card Likes hot tea Spouse Joan First Name Jack Julie Jeffery Last Name Johnson Jacobs Jackson Address PO Box 123 PO Box 456 PO Box 789 If you try to change Johnson to Johnston in the Visit table access will say: If you try to change Johnson to Johnston in the Person table Access will say: Solution: Create a new record in the Person table for Mr. Johnston, then change all of the Johnsons to Johnston in the Visit table, and then remove the Johnson record from the Person table. Or turn on the Cascade Update Related Fields. If you try to change Johnson to Johnston in the Visits table access will give you the same error as above, however if you try to change Johnson to Johnston in the Person table Access will let you change the data, no questions asked, and it will automatically change all the Johnsons to Johnstons in the Visits table. Updated: 01/04/06 Page 5 Cascade Delete Related Records: Once you enforce referential integrity, Access won’t allow you to delete the primary key field in the main table. If you check the “Cascade Delete Related Records”, you will be allowed to delete the data in the main table, and it will in turn, cascade delete the related records. Example: We need to Delete Johnson One-To-Many relationship, with enforced referential integrity but no cascade delete. VISITS PEOPLE Last Name Johnson Jacobs Johnson Johnson Jackson VisitID V1 V2 V3 V4 V5 VisitDate 1/23/03 1/23/03 1/31/03 3/10/03 2/15/03 Comments First Contact First Contact Gave Card Likes hot tea Spouse Joan First Name Jack Julie Jeffery Last Name Johnson Jacobs Jackson Address PO Box 123 PO Box 456 PO Box 789 You can delete the records in the Visits table with no problem. There doesn’t have to be matching records in the Visits table for each person in the People table. However, if you try to delete Johnson from the People table, you will get this message: If you turn on the Cascade Delete Related Records, you will be able to delete Johnson from the People table, but it will delete all the related records in the Visits table. VISITS Last Name Johnson Jacobs Johnson Johnson Jackson PEOPLE VisitID V1 V2 V3 V4 V5 VisitDate 1/23/03 1/23/03 1/31/03 3/10/03 2/15/03 Comments First Contact First Contact Gave Card Likes hot tea Spouse Joan First Name Jack Julie Jeffery Last Name Johnson Jacobs Jackson Address PO Box 123 PO Box 456 PO Box 789 Updated: 1/4/06 Page 6 About Relationships in Databases (Access Help) After you've set up different tables for each subject in your database, you need a way of telling Microsoft Access how to bring that information back together again. The first step in this process is to define relationships between your tables. After you've done that, you can create queries, forms, and reports to display information from several tables at once. For example, this form includes information from five tables: How do relationships work? In the previous example, the fields in five tables must be coordinated so that they show information about the same order. This coordination is accomplished with relationships between tables. A relationship works by matching data in key fields, usually a field with the same name in both tables. In most cases, these matching fields are the primary key from one table, which provides a unique identifier for each record, and a foreign key in the other table. For example, employees can be associated with orders they're responsible for by creating a relationship between the Employees table and the Orders table using the EmployeeID fields. Updated: 01/04/06 Page 7 DEFINITIONS Relationship: An association established between common fields (columns) in two tables. A relationship can be one-to-one, one-to-many, or many-to many. Primary Key: One or more fields whose value or values uniquely identify each record in a table. In a relationship, a primary key is used to refer to specific records in one table from another table. A primary key is called a foreign key when it is referred to from another table. Foreign Key: One or more table fields to the primary key field or fields in another table. A foreign key indicates how the tables are related – the data in the foreign key and primary key fields must match. A One-to-Many Relationship A one-to-many relationship is the most common type of relationship. In a oneto-many relationship, a record in Table A can have many matching records in Table B, but a record in Table B has only one matching record in Table A. A Many-to-Many Relationship In a many-to-many relationship, a record in Table A can have many matching records in Table B, and a record in Table B can have many matching records in Table A. This type of relationship is only possible by defining a third table (called a junction table) whose primary key consists of two fields, the foreign keys from both Tables A and B. A many-tomany relationship is really two one-to-many relationships with a third table. For example, the Orders table and the Products table have a many-tomany relationship that's defined by creating two one-to-many relationships to the Order Details table. Updated: 1/4/06 Page 8 A one-to-one relationship In a one-to-one relationship, each record in Table A can have only one matching record in Table B, and each record in Table B can have only one matching record in Table A. This type of relationship is not common, because most information related in this way would be in one table. You might use a one-to-one relationship to divide a table with many fields, to isolate part of a table for security reasons, or to store information that applies only to a subset of the main table. For example, you might want to create a table to track employees participating in a fundraising soccer game. Defining relationships You define a relationship by adding the tables you want to relate to the Relationships window, and then dragging the key field from one table and dropping it on the key field in the other table. The kind of relationship that Microsoft Access creates depends on how the related fields are defined: A one-to-many relationship is created if only one of the related fields is a primary key or has a unique index. A one-to-one relationship is created if both of the related fields are primary keys or have unique indexes. A many-to-many relationship is really two one-tomany relationships with a third table whose primary key consists of two fields – the foreign keys from the two other tables. Note If you drag a field that isn't a primary key and doesn't have a unique index to another field that isn't a primary key and doesn't have a unique index, an indeterminate relationship is created. In queries containing tables with an indeterminate relationship, Microsoft Access displays a default join line between the tables, but referential integrity won't be enforced, and there's no guarantee that records are unique in either table. Updated: 01/04/06 Page 9 Updated: 1/4/06 Page 10 Relationship Error Messages There are four reasons you will not be able to enforce referential integrity on your relationships. (1) No primary key set. (2) Conflicting Data Types You are linking a text field to a number field. Or date to text, or possibly even a integer value to a double value. You need to make sure the data in both fields is the same type. (If linking AutoNumber to a Number, then the Number should be a LONG INTEGER). (3) You have one of the tables open. May possibly be because you also have a query, form or report, based on one of the tables, open. Cancel the relationship window, close the object, and try again. (4) Mismatched data You have created data in a table (most likely the "MANY" table) that doesn't exist in the other. You need to either place the data in both tables, or remove the offending data. If needed, you can run a FIND MISMATCHED query to help find the data that isn't matching. Updated: 01/04/06