Lesson 5.3. Enforcing Referential Integrity
Figure 5-7. The Edit Relationships dialog box.
Figure 5-8. The . The relationship line between the tblCustomers table and the tblCustomerTours table indicates a one-to-many relationship.
When you create a relationship between two tables, it is usually a good idea to enforce . So what does this technical-sounding phrase mean? Referential integrity keeps records in related fields valid and accurate. Referential integrity ensures that you don't accidentally change or delete related data in one table but not in the other. For example, say you were using two related Social Security fields to link two tables. Referential integrity would not allow you to change the Social Security number in one record without changing the Social Security number in the other related records.
Access is very picky about when you can set . You can only use referential integrity when all of the following conditions are met:
One of the linked fields is a primary key
The related fields are the same data type and size. (If you are using an AutoNumber field, you can relate it to a Number field with a Long Integer Field size.)
Both tables are in the same Access database.
You can't have a record in a related table unless a matching record already exists in the primary table. Orphan data in a related table is the most common problem people encounter when attempting to establish referential integrity.
Once you have established referential integrity, the following rules are set:
You can't add a record to a related table unless a matching record already exists in the primary table.
You can't change the value of a primary key in the primary table if matching records exist in the related table (unless you select the ).
You can't delete a record from a primary table if matching records exist in a related table (unless you select the ).
In this lesson you will learn how to enforce referential integrity. You should still have the open from the previous lesson in order to complete this lesson.
First, let's establish a relationship between the tblCustomers table and the tblCustomerTours table...
Drag the CustomerID field from the tblCustomers table to the CustomerID field in the tblCustomerTours table.
The Edit Relationships dialog box appears, as shown in Figure 5-7. We want to enforce referential integrity so that you can't enter a CustomerID in the tblCustomerTours table unless that CustomerID number already exists in the tblCustomers table.
Check the Enforce Referential Integrity box.
This will enforce referential integrity between the tables. If you get an error message, it's because your tables and fields don't meet all the required conditions listed on the previous page.
There are two other very important boxes in the Edit Relationships dialog box:
Cascade Update Related Fields: When you change data in the main field of one table, Access will automatically update the matching data in the related table.
Cascade Delete Related Records: When you delete a record in the main table, Access will automatically delete any matching records in the related table.
These are both very powerful optionsthink twice before using them.
Check both the Cascade Update Related Fields box and the Cascade Delete Related Records box.
Now you can save the changes to the relationship.
Click Create to create the relationship between the tblCustomers and tblCustomerTours tables.
Access creates the relationship between the two tables and enforces between them. Notice that the join line between the tblCustomers table and the tblCustomerTours table looks a little different. This relationship indicates that referential integrity is being enforced between the two tables and that the tables have a one-to-many relationship, as shown in Figure 5-8 (more about that later).
Click the Close button to close the Relationships window and click Yes to save the changes you made.
In the next lesson you will get to test the results of your new referential integrity settings.
TO ENFORCE REFERENTIAL INTEGRITY:
IN THE DATABASE WINDOW, CLICK THE RELATIONSHIPS BUTTON ON THE TOOLBAR.
RIGHT-CLICK ANY BLANK AREA IN THE DATABASE WINDOW AND SELECT RELATIONSHIPS
DOUBLE-CLICK THE JOIN LINE FOR THE RELATIONSHIP YOU WANT TO WORK WITH.
CHECK THE ENFORCE REFERENTIAL INTEGRITY BOX.
IF YOU WANT CHANGES TO THE PRIMARY FIELD OF THE PRIMARY TABLE COPIED TO THE RELATED FIELD IN THE RELATED TABLE, CHECK THE CASCADE UPDATE RELATED FIELDS BOX.
IF YOU WANT ACCESS TO AUTOMATICALLY DELETE ORPHAN RECORDS IN THE RELATED TABLE, CHECK THE CASCADE DELETE RELATED RECORDS BOX.