Join Alicia Katz Pollock for an in-depth discussion in this video Creating relationships and enforcing referential integrity, part of Access 2010 Essential Training.
- View Offline
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