How do relations work in access




















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 web database.

In the Relationships window, add the tables that you want to relate, and then drag the field to relate them from one table to the other table. 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.

The common fields must have the same data type. If the primary key field is an AutoNumber field, however, the foreign key field can also 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. On the Database Tools tab, in the Relationships group, click Relationships. Select one or more tables or queries and then click Add.

After you have finished adding tables and queries to the Relationships document tab, 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 on the field name and select the appropriate 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 section Enforce Referential Integrity. Access draws a relationship line between the two tables.

If you selected the Enforce Referential Integrity check box, the line appears thicker at each end. To create a one-to-one relationship Both of the common fields typically the primary key and foreign key fields must have a unique index. This means that the Indexed property for these fields should be set to Yes No Duplicates. If both fields have 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 have a unique index.

This means that the Indexed property for this field should be set to Yes No Duplicates. The field on the many side should not have a unique index.

It can have an index, but it must allow duplicates. When one field has a unique index, and the other does not, Access creates a one-to-many relationship. You can add a field to an existing table that is open in Datasheet view by dragging it from the Field List pane. The Field List pane shows fields available in related tables and also fields available in other tables in the database.

When you drag a field from an "other" unrelated table and then complete the Lookup Wizard, a new one-to-many relationship is automatically created between the table in the Field List pane and the table to which you dragged the field.

This relationship, created by Access, does not enforce referential integrity by default. To enforce referential integrity, you must edit the relationship. See the section Edit a relationship for more information. The Field List pane shows all of the other tables in your database, grouped into categories. When you work with a table in Datasheet view, Access displays 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 lists all of the tables that have a relationship with the table with which you are currently working. The second category lists all of the tables with which your table does not have a relationship.

To add a field to your table, drag the field that you want from the Field List pane to the table in Datasheet view. The Field List pane appears. Drag the field that you want from the Field List pane to the table that is open in Datasheet view. When you drag a field from an "other" unrelated table and then complete the Lookup Wizard, a new one-to-many relationship is automatically created between the table in the Field List and the table to which you dragged the field.

Carefully position the cursor so that it points to the relationship line, and then click the line to select it. On the Design tab, in the Tools group, click Edit Relationships. On the Design tab, in the Relationships group, click All Relationships. You define a relationship by adding the tables that you want to relate to the Relationships window, and then dragging the key field from one table and dropping it on the key field in the other table. The kind of relationship that Microsoft Access creates depends on how the related fields are defined:.

Note If you drag a field that isn't a primary key and doesn't have a unique index to another field that isn't a primary key and doesn't have a unique index, an indeterminate relationship is created.

In queries containing tables with an indeterminate relationship, Microsoft Access displays a default join line between the tables, but referential integrity won't be enforced, and there's no guarantee that records are unique in either table. When creating a relationship between two tables MS Access provides uses the Referential Integrity feature.

This feature prevents adding records to a detail table for which there is no matching record in the master table. The primary key Class ID uniquely identifies each class in the Classes table.

Using the example above, create a table named Enrollments. This will be the join table. Join tables typically hold fields that might not make sense to have in any other table.

You can add fields to the Enrollments table, such as a Date field to keep track of when someone started a class, and a Cost field to track how much a student paid to take a class. Create a relationship between the two Student ID fields in the tables.

Then create a relationship between the two Class ID fields in the tables. Using this design, if a student registers for three classes, that student will have one record in the Students table and three records in the Enrollments table—one record for each class the student enrolled in.

Above, tables with data about customers and orders that have a one-to-many relationship. The Customers table holds a unique record for each customer. Each customer can and, we hope, does place many orders. Many records in the Orders table can relate to only one record in the Customers table. This is a one-to-many relationship 1:N between the Customers table and the Orders table. In a one-to-many relationship, the table on the one side of the relationship is the primary table and the table on the many side is the related table.

A one-to-many relationship is the most common relationship found between tables in a relational database. The following is an example of a form used in a database to display the data from two tables with a one-to-many relationship.



0コメント

  • 1000 / 1000