In this lesson you will get to test the referential integrity rules you established in the previous lesson. You just went over it, but since this referential integrity stuff is a little tricky and confusing at first, let's review...
Think you have a better understanding of referential integrity yet? No? This lesson will give you a chance to work with some related tables where referential integrity has been set. You will also learn how to delete a relationship between two tables.
Open the tblCustomers tablein Datasheet View.
First let's try deleting a record in the tblCustomers table.
Click on the record selector for the record with the CustomerID 1 (Antonio Rommero) and click the Delete Record button on the toolbar.
Because you enabled both referential integrity and cascaded deletes in the previous lesson, Access displays the dialog box shown in Figure 5-9. Access wants you to confirm your deletion, as deleting the customer record will delete any related data for the customer in the tblCustomerTours table.
Access deletes the record from the tblCustomers table and any related tours for the customer in the tblCustomerTours table. If you hadn't enforced referential integrity between these tables, you would be left with one or more "orphan" records for Antonio Rommero in the tblCustomerTours table.
Close the tblCustomers table and open the tblCustomerTours tablein Datasheet view.
Yikes! The tblCustomerTours table contains a lot of ID number fields, such as CustomerID and TourID. Unless you have a printed a list of these ID numbers (or have a super-human memory), you won't know which CustomerID to enter. (By the way, this is why you will want to use a lot of forms and queries in relational databasesto hide all the technical inner workings of a database from your hapless users.)
Anyway, let's try adding a new record to the tblCustomerTours table.
Click the New Record button on the Record Navigation bar.
Access adds a new blank record to the table.
Enter a new record using the information in the following tables.
Before you finish adding the record, it's very important that you remember that you deleted the CustomerID 1 record back in Steps 2 and 3. Since you enforced referential integrity between the two related CustomerID fields, move on to Step 7 to find out what happens when you try to add the record.
Number of Tickets
Finish adding the record by pressing Tab or clicking in any other record when you have finished entering the record's information.
Because a CustomerID 1 doesn't exist in the tblCustomers table, a dialog box appears, as shown in Figure 5-10, informing you that Access can't add the record because doing so would violate referential integrity.
Click OK to close the dialog box.
You can cancel the addition of the new record.
Press Esc to cancel the new record, then close the tblCustomerTours table.
Now that you have a better feel for how referential integrity works, let's look at the other two topics in this lessonprinting and deleting table relationships.
Click the Relationships button on the toolbar.
Sometimes you may want a printed hard copy of the Relationships window.
Select File A report appears in Print Preview. You don't actually need to print anything for now, so...
Click the Close button to close the report without saving changes.
Sometimes you may want to delete the relationship between two tables. Access is very restrictive about letting you modify a related table, and often you must temporarily delete the relationship between two tables, modify one of the tables, and then re-connect them. Here's how to delete a table relationship.
Click the join line between the tblTours table and the tblCustomerTours table to select it and press Delete.
Note: Clicking a tiny join line between two tables requires a lot of mouse dexterityyou probably will have to try several times before you get it.
Access asks you to confirm the deletion. Let's leave the table relationship be for now.
Click No and click the Close button to close the Relationships window.