Create a relationship - Access
To create a relationship in Access or While viewing a table in Design view, and ensuring that the DESIGN tab is selected, click. In a relational database (Access), the data in one table is related to the data in other tables. In general, tables can be related in one of three different ways. Describes how to define relationships in a database in Access , Access , Access , Access , or Access
As a result, the third table records each occurrence, or instance, of the relationship. For example, the Orders table and the Products table have a many-to-many relationship that is defined by creating two one-to-many relationships to the Order Details table. One order can have many products, and each product can appear on many orders. A one-to-one relationship In a one-to-one relationship, each record in the first table can have only one matching record in the second table, and each record in the second table can have only one matching record in the first table.
This relationship is not common because, most often, the information related in this way is stored in the same table. You might use a one-to-one relationship to divide a table with many fields, to isolate part of a table for security reasons, or to store information that applies only to a subset of the main table.
When you do identify such a relationship, both tables must share a common field. Top of Page Why create table relationships? You can create table relationships explicitly by using the Relationships window, or by dragging a field from the Field List pane.
Access uses table relationships to decide how to join tables when you need to use them in a database object. There are several reasons why you should create table relationships before you create other database objects, such as forms, queries and reports. Table relationships inform your query designs To work with records from more than one table, you often must create a query that joins the tables.
The query works by matching the values in the primary key field of the first table with a foreign key field in the second table. For example, to return rows that list all of the orders for each customer, you construct a query that joins the Customers table with the Orders table based on the Customer ID field.
In the Relationships window, you can manually specify the fields to join. But, if you already have a relationship defined between the tables, Access supplies the default join, based on the existing table relationship.
In addition, if you use one of the query wizards, Access uses the information it gathers from the table relationships you have already defined to present you with informed choices and to prepopulate property settings with appropriate default values. Table relationships inform your form and report designs When you design a form or report, Access uses the information it gathers from the table relationships you have already defined to present you with informed choices and to prepopulate property settings with appropriate default values.
Table relationships are the foundation upon which you can enforce referential integrity to help prevent orphan records in your database. When you design a database, you divide your information into tables, each of which has a primary key.
- Create a relationship
- Guide to table relationships
- How to define relationships between tables in an Access database
You then add foreign keys to related tables that reference those primary keys. These foreign key-primary key pairings form the basis for table relationships and multi-table queries. Referential integrity, which is dependent on table relationships, helps ensure that references stay synchronized.
Top of Page Understanding referential integrity When you design a database, you divide your database information into many subject-based tables to minimize data redundancy. You then give Access a way to bring the data back together by placing common fields into related tables. For example, to represent a one-to-many relationship you take the primary key from the "one" table and add it as an additional field to the "many" table.
To bring the data back together, Access takes the value in the "many" table and looks up the corresponding value in the "one" table.
In this way the values in the "many" table reference the corresponding values in the "one" table. Suppose you have a one-to-many relationship between Shippers and Orders and you want to delete a Shipper. If the shipper you want to delete has orders in the Orders table, those orders will become "orphans" when you delete the Shipper record.
The orders will still contain a shipper ID, but the ID will no longer be valid, because the record that it references no longer exists. The purpose of referential integrity is to prevent orphans and keep references in sync so that this hypothetical situation never occurs. You enforce referential integrity by enabling it for a table relationship see Enforce referential integrity for step-by-step instructions. Once enforced, Access rejects any operation that violates referential integrity for that table relationship.
This means Access will reject both updates that change the target of a reference, and deletions that remove the target of a reference. For such cases, what you really need is for Access to automatically update all the effected rows as part of a single operation.
That way, Access ensures that the update is completed in full so that your database is not left in an inconsistent state, with some rows updated and some not. When you enforce referential integrity and choose the Cascade Update Related Fields option, and you then update a primary key, Access automatically updates all fields that reference the primary key.Access Database Basics-Relationships 101
When you enforce referential integrity and choose the Cascade Delete Related Records option, and you then delete a record on the primary key side of the relationship, Access automatically deletes all records that reference the primary key. The Relationships window opens and displays any existing relationships.
If no table relationships have been defined and you are opening the Relationships window for the first time, Access prompts you to add a table or query to the window. Open the Relationships window Click File, and then click Open.
Select and open the database. On the Database Tools tab, in the Relationships group, click Relationships. If the database contains relationships, the Relationships window appears. If the database does not contain any relationships and you are opening the Relationships window for the first time, the Show Table dialog box appears.
Guide to table relationships - Access
Click Close to close the dialog box. On the Design tab, in the Relationships group, click All Relationships. This displays all of the defined relationships in your database. Note that hidden tables tables for which the Hidden check box in the table's Properties dialog box is selected and their relationships will not be shown unless the Show Hidden Objects check box is selected in the Navigation Options dialog box.
A table relationship is represented by a relationship line drawn between tables in the Relationships window. A relationship that does not enforce referential integrity appears as a thin line between the common fields supporting the relationship. When you select the relationship by clicking its line, the line thickens to indicate it is selected. If you enforce referential integrity for this relationship, the line appears thicker at each end.
When the Relationships window is active, you can select from the following commands on the ribbon: On the Design tab, in the Tools group: When you select a relationship line, you can click Edit Relationships to change the table relationship.
You can also double-click the relationship line. The report shows only the tables and relationships that are not hidden in the Relationships window.
On the Design tab, in the Relationships group: Note that hidden tables tables for which the Hidden check box in the table's Properties dialog box is selected and their relationships will not be shown unless Show Hidden Objects is selected in the Navigation Options dialog box. If you made any changes to the layout of the Relationships window, you are asked whether to save those changes.
Top of Page Create a table relationship You can create a table relationship by using the Relationships window, or by dragging a field onto a datasheet from the Field List pane. When you create a relationship between tables, the common fields are not required to have the same names, although it is often the case that they do. Rather, those fields must have the same data type.
If the primary key field is an AutoNumber field, however, the foreign key field can be a Number field if the FieldSize property of both fields is the same. When both common fields are Number fields, they must have the same FieldSize property setting. Create a table relationship by using the Relationships window Click File, and then click Open. If you have not yet defined any relationships, the Show Table dialog box automatically appears.
If it does not appear, on the Design tab, in the Relationships group, click Show Table. The Show Table dialog box displays all of the tables and queries in the database. To see only tables, click Tables.
Why Use Microsoft Access? | What Is a Relational Database? | InformIT
To see only queries, click Queries. To see both tables and queries, click Both. Select one or more tables or queries and then click Add. When you have finished adding tables and queries to the Relationships window, click Close. Drag a field typically the primary key from one table to the common field the foreign key in the other table.
To drag multiple fields, press the CTRL key, click each field, and then drag them. The Edit Relationships dialog box appears. Verify that the field names shown are the common fields for the relationship. If a field name is incorrect, click the field name and select a new field from the list. To enforce referential integrity for this relationship, select the Enforce Referential Integrity check box.
For more information about referential integrity, see the Understanding Referential Integrity and the Enforce Referential Integrity sections. The relationship line is drawn between the two tables. If you selected the Enforce Referential Integrity check box, the line appears thicker at each end. A one-to-many relationship is created if only one of the related columns is a primary key or has a unique constraint.
In the relationship window in Access, the primary key side of a one-to-many relationship is denoted by a number 1. The foreign key side of a relationship is denoted by an infinity symbol.
Many-to-many relationships In a many-to-many relationship, a row in table A can have many matching rows in table B, and vice versa. You create such a relationship by defining a third table that is called a junction table. The primary key of the junction table consists of the foreign keys from both table A and table B. For example, the "Authors" table and the "Titles" table have a many-to-many relationship that is defined by a one-to-many relationship from each of these tables to the "TitleAuthors" table.
One-to-one relationships In a one-to-one relationship, a row in table A can have no more than one matching row in table B, and vice versa. A one-to-one relationship is created if both of the related columns are primary keys or have unique constraints.
This kind of relationship is not common, because most information that is related in this manner would be in one table. You might use a one-to-one relationship to take the following actions: Divide a table with many columns.
Isolate part of a table for security reasons. Store data that is short-lived and could be easily deleted by deleting the table. Store information that applies only to a subset of the main table.
In Access, the primary key side of a one-to-one relationship is denoted by a key symbol. The foreign key side is also denoted by a key symbol. How to define relationships between tables When you create a relationship between tables, the related fields do not have to have the same names.
However, related fields must have the same data type unless the primary key field is an AutoNumber field. You can match an AutoNumber field with a Number field only if the FieldSize property of both of the matching fields is the same. Even when both matching fields are Number fields, they must have the same FieldSize property setting.
How to define a one-to-many or one-to-one relationship To create a one-to-many or a one-to-one relationship, follow these steps: You cannot create or change relationships between open tables. In Access or Accessfollow these steps: Press F11 to switch to the Database window. On the Tools menu, click Relationships. If you have not yet defined any relationships in your database, the Show Table dialog box is automatically displayed. To create a relationship between a table and itself, add that table two times.
Drag the field that you want to relate from one table to the related field in the other table. To drag multiple fields, press Ctrl, click each field, and then drag them. In most cases, you drag the primary key field this field is displayed in bold text from one table to a similar field this field frequently has the same name that is called the foreign key in the other table.
Make sure that the field names that are displayed in the two columns are correct. You can change the names if it is necessary. Set the relationship options if it is necessary. These options will be explained in detail later in this article.
Click Create to create the relationship. Repeat steps 4 through 7 for each pair of tables that you want to relate. Whether you save the layout or do not save the layout, the relationships that you create are saved in the database.
However, referential integrity is not enforced with queries. How to define a many-to-many relationship To create a many-to-many relationship, follow these steps: Create the two tables that will have a many-to-many relationship. Create a third table. This is the junction table. In the junction table, add new fields that have the same definitions as the primary key fields from each table that you created in step 1.
In the junction table, the primary key fields function as foreign keys.
Why Use Microsoft Access?
You can add other fields to the junction table, just as you can to any other table. In the junction table, set the primary key to include the primary key fields from the other two tables. Note To create a primary key, follow these steps: Open a table in Design view. Select the field or fields that you want to define as the primary key.
To select one field, click the row selector for the desired field. To select multiple fields, hold down the Ctrl key, and then click the row selector for each field.