Monday, July 25, 2022

Video: Create one-to-one relationships [MS]

Your browser does not support video. Install Microsoft Silverlight, Adobe Flash Player, or Internet Explorer 9.

Try it!

What is a one-to-one relationship?

One-to-one relationships are frequently used to indicate critical relationships so you can get the data you need to run your business.

A one-to-one relationship is a link between the information in two tables, where each record in each table only appears once. For example, there might be a one-to-one relationship between employees and the cars they drive. Each employee appears only once in the Employees table, and each car appears only once in the Company Cars table.

You might use one-to-one relationships if you have a table containing a list of items, but the specific information you want to capture about them varies by type. For example, you might have a contacts table in which some people are employees and other people are subcontractors. For the employees, you want to know their employee number, their extension, and other key information. For subcontractors, you want to know their company name, phone number, and bill rate, among other things. In this case, you'd create three separate tables—Contacts, Employees, and Subcontractors— and then create a one-to-one relationship between the Contacts and Employees tables and a one-to-one relationship between the Contacts and Subcontractors tables.

Create one-to-one relationships overview

You create one-to-one relationships by linking the index (usually the primary key) in one table and an index in another table which shares the same value. For example:

Screen snippet showing two tables sharing an ID

Often, the best way to create this relationship is to have the secondary table look up a value from the first table. For example, make the Car ID field in the Employees table a lookup field that looks for a value in the Car ID index from the Company Cars table. That way, you never accidentally add the ID of a car that doesn't actually exist.

Important: When you create a one-to-one relationship, decide carefully whether to enforce referential data integrity for the relationship.

Referential data integrity helps Access to keep your data clean by deleting related records. For example, if you delete an employee from the Employees table, you also delete the benefits records for that employee from the Benefits table. But in some relationships, like this example, referential integrity doesn't make sense: if we delete an employee, we don't want the vehicle deleted from the Company Cars table, because the car will still belong to the company and will be assigned to someone else.

Create a one-to-one relationship steps

Create the one-to-one relationship by adding a lookup field to a table. (To learn how, see Build tables and set data types.) For example, to indicate which car has been assigned to a specific employee, you might add Car ID to the Employees table. Then, to create the relationship between the two fields, use the Lookup Wizard:

  1. Open a table.

  2. In Design View, add a new field, select the Data Type value, and then select Lookup Wizard.

  3. In the wizard, the default is set to look up values from another table, so select Next.

  4. Select the table that contains the key (usually a primary key) that you want to include in this table, and then select Next. In our example, you'd select the Company Cars table.

  5. In the Selected Fields list, add the field that contains the key you want to use. Select Next.

    Screen snippet of the Lookup Wizard
  6. Set a sort order and, if you prefer, change the width of the field.

  7. On the final screen, clear the Enable Data Integrity check box and then select Finish.

Want more?

Get started with table relationships

Create, edit or delete a relationship

No comments:

Post a Comment

Search This Blog