Advertisements

Create, edit or delete a relationship

Applies to: Access for Microsoft 365, Access 2019, Access 2016, Access 2013, Access 2010, Access 2007.

A relationship in Access aids you integrate data from two unrelated tables. Every relationship comprises fields in two tables with corresponding data. For example, you might have a ProductID field in a Products table and in an OrderDetails table. Each record in the OrderDetails table has a ProductID that corresponds to a record in the Products table with the same ProductID.

Once you use related tables in a query, the relationship enables Access to pinpoint which records from each table to merge in the result set. A relationship can even assist with precluding missing data, by retaining deleted data from getting out of sync, and this is named referential integrity.

Before you work with relationships, ensure you understand the background concepts. For more information, see Guide to table relationships and Get started with table relationships.

In this article

Overview

Create a table relationship by using the Relationships window

Create a table relationship by using the Field List pane to add a field

Edit a relationship

Enforce referential integrity

Delete a table relationship

Create, edit, or delete a relationship in a Web App

Create or modify a relationship in an Access 2010 web database

Overview

In an Access database, you make a table relationship using one of the following methods:

  • In the Relationships window, create the tables that you want to relate, and then drag the field to relate them from one table to the other table.
  • Drag a field on to a table datasheet from the Field List pane.

After you produce a relationship between tables, the common fields are not obliged to have consistent names, although it is typically the case that they do. The common fields must belong to the same data type. If the primary key field is an AutoNumber field, however, the foreign key field can even be a Number field if the FieldSize property of both fields is the same. For example, you can match an AutoNumber field and a Number field if the FieldSize property of both fields is Long Integer. When both common fields are Number fields, they must have the same FieldSize property setting.

Top of Page

Create a table relationship by using the Relationships window

  1. On the Database Tools tab, in the Relationships group, pick Relationships.
  1. If you have not yet defined any relationships, the Show Table dialogue box automatically emerges. If it does not appear, on the Design tab, in the Relationships group, press Show Table.

    The Show Table dialogue box shoes each of the tables and queries in the database. To view only tables, pick Tables. To see only queries, choose Queries. To see both, select Both.

  1. Pick one or additional tables or queries and then press Add. After you have finished creating tables and queries to the Relationships document tab, press Close.
  1. Drag a field (typically the primary key) from one table to the common field (the foreign key) in the other table. To drag several fields, press the CTRL key, select all fields, and then drag them.

    The Edit Relationships dialogue box launches.


Edit Relationships dialog box

  1. Confirm that the field names listed are the common fields for the relationship. If a field name is wrong, pick on the field name and choose the suitable field from the list.

    To enforce referential integrity for this relationship, choose the Enforce Referential Integrity checkbox. For more information about referential integrity, see the section Enforce Referential Integrity.

  1. Press Create. Access draws a relationship line between the two tables. If you chose the Enforce Referential Integrity checkbox, the line displays thicker at both ends. Moreover, again only if you picked the Enforce Referential Integrity checkbox, the number 1 emerges over the thick portion on one side of the relationship line, and the infinity symbol () presents over the thick portion on the on the other side of the line, as denoted in the following figure.

    Drag a field from one table to the matching field in the other table.

Notes: 

  • To create a one-to-one relationship  –  Both of the common fields (typically the primary key and foreign key fields) must own a unique index. This means that the Indexed property for these fields must be set to Yes (No Duplicates). If both fields contain a unique index, Access creates a one-to-one relationship.
  • To create a one-to-many relationship – The field on the one side (typically the primary key) of the relationship must be assigned a unique index. It follows that the Indexed property for this field needs to be set to Yes (No Duplicates). The field on the many side should exclude a unique index. It can have an index, but it should permit duplicates. This means that the Indexed property for this field should be toggled to either No or Yes (Duplicates OK). After one field has a unique index, and the other does not, Access makes a one-to-many relationship.

Top of Page

Create a table relationship by using the Field List pane to add a field

You can create a field to an existing table that is open in Datasheet view by dragging it from the Field List pane. The Field List pane presents fields available in related tables and also fields available in other tables in the database.

After you drag a field from an “other” (unrelated) table and then finish the Lookup Wizard, a new one-to-many relationship is promptly designed between the table in the Field List pane and the table to which you dragged the field. This relationship, generated by Access, does not enforce referential integrity intuitively. To enforce referential integrity, you are required to edit the relationship. See the section Edit a relationship for more information.

Open a table in Datasheet view

  • In the Navigation Pane, double-click the table.

Open the Field List pane

  • Press ALT+F8. The Field List pane launches.
Field List pane

The Field List pane displays all of the remaining tables in your database, organised into categories. Once you interact with a table in Datasheet view, Access lists fields in either of two categories in the Field List pane: Fields available in related tables and Fields available in other tables. The first category reveals each of the tables that have a relationship with the table with which you are presently working. The second category displays all of the tables with which your table does not have a relationship.

In the Field List pane, after you select the plus sign (+) beside a table name, you notice a list of all the fields available in that table. To insert a field to your table, drag your desired field from the Field List pane to the table in Datasheet view.

Add a field and create a relationship from the Field List pane

  1. With the table accessed in Datasheet view, press ALT+F8. The Field List pane emerges.
  1. Below Fields available in other tables, pick the plus sign (+) adjacent to a table name to display the list of fields in that table.
  1. Drag your sought field from the Field List pane to the table that is open in Datasheet view.
  1. Once the insertion line emerges, drop the field into position. The Lookup Wizard starts.
  1. Follow the instructions to finish the Lookup Wizard. The field displays in the table in Datasheet view.

After you drag a field from an “other” (unrelated) table and then complete the Lookup Wizard, a new one-to-many relationship is instantly generated between the table in the Field List and the table to which you dragged the field. This relationship, created by Access, avoids enforcing referential integrity by default. To enforce referential integrity, you must edit the relationship. See the section Edit a relationship for more information.

Top of Page

Edit a relationship

You alter a relationship by picking it in the Relationships window and then editing it.

  1. Carefully locate the cursor so that it points to the relationship line, and then click the line to choose it. The relationship line displays thicker once it is picked.
  1. With the relationship line chosen, double-click it.–or–On the Design tab, in the Tools group, press Edit Relationships.

The Edit Relationships dialogue box appears.

Open the Edit Relationships dialogue box

  1. On the Database Tools tab, in the Relationships group, pick Relationships. The Relationships window appears. If you have not yet defined any relationships and you have not previously opened the Relationships window, the Show Table dialogue box launches. If the dialogue box appears, select Close.
  1. On the Design tab, in the Relationships group, press All Relationships. Every table with relationships are shown, presenting relationship lines. Note that hidden tables (tables for which the Hidden checkbox in the table’s Properties dialogue box is chosen) and their relationships will remain hidden unless Show Hidden Objects is chosen in the Navigation Options dialogue box.
  1. Select your preferred relationship line for the relationship to edit. The relationship line becomes thicker when it is picked.
  1. Double-click the relationship line.-or-On the Design tab, in the Tools group, press Edit Relationships.The Edit Relationships dialogue box launches.

Edit Relationships dialog box

  1. Verify your changes, and then pick OK. The Edit Relationships dialogue box enables you to amend a table relationship. Specifically, you can edit the tables or queries on either side of the relationship, or the fields on either side. You can even define the join type, or enforce referential integrity and select a cascade option. For more information about the join type and how to set it, see the section Set the join type. For more details about how to enforce referential integrity and pick a cascade option, see the section Enforce referential integrity.

Set the join type

After you define a table relationship, the facts about the relationship determine your query designs. For example, if you define a relationship between two tables, and you then create a query that employs the two tables, Access automatically chooses the default matching fields based upon the fields specified in the relationship. You can bypass these first standard values in your query, but the values offered by the relationship will usually prove to be the correct ones. Because matching and bringing together data from several tables is a regular occurrence in all except the most basic databases, setting defaults by making relationships can be fast saving and valuable.

A multiple table query merges information from numerous tables by matching the values in common fields. The operation that does the matching and combining is called a join. For instance, suppose you want to display customer orders. You create a query that joins the Customers table and the Orders table on the Customer ID field. The query result contains customer information and order information for only those rows where a corresponding match was found.

One of the values you can assign for each relationship is the join type. The join type informs Access which records to contain within a query result. For example, consider again a query that joins the Customers table and the Orders table on the common fields that represents the Customer ID. Using the default join type (called an inner join), the query returns only the Customer rows and the Order rows where the common fields (also called the joined fields) are equal.

However, suppose you want to include all Customers — even those who have not yet placed any orders. To achieve this, you have to alter the join type from an inner join to what is known as a left outer join. A left outer join returns all of the rows in the table on the left side of the relationship and only those that match in the table on the right. A right outer join returns all of the rows on the right and only those that match on the left.

Note: In this case, “left” and “right” refer to the position of the tables in the Edit Relationships dialogue box, not the Relationships window.

You should consider the result you will most frequently want from a query that joins the tables in this relationship, and then set the join type accordingly.

Set the join type

  1. In the Edit Relationships dialogue box, pick Join Type. The Join Properties dialogue box appears.
  1. Press your choice, and then click OK.

The following table (using the Customers and Orders tables) lists the three choices that are presented in the Join Properties dialogue box, the type of join they use, and whether all rows or matching rows are returned for each table.

ChoiceRelational joinLeft tableRight table
1. Only consider rows where the joined fields from both tables are equal.Inner joinMatching rowsMatching rows
2. Take into account ALL records from ‘Customers’ and only those records from ‘Orders’ where the joined fields are equal.Left outer joinAll rowsMatching rows
3. Use ALL records from ‘Orders’ and only those records from ‘Customers’ where the joined fields are equal.Right outer joinMatching rowsAll rows

After you select option 2 or option 3, an arrow is presented on the relationship line. This arrow directs to the side of the relationship that displays only matching rows.

Make changes in the Join Properties dialogue box

  1. On the Database Tools tab, in the Relationships group, pick Relationships.

    Note:  If you have not yet defined any relationships and this is the first time you are opening the Relationships window, the Show Table dialogue box emerges. If the dialogue box appears, choose Close.
  1. On the Design tab, in the Relationships group, pick All Relationships.

    All tables that have relationships are shown, displaying relationship lines. Note that hidden tables (tables for which the Hidden checkbox in the table’s Properties dialogue box is clicked) and their relationships will remain out of view unless Show Hidden Objects is picked in the Navigation Options dialogue box.
  1. Select the relationship line for the relationship that you want to change. The relationship line becomes denser when it is chosen.
  1. Double-click the relationship line. The Edit Relationships dialogue box emerges.

  1. Press Join Type.

  1. In the Join Properties dialogue box, click an option, and then select OK.
    Join Properties dialog box

  1. Set any additional changes to the relationship, and then pick OK.

Top of Page

Enforce referential integrity

The objective of referential integrity is to impede orphan records – records that reference other records which are now obsolete. You enforce referential integrity by activating it for a table relationship. When enforced, Access dismisses any operation that would breach referential integrity for that table relationship. Thus, Access will reject both updates that edit the target of a reference and deletions that delete the target of a reference. To have Access disseminate referential updates and deletions so that all related rows are changed accordingly, see the Set the cascade options section.

Turn referential integrity on or off

  1. On the Database Tools tab, in the Relationships group, pick Relationships.
  1. On the Design tab, in the Relationships group, select All Relationships.

    All tables with relationships are shown, illustrating relationship lines. Note that hidden tables (tables for which the Hidden checkbox in the table’s Properties dialogue box is chosen) and their relationships will remain hidden unless Show Hidden Objects is picked in the Navigation Options dialogue box.
  1. Select your desired relationship line for the relationship to alter. The relationship line becomes thicker once it is chosen.
  1. Double-click the relationship line. The Edit Relationships dialogue box emerges.
  1. Choose or clear Enforce Referential Integrity.

  1. Apply any additional changes to the relationship, and then press OK.

If you enforce referential integrity, the following rules apply:

  • You cannot enter a value in the foreign key field of a related table if that value doesn’t exist in the primary key field of the primary table — doing so creates orphan records.
  • You cannot delete a record from a primary table if matching records exist in a related table. For example, you cannot delete an employee record from the Employees table if there are orders assigned to that employee in the Orders table. You can, however, choose to delete a primary record and all related records in one operation by selecting the Cascade Delete Related Records checkbox.
  • You cannot change a primary key value in the primary table if doing so would create orphan records. For example, you cannot change an order number in the Orders table if there are line items assigned to that order in the Order Details table. You can, however, choose to update a primary record and all related records in one operation by selecting the Cascade Update Related Fields checkbox.

Notes: If you have difficulty enabling referential integrity, note that the following conditions are required to enforce referential integrity:

  • The common field from the primary table must be a primary key or have a unique index.
  • The common fields must have the same data type. The one exception is that an AutoNumber field can be related to a Number field that has a FieldSize property setting of Long Integer.
  • Both tables exist in the same Access database. Referential integrity cannot be enforced on linked tables. However, if the source tables are in Access format, you can open the database in which they are stored and enable referential integrity in that database.

Set the cascade options

You could experience a situation where you have a valid reason to update the value on the “one” side of a relationship. In such a case, you require Access to autonomously revise each of the affected rows as part of a single operation. Accordingly, the update is performed entirely to prevent your database from being inaccurate — with some rows updated and some not. Access assists you with overcoming this problem by supporting the Cascade Update Related Fields option. After you enforce referential integrity and select the Cascade Update Related Fields option, and you then update a primary key, Access instantly amends all fields that reference the primary key.

You may even have to delete a row and all related records — for instance, a Shipper record and all related orders for that shipper. For this reason, Access supports the Cascade Delete Related Records option. After you enforce referential integrity and select the Cascade Delete Related Records checkbox, Access promptly erases every record that reference the primary key after you delete the record that contains the primary key.

Turn cascade update and/or cascade delete on or off

  1. On the Database Tools tab, in the Relationships group, pick Relationships.
  1. On the Design tab, in the Relationships group, press All Relationships.

    All tables with relationships are presented, displaying relationship lines. Note that hidden tables (tables for which the Hidden checkbox in the table’s Properties dialogue box is chosen) and their relationships will stay hidden unless Show Hidden Objects is clicked in the Navigation Options dialogue box.
  1. Select the relationship line for the relationship that you want to change. The relationship line thickens once it is chosen.
  1. Double-click the relationship line.

    The Edit Relationships dialogue box comes up.

  1. Click the Enforce Referential Integrity checkbox.
  1. Choose either the Cascade Update Related Fields or the Cascade Delete Related Records checkbox, or pick both.
  1. Set any additional changes to the relationship, and then select OK.

Note: If the primary key is an AutoNumber field, selecting the Cascade Update Related Fields checkbox will have no effect, because you cannot change the value in an AutoNumber field.

Top of Page

Delete a table relationship

Important: Once you remove a relationship, you also remove referential integrity support for that relationship, if it is enabled. As a result, Access will no longer automatically prevent the creation of orphan records on the “many” side of a relationship.

To remove a table relationship, you must delete the relationship line in the Relationships window. Position the cursor so that it points to the relationship line, and then click the line. The relationship line appears thicker when it is selected. With the relationship line selected, press DELETE.

  1. On the Database Tools tab, in the Relationships group, pick Relationships.
  1. On the Design tab, in the Relationships group, select All Relationships.

    All tables with relationships are presented, displaying relationship lines. Note that hidden tables (tables for which the Hidden checkbox in the table’s Properties dialogue box is chosen) and their relationships will not be shown unless Show Hidden Objects is picked in the Navigation Options dialogue box.
  1. Select your target relationship line for deleting your desired relationship to delete. The relationship line thickens after it is chosen.
  1. Press the DELETE key.
  1. Access may present the message Are you sure you want to permanently delete the selected relationship from your database?. If this confirmation message appears, press Yes.

Note: If either of the tables employed in the table relationship are in use — perhaps by another person or process, or in an open database object, such as a form — you will not be able to delete the relationship. You must exit any open objects that utilise these tables before you try to erase the relationship.

Top of Page

Create, edit, or delete a relationship in an Access web app

There are vital variations when working with relationships in an Access web app.

Create a relationship

The Relationships window isn’t available in an Access web app. Rather than making a relationship in an Access web app, you design a lookup field that obtains values from a related field in another table. For example, let’s say you have an Employees table and you want to add a lookup to a Regions table so you can show which region each employee works in.

Note:  The field that your lookup will use as the source for values must already exist before you create your lookup field.

Here’s how you produce a lookup field in an Access web app:

  1. Enter your preferred table for creating a new lookup field by double-clicking it in the navigation. (Hint: you may need to click Home > Navigation Pane to see the available tables.)In the above example, select the Employees table.
  1. Pick in the Field Name column right under the last field in the table and state a name for your new lookup field. In the example, type Region as the field name.

  1. In the Data Type column, select the arrow and pick Lookup.Setting the Lookup data type for a lookup field

  1. The Lookup Wizard commences.

  1. On the first page of the Lookup Wizard, pick I want the lookup field to get values from another table or query. More options unravel in the dialogue box.

Pick the name of the table or query that should provide the values for your lookup. In the example, choose Table: Regions.

Options to choose in Lookup Wizard

(Names of the tables in the image correspond to the example we’re using.)

  1. After you choose the table, use the Which value do you want to display in your lookup list to pick your desired field to use as a display value for your lookup field. Automatically, Access highlights the first text field it can gather in the chosen table. In the example, you would leave the picked field, Title, as the display value.

  1. Use the Do you want to sort the items in your lookup list to set the sorting, if preferred.
  1. Below What should happen when a record from the “Regions” table is deleted, define your preferred type of relationship between the two tables and whether you want to enforce referential integrity. (The name of the table in this question varies depending on which table you selected in step 5.)The Lookup Wizard reverts to Prevent delete if there are corresponding records in the “Employees” table, because that’s the most convenient option in many cases. In the example, this option means you can’t delete a value from the Regions table if that region is being used in records in the Employees table. So, if employee records are using a region, such as “West” and you aim to delete “West” from the Regions table, Access will block you from deleting it. In this case, you must reset all employee records using that value to something else, before you can erase “West” from the Regions table. The last option could work in this example, because that would enable you to delete “West” from the Regions table. The region value would be promptly deleted from Employee records that were set to “West,” leaving the value blank. Selecting the second option would erase all employee records from the Employees table that have the region set to “West.” That’s called a cascading delete and would delete far more data than you want in the example. Be careful when choosing that option.

Edit a relationship

The Relationships window isn’t available in an Access web app. You apply a field in one table as the source (lookup field) for values in the related field in another table.

  1. Launch the table that includes the lookup you want to change by selecting Edit Table.
Edit Table
  1. Pick the field that gets its values from another table.

  1. On the Design tab, under Table Tools, pick Modify Lookups.

  1. Follow the steps in the wizard to make your relevant changes. You can modify the following:
  • The field that is the source of values
  • The sort order of the values
  • The width of the field, and whether to hide the key column
  • The label for the lookup field
  • Whether data integrity is enabled
  • If data integrity is enabled, whether deletes are cascaded or restricted

Delete a relationship

The Relationships window isn’t available in an Access web app. You utilise a field in one table as the source (lookup field) for values in the related field in another table. To actually delete a relationship between two tables in an Access web app, you must delete the lookup field and its related data.

  1. Access the table that includes the lookup field you want to delete by picking Edit Table.
Edit Table
  1. Choose the lookup field that gets its values from another table in the web app.
  1. On the Design tab, under Table Tools, press Delete Fields.
  1. Access shows the message Do you want to permanently delete the selected fields(s) and all the data in the field(s)? Press Yes to confirm erasing the lookup field and its data.

Note: You might also be prompted by Access to delete one or more indexes when deleting the lookup field. If this confirmation message appears, click Yes.

  1. Select Save to save your table design changes.

Top of Page

Create or modify a relationship in an Access 2010 web database

To design a relationship in an Access 2010 web database, you use the Lookup Wizard. The Relationships window is unavailable in a web database. You apply a field in one table as the source for values in the related field in another table.

Note:  The field that you use as the source for values must already exist before you can use the Lookup Wizard.

Create a relationship in an Access 2010 web database by using the Lookup Wizard

  1. Access the table that should get its values from the other table.
  1. To the right of the last field, pick Click to Add, and then choose Lookup & Relationship.

Note:  You may have to scroll horizontally to notice Click to Add.

  1. On the first screen of the Lookup Wizard, click I want the lookup field to get the values from another table, and then press Next.
  1. Choose the table that has the source values, and then pick Next.
  1. In the Available Fields box, double-click the field that contains the source values, and then press Next.
  1. Optionally, state a sort order for the lookup field. Press Next.
  1. Optionally, modify the width of the lookup field – a good idea if the values are long. Pick Next.
  1. Type a name for the new field. If you want to ensure that the values in the two tables always correspond, choose the Enable Data Integrity checkbox, and then select one of the following:
  • Cascade Delete – this option means that if you erase a record from one table, matching records in the other table are also removed.
  • Restrict Delete – this option means that if you seek to delete a record from one table but there is a related record in the other table, the delete operation is prohibited.

Note:  Avoid choosing Allow Multiple Values in the Lookup Wizard when you are using the wizard to create a relationship.

Modify a relationship in an Access 2010 web database

  1. Access the table that gets its values from another table.
  1. Pick the field that gets its values from another table.
  1. On the Fields tab, in the Properties group, select Modify Lookups.
  1. Follow the steps in the wizard to verify your sought changes. You can edit the following:
  • The field that is the source of values
  • The sort order of the values
  • The width of the field, and whether to hide the key column
  • The label for the lookup field
  • Whether data integrity is enabled
  • If data integrity is enabled, whether deletes are cascaded or restricted.

Top of Page

See Also

Add a lookup field to a table

What is the Relationships window?

Compare two tables in Access and find only matching data

Advertisements

Leave a Reply

%d bloggers like this: