Create -edit or delete a relationship Access 2007

Author: mety Labels::



After you have created a table for each subject in your database, you must provide Office Access 2007 with the means by which to bring that information back together again when needed. You do this by placing common fields in tables that are related, and by defining table relationships between your tables. You can then create queries, forms, and reports that display information from several tables at once.

Create a table relationship

You can create a table relationship in the Relationships window, or by dragging a field on to 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, 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. 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.

Create a table relationship by using the Relationships document tab

  1. Click the Microsoft Office Button Button image, and then click Open.
  2. In the Open dialog box, select and open the database.
  3. On the Database Tools tab, in the Show/Hide group, click Relationships. Access Ribbon Image
  4. 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. Ribbon Design Tab Relationships Group
    The Show Table dialog box displays all of the tables and queries in the database. To see only tables, click Tables. To see only queries, click Queries. To see both, click Both.
  5. 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.
  6. 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.
    Edit Relationships dialog box
  7. 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.
  8. Click Create. 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. In addition, again only if you selected the Enforce Referential Integrity check box, the number 1 appears over the thick portion on one side of the relationship line, and the infinity symbol () appears over the thick portion on the on the other side of the line, as shown in the following figure.
Drag a field from a table to the matching field in another table
Notes
  • 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. This means that the Indexed property for this field should be set to either No or Yes (Duplicates OK). When one field has a unique index, and the other does not, Access creates a one-to-many relationship.


Create a table relationship by using the Field List pane

In Office Access 2007, 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.

Open a table in Datasheet view

  1. Click the Microsoft Office Button Button image, and then click Open.
  2. In the Open dialog box, select and open the database.
  3. In the Navigation Pane, right-click the table to which you want to add the field and create the relationship, and then click Datasheet View on the shortcut menu.

Open the Field List pane

  • On the Datasheet tab, in the Fields & Columns group, click Add Existing Fields. Access Ribbon Image
    The Field List pane appears.
    Field List pane
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.
In the Field List pane, when you click the plus sign (+) next to a table name, you see a list of all the fields available in that table. To add a field to your table, drag the field that you want from the Field List pane to the table in Datasheet view.

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

  1. On the Datasheet tab, in the Fields & Columns group, click Add Existing Fields. Access Ribbon Image
    The Field List pane appears.
  2. Under Fields available in other tables, click the plus sign (+) next to a table name to display the list of fields in that table.
  3. Drag the field that you want from the Field List pane to the table that is open in Datasheet view.
  4. When the insertion line appears, drop the field into position. The Lookup Wizard starts.
  5. Follow the instructions to complete the Lookup Wizard. The field appears in the table 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. This relationship, created by Access, does not enforce referential integrity by default. To enforce referential integrity, you must edit the relationship.

Edit a table relationship

You change a table relationship by selecting it in the Relationships document tab and then editing it.
  1. Carefully position the cursor so that it points to the relationship line, and then click the line to select it. The relationship line appears thicker when it is selected.
  2. With the relationship line selected, double-click it. –or–
    On the Design tab, in the Tools group, click Edit Relationships.
The Edit Relationships dialog box appears.

Open the Edit Relationships dialog box

  1. Click the Microsoft Office Button Button image, and then click Open.
  2. In the Open dialog box, select and open the database.
  3. On the Database Tools tab, in the Show/Hide group, click Relationships. The Relationships document tab appears.
    If you have not yet defined any relationships and this is the first time you are opening the Relationships document tab, the Show Table dialog box appears. If the dialog box appears, click Close.
  4. On the Design tab, in the Relationships group, click All Relationships. Ribbon Design Tab Relationships Group
    All tables with relationships are displayed, showing relationship lines. 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.
    For more information about the Show Hidden Objects option,
  5. Click the relationship line for the relationship that you want to change. The relationship line appears thicker when it is selected.
  6. Double-click the relationship line. -or-
    On the Design tab, in the Tools group, click Edit Relationships.
    The Edit Relationships dialog box appears.
    Edit Relationships dialog box
  7. Make your changes, and then click OK. The Edit Relationships dialog box allows you to change a table relationship. Specifically, you can change the tables or queries on either side of the relationship, or the fields on either side. You can also set the join type, or enforce referential integrity and choose a cascade option.

Set the join type

When you define a table relationship, the facts about the relationship inform 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 selects the default matching fields based upon the fields specified in the relationship. You can override these initial default values in your query, but the values supplied by the relationship will often prove to be the correct ones. Because matching and bringing together data from more than one table is something you will do frequently in all but the most simple databases, setting defaults by creating relationships can be time saving and beneficial.
A multiple table query combines information from more than one table 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 specify for each relationship is the join type. The join type tells Access which records to include in 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 accomplish this, you have to change 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 dialog box, not the Relationships document tab.
You should think about the result you will most often 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 dialog box, click Join Type. The Join Properties dialog box appears.
  2. Click your choice, and then click OK.
The following table (using the Customers and Orders tables) shows the three choices that are displayed in the Join Properties dialog box, the type of join they use, and whether all rows or matching rows are returned for each table.
ChoiceRelational join Left table Right table
1. Only include rows where the joined fields from both tables are equal.Inner joinMatching rowsMatching rows
2. Include ALL records from 'Customers' and only those records from 'Orders' where the joined fields are equal.Left outer joinAll rowsMatching rows
3. Include ALL records from 'Orders' and only those records from 'Customers' where the joined fields are equal.Right outer joinMatching rowsAll rows

When you choose option 2 or option 3, an arrow is shown on the relationship line. This arrow points to the side of the relationship that shows only matching rows.

Make changes in the Join Properties dialog box

  1. Click the Microsoft Office Button Button image, and then click Open.
  2. In the Open dialog box, select and open the database.
  3. On the Database Tools tab, in the Show/Hide group, click Relationships. Access Ribbon Image
    The Relationships document tab appears.
    If you have not yet defined any relationships and this is the first time you are opening the Relationships document tab, the Show Table dialog box appears. If the dialog box appears, click Close.
  4. On the Design tab, in the Relationships group, click All Relationships. Ribbon Design Tab Relationships Group
    All tables that have relationships are displayed, showing relationship lines. 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.

  5. Click the relationship line for the relationship that you want to change. The relationship line appears thicker when it is selected.
  6. Double-click the relationship line. -or-
    On the Design tab, in the Tools group, click Edit Relationships.
    The Edit Relationships dialog box appears.
  7. Click Join Type
  8. In the Join Properties dialog box, click an option, and then click OK. Join Properties dialog box
  9. Make any additional changes to the relationship, and then click OK.

Enforce referential integrity

The purpose of referential integrity is to to prevent orphan records and to keep references synchronized so that you don't have any records that reference other records that no longer exist. You enforce referential integrity by enabling it for a table relationship. Once enforced, Access rejects any operation that would violate referential integrity for that table relationship. This means that Access will reject both updates that change the target of a reference and deletions that remove the target of a reference. To have Access propagate referential updates and deletions so that all related rows are changed accordingly

Turn referential integrity on or off

  1. Click the Microsoft Office Button Button image, and then click Open.
  2. In the Open dialog box, select and open the database.
  3. On the Database Tools tab, in the Show/Hide group, click Relationships. Access Ribbon Image
    The Relationships document tab appears.
    If you have not yet defined any relationships and this is the first time you are opening the Relationships document tab, the Show Table dialog box appears. If the dialog box appears, click Close.
  4. On the Design tab, in the Relationships group, click All Relationships. All tables with relationships are displayed, showing relationship lines. 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.

  5. Click the relationship line for the relationship that you want to change. The relationship line appears thicker when it is selected.
  6. Double-click the relationship line. -or-
    On the Design tab, in the Tools group, click Edit Relationships.
    The Edit Relationships dialog box appears.
  7. Check Enforce Referential Integrity.
  8. Make any additional changes to the relationship, and then click OK.
After you have enforced 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 check box.
  • 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 check box.
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 might encounter a situation where you have a valid need to change the value on the "one" side of a relationship. In such a case, you need Access to automatically update all of the affected rows as part of a single operation. That way the update is completed in full so that your database is not left in an inconsistent state — with some rows updated and some not. Access helps you avoid this problem by supporting the Cascade Update Related Fields option. 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.
You might also need 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. When you enforce referential integrity and select the Cascade Delete Related Records check box, Access automatically deletes all records that reference the primary key when you delete the record that contains the primary key.

Turn cascade update and/or cascade delete on or off

  1. Click the Microsoft Office Button Button image, and then click Open.
  2. In the Open dialog box, select and open the database.
  3. On the Database Tools tab, in the Show/Hide group, click Relationships.
    Access Ribbon Image
    The Relationships document tab appears.
    If you have not yet defined any relationships and this is the first time you are opening the Relationships document tab, the Show Table dialog box appears. If the dialog box appears, click Close.
  4. On the Design tab, in the Relationships group, click All Relationships.
    All tables that have relationships are displayed, showing relationship lines. 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.
  5. Click the relationship line for the relationship that you want to change. The relationship line appears thicker when it is selected.
  6. Double-click the relationship line.
    -or-
    On the Design tab, in the Tools group, click Edit Relationships.
    The Edit Relationships dialog box appears.
  7. Select the Enforce Referential Integrity check box.
  8. Select either the Cascade Update Related Fields or the Cascade Delete Related Records check box, or select both.
  9. Make any additional changes to the relationship, and then click OK.
Note If the primary key is an AutoNumber (AutoNumber data type: In a Microsoft Access database, a field data type that automatically stores a unique number for each record as it's added to a table. Three kinds of numbers can be generated: sequential, random, and Replication ID.) field, selecting the Cascade Update Related Fields check box will have no effect, because you cannot change the value in an AutoNumber field.

Delete a table relationship

To remove a table relationship, you must delete the relationship line in the Relationships document tab. Carefully 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. Note that when 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.
  1. Click the Microsoft Office Button Button image, and then click Open.
  2. In the Open dialog box, select and open the database.
  3. On the Database Tools tab, in the Show/Hide group, click Relationships.
    Access Ribbon Image
    The Relationships document tab appears.
    If you have not yet defined any relationships and this is the first time you are opening the Relationships document tab, the Show Table dialog box appears. If the dialog box appears, click Close.
  4. On the Design tab, in the Relationships group, click All Relationships.
    All tables that have relationships are displayed, showing relationship lines. 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.
  5. Click the relationship line for the relationship that you want to delete. The relationship line appears thicker when it is selected.
  6. Press the DELETE key.
    -or-
    Right-click and then click DELETE.
  7. Access might display the message Are you sure you want to permanently delete the selected relationship from your database?. If this confirmation message appears, click 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 close any open objects that employ these tables before you try to remove the relationship.

0 comments |

Labels

Blog Archive

Powered by Blogger.

I made these pages for me and my friends to help solving the problem we face regarding Computer & internet, if anyone wants me to answer a question or find out about some information please send me email and I will try to reply.*P.S. some of the article I wrote and the other I found on the internet I posted them in sprit of learning and shearing, please forgive me if you found something you don’t want to be in my blog, email me and I will delete them. Thank you for your interest in my pages.امل نجم Amal Nagm

banner 1 banner 2