Join Alicia Katz Pollock for an in-depth discussion in this video Creating relationships and enforcing referential integrity, part of Access 2010 Essential Training.
Access 2010 is considered a relational database, meaning that the data is split into tables that are interconnected. In this lesson, we'll create relationships between our four tables and set the referential integrity. Start by clicking on the Database Tools tab, then on the Relationships button. Click on the Show Tables button. Then double-click each of the four tables. Now, I have a tendency to click on Add, thinking I am going to close the window. So, if you do this, it will accidentally add in a repeat of that same table.
You can see the _1. Go ahead and close the Show Table button. Click on that extra table and hit the Delete button on your keyboard and it will disappear. Now, hold your cursor over the bottom edge of the Customers table until you get a double-headed arrow. Then click and drag them down so that you can see the rest of your fields, and do the same thing on the SalesReps table. So, let's start with our Customers table. One of the fields is SalesRep, and it gets that info from the SalesReps table. So, let's drag the primary key field, EmpID, from the SalesReps table and drop it on SalesReps here in the Customers table.
It brings up an Edit Relationships window showing the two: the source field and the destination field. We want to click in the check box to Enforce Referential Integrity. Then check both Cascade Update and Cascade Delete Related Records. Cascade Update means if we make any changes to the EmpID field, it will make the same changes in any other tables that use that field. In our case, if we change a SalesRep's ID, it would make the same changes in the Customer table. If we didn't check this, we could potentially find SalesReps in the Customer table that no longer existed.
Those are called orphaned records. We also have a Join Type to set. Click this button here. There are three states: Only include rows where the joined fields from both tables are equal, Include All records from SalesReps and only those records from Customers where the joined fields are equal, Include All records from Customers and only those records from SalesReps where the joined fields are equal. Now, most of the time we use the first one, but in cases where we would want to see reports that included the SalesReps, even if they didn't have any assigned Customers, we would choose the second option.
If we wanted to run reports that included all of our Customers, even if they didn't have any SalesReps, we'd choose that option. Let's go ahead and choose option number two and then click OK. Now, notice down here the One-To-Many designation, indicating that there can only be one of each rep, and that each rep can have many customers. Then click Create. You'll now see a black line running between the SalesReps table and the Customers table. There is 1 right here, indicating the one part of the relationship, and there is an infinity symbol right here, indicating the many customers that each rep might have.
Notice that line goes under the Products table. You can drag the tables and move them around to your liking. In fact, let's move SalesReps over to the left and move all of these tables over. Next, the Customers all place orders. So, let's dragged the CustID field over to the Customer field in the Orders table. Enforce Referential Integrity and Cascade Update and Delete so that if we delete a customer from the database, all of their orders will disappear too.
We don't need to adjust the Join Type. We won't ever have customers that haven't place an order, and we won't have orders without customers. Click Create, and you'll see the One-To-Many relationship. There is no arrow here, the way there is here, because we did a normal Join Type instead of a Type2. Now, let's do the same for the SKU in the Product table. Pick it up and drag it to the Product in the Orders table, Enforce Referential Integrity, but only the Cascade Update.
We don't want a Cascade Delete, because if we discontinue our product, we don't want that product's SKU to disappear out of an order. We also don't need to change the Join Type. Click Create to finish. Now, click the Close button and Access will ask to save the changes. Say Yes. Now, all of our tables are linked together and Access understands which pieces of data interrelate. Relationships will also allow us to bring data together from the different tables into our forms and reports.
- Understanding database concepts and terminology
- Building tables with Application Parts
- Defining field properties
- Creating relationships between fields and tables
- Sorting and filtering
- Creating forms with the Form Wizard
- Analyzing data with the Query Designer
- Automating with macros
- Formatting reports with Layout Tools
Skill Level Beginner
Q: It seems there are movies missing in the course which should explain how to enter products in the Order table. How do I do this?
Q: In the Chapter 6 video "Using Design view," we work with the Combo Box Wizard. When I click on the Combo Box then click the location on the form, it does not start the Combo Wizard. Please advise.
A: Click on the Data tab and make sure one of the tables or queries appears selected in the Control Source. The form needs to be bound to a table or query before you make the combo box.
Also, Access is extremely finicky. When you're looking at the Properties window, be sure to click in the little box in the upper left corner of the form, between the vertical and horizontal rulers—as noted in the screenshot—before creating the combo box.
Q: It seems there are steps missing in the course. The file I built myself by following along doesn't match what's on screen. Why not?
A: The lessons in this course are only somewhat cumulative. Each movie is a standalone explanation of a technique or feature. The database is built throughout the course, but not every step between the lessons are shown. The course would be overly long if the author repeated every step in building a realistic, functioning database. However, the missing steps are variations on the techniques you've already learned. Also, this is one of those courses where using the exercise files is recommended.