Sunteți pe pagina 1din 16

1

Guide to table relationships


One of the goals of good database design is to remove data redundancy (duplicate data). To achieve that goal, you divide your data into many subject-based tables so that each fact is represented only once. You then provide Microsoft Office ccess !""# $ith the means by $hich to bring the divided information bac% together & you do this by placing common fields in tables that are related. To do this step correctly, ho$ever, you must first understand the relationships bet$een your tables, and then specify these relationships in your Office ccess !""# database. 'or more information, see the article (atabase design basics. )n this article )ntroduction Types of table relationships *hy create table relationships+ ,nderstanding referential integrity -ie$ table relationships .reate a table relationship (elete a table relationship .hange a table relationship /nforce referential integrity

Introduction
fter you have created a table for each subject in your database, you must provide Office ccess !""# $ith the means by $hich to bring that information bac% together again $hen needed. You do this by placing common fields in tables that are related, and by defining relationships bet$een your tables. You can then create 0ueries, forms, and reports that display information from several tables at once. 'or e1ample, the form sho$n here includes information dra$n from several tables2

)nformation in this form comes from the .ustomers table... ...the Orders table... ...the 3roducts table... ...and the Order (etails table. The customer name in the Bill To bo1 is retrieved from the .ustomers table, the Order )( and the Order (ate values come from the Orders table, the 3roduct name comes from the 3roducts table, and the ,nit 3rice and 4uantity values come from the Order (etails table. These tables are lin%ed to each other in a variety of $ays to bring information from each into the form.

2 )n the preceding e1ample, the fields in the tables must be coordinated so that they sho$ information about the same order. This coordination is accomplished by using table relationships. table relationship $or%s by matching data in %ey fields & often a field $ith the same name in both tables. )n most cases, these matching fields are the primary key from one table, $hich provides a uni0ue identifier for each record, and a foreign key in the other table. 'or e1ample, employees can be associated $ith orders for $hich they are responsible by creating a table relationship bet$een the /mployee)( fields in the /mployees and the Orders tables.

/mployee)( appears in both tables & as a primary %ey ... ... and as a foreign %ey. Top of 3age

Types of table relationships


There are three types of table relationships.

one-to-many relationship

.onsider an order trac%ing database that includes a .ustomers table and an Orders table. customer can place any number of orders. )t follo$s that for any customer represented in the .ustomers table, there can be many orders represented in the Orders table. The relationship bet$een the .ustomers table and the Orders table is, therefore, a one-to-many relationship. To represent a one-to-many relationship in your database design, ta%e the primary %ey on the 5one5 side of the relationship and add it as an additional field or fields to the table on the 5many5 side of the relationship. )n this case, for e1ample, you add a ne$ field & the )( field from the .ustomers table & to the Orders table and name it .ustomer )(. ccess can then use the .ustomer )( number in the Orders table to locate the correct customer for each order.

many-to-many relationship

.onsider the relationship bet$een a 3roducts table and an Orders table. single order can include more than one product. On the other hand, a single product can appear on many orders. Therefore, for each record in the Orders table, there can be many records in the 3roducts table. )n addition, for each record in the 3roducts table, there can be many records in the Orders table. This type of relationship is called a many-tomany relationship because, for any product, there can be many orders and, for any order, there can be many products. 6ote that to detect e1isting many-to-many relationships bet$een your tables, it is important that you consider both sides of the relationship. To represent a many-to-many relationship, you must create a third table, often called a junction table, that brea%s do$n the many-to-many relationship into t$o one-to-many relationships. You insert the primary %ey from each of the t$o tables into the third table. s a result, the third table records each occurrence, or

3 instance, of the relationship. 'or e1ample, the Orders table and the 3roducts table have a many-to-many relationship that is defined by creating t$o one-to-many relationships to the Order (etails table. One order can have many products, and each product can appear on many orders.

one-to-one relationship

)n 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 type of relationship is not common because, most often, the information related in this $ay is stored in the same table. You might use a one-to-one relationship to divide a table $ith 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. *hen you do identify such a relationship, both tables must share a common field. Top of 3age

Why create table relationships?


You can create table relationships e1plicitly by using the 7elationships $indo$, or by dragging a field from the Field ist pane. Office ccess !""# uses table relationships to ho$ to join tables $hen you need to use them in a database object. There are several reasons $hy you should create table relationships before you create other database objects, such as forms, 0ueries and reports.

Table relationships inform your 0uery designs

To $or% $ith records from more than one table, you often must create a 0uery that joins the tables. The 0uery $or%s by matching the values in the primary %ey field of the first table $ith a foreign %ey field in the second table. 'or e1ample, to return ro$s that list all of the orders for each customer, you construct a 0uery that joins the .ustomers table $ith the Orders table based on the .ustomer )( field. )n the 7elationships $indo$, you can manually specify the fields to join. 8o$ever, if you already have a relationship defined bet$een the tables, Office ccess !""# supplies the default join, based on e1isting the table relationship. )n addition, if you use one of the 0uery $i9ards, ccess uses the information it gathers from the table relationships you have already defined to present you $ith informed choices and to prepopulate property settings $ith appropriate default values.

Table relationships inform your form and report designs

*hen you design a form or report, Office ccess !""# uses the information it gathers from the table relationships you have already defined to present you $ith informed choices and to prepopulate property settings $ith appropriate default values.

Table relationships are the foundation upon $hich you can enforce referential integrity to help prevent orphan records in your database. n orphan record is a record $ith a reference to another record that does not e1ist & for e1ample, an order record that references a customer record that does not e1ist.

*hen you design a database, you divide your information into tables, each of $hich has a primary %ey. You then add foreign %eys to related tables that reference those primary %eys. These foreign %ey-primary %ey pairings form the basis for table relationships and multi-table 0ueries. )t is important, therefore, that these

! foreign %ey-primary %ey references stay synchroni9ed. 7eferential integrity helps ensure that references stay synchroni9ed and is dependent upon table relationships. Top of 3age

"nderstanding referential integrity


*hen you design a database, you divide your information into many subject-based tables to minimi9e data redundancy. You then provide Office ccess !""# $ith the means by $hich to bring the data bac% together by placing common fields into related tables. 'or e1ample, to represent a one-to-many relationship you ta%e the primary %ey from the 5one5 table and add it as an additional field to the 5many5 table. To bring the data bac% together, ccess ta%es the value in the 5many5 table and loo%s up the corresponding value in the 5one5 table. )n this $ay the values in the 5many5 table reference the corresponding values in the 5one5 table. :uppose you have a one-to-many relationship bet$een :hippers and Orders and you $ant to delete a :hipper. )f the shipper you $ant to delete has orders in the Orders table, those orders $ill become 5orphans5 $hen you delete the :hipper record. The orders $ill still contain a shipper )(, but the )( $ill no longer be valid, because the record that it references no longer e1ists. The purpose of referential integrity is to to prevent orphans and %eep references in sync so that this hypothetical situation never occurs. You enforce referential integrity by enabling it for a table relationship (see /nforce referential integrity for step-by-step instructions). Once enforced, ccess rejects any operation that $ould violate referential integrity for that table relationship. This means ccess $ill reject both updates that change the target of a reference, and deletions that remove the target of a reference. 8o$ever, it is possible you might have a perfectly valid need to change the primary %ey for a shipper that has orders in the Orders table. 'or such cases, $hat you really need is for ccess to automatically update all the effected ro$s as part of a single operation. That $ay, ccess ensures that the update is completed in full so that your database is not left in an inconsistent state, $ith some ro$s updated and some not. 'or this reason ccess supports the .ascade ,pdate 7elated 'ields option. *hen you enforce referential integrity and choose the .ascade ,pdate 7elated 'ields option, and you then update a primary %ey, ccess automatically updates all fields that reference the primary %ey. )t;s also possible you might have a valid need to delete a ro$ and all related records & for e1ample, a :hipper record and all related orders for that shipper. 'or this reason, ccess supports the .ascade (elete 7elated 7ecords option. *hen you enforce referential integrity and choose the .ascade (elete 7elated 7ecords option, and you then delete a record on the primary %ey side of the relationship, ccess automatically deletes all records that reference the primary %ey. Top of 3age

#ie$ table relationships


To vie$ your table relationships, clic% %elationships on the &atabase Tools tab. The 7elationships $indo$ opens and displays any e1isting relationships. )f no table relationships have yet been defined and you are opening the 7elationships $indo$ for the first time, ccess prompts you to add a table or 0uery to the $indo$.

' (pen the %elationships $indo$ <. .lic% the )icrosoft (ffice Button , and then clic% (pen.

!. )n the (pen dialog bo1, select and open the database. =. On the &atabase Tools tab, in the *ho$+,ide group, clic% %elationships. >. )f the database contains relationships, the 7elationships $indo$ appears. )f the database does not contain any relationships and you are opening the 7elationships $indo$ for the first time, the *ho$ Table dialog bo1 appears. .lic% -lose to close the dialog bo1. ?. On the &esign tab, in the %elationships group, clic% .ll %elationships. This displays all of the defined relationships in your database. 6ote that hidden tables (tables for $hich the ,idden chec% bo1 in the table;s /roperties dialog bo1 is selected) and their relationships $ill not be sho$n unless the *ho$ ,idden (b0ects chec% bo1 is selected in the 1a2igation (ptions dialog bo1. 'or more information about the :ho$ 8idden Objects option, see the article @uide to the 6avigation 3ane.

The primary %ey This line represents the relationship The foreign %ey table relationship is represented by a relationship line dra$n bet$een tables in the 7elationships $indo$. relationship that does not enforce referential integrity appears as a thin line bet$een the common fields supporting the relationship. *hen you select the relationship by clic%ing its line, the line thic%ens to indicate it is selected. )f you enforce referential integrity for this relationship, the line appears thic%er at each end. )n addition, the number 1 appears over the thic% portion of the line on one side of the relationship, and the infinity symbol (3) appears over the thic% portion of the line on the other side. *hen the 7elationships $indo$ is active, you can select from the follo$ing commands on the 7ibbon, part of the Microsoft Office 'luent user interface2 On the &esign tab, in the Tools group2

4dit %elationships Opens the 4dit %elationships dialog bo1. *hen you select a relationship line, you can clic% 4dit %elationships to change the table relationship. You can also double-clic% the relationship line.

-lear ayout 7emoves all tables and relationships from display in the 7elationships $indo$. 6ote that this command only hides the tables and relationships & it does not delete them. %elationships %eport .reates a report that displays the tables and relationships in your database. The report sho$s only the tables and relationships that are not hidden in the 7elationships $indo$.

On the &esign tab, in the %elationships group2


*ho$ Table Opens the *ho$ Table dialog bo1 so that you can select tables and 0ueries for vie$ing in the 7elationships $indo$. ,ide Table 8ides the selected table in the 7elationships $indo$. &irect %elationships (isplays all relationships and related tables for the selected table in the 7elationships $indo$, if they are not already displayed. .ll %elationships (isplays all of the relationships and related tables in your database in the 7elationships $indo$. 6ote that hidden tables (tables for $hich the ,idden chec% bo1 in the table;s /roperties dialog bo1 is selected) and their relationships $ill not be sho$n unless :ho$ 8idden Objects is selected in the 6avigation Options dialog bo1.

'or more information about the :ho$ 8idden Tables option, see the article @uide to the 6avigation 3ane.

-lose .loses the 7elationships $indo$. )f you made any changes to the layout of the 7elationships $indo$, you are as%ed $hether to save those changes.

Top of 3age

-reate a table relationship


You can create a table relationship by using the 7elationships $indo$, or by dragging a field onto a datasheet from the Field ist pane. *hen you create a relationship bet$een tables, the common fields are not re0uired to have the same names, although it is often the case that they do. 7ather, those fields must have the same data type. )f the primary %ey field is an uto6umber field, ho$ever, the foreign %ey field can be a 6umber field if the Field*i6e property of both fields is the same. 'or e1ample, you can match an uto6umber field and a 6umber field if the Field*i6e property of both fields is Aong )nteger. *hen both common fields are 6umber fields, they must have the same Field*i6e property setting.

-reate a table relationship by using the %elationships $indo$


<. .lic% the )icrosoft (ffice Button , and then clic% (pen.

!. )n the (pen dialog bo1, select and open the database. =. On the &atabase Tools tab, in the *ho$+,ide group, clic% %elationships. >. )f you have not yet defined any relationships, the *ho$ Table dialog bo1 automatically appears. )f it does not appear, on the &esign tab, in the %elationships group, clic% *ho$ Table. The *ho$ Table dialog bo1 displays all of the tables and 0ueries in the database. To see only tables, clic% Tables. To see only 0ueries, clic% 7ueries. To see both tables and 0ueries, clic% Both.

8 ?. :elect one or more tables or 0ueries and then clic% .dd. *hen you have finished adding tables and 0ueries to the 7elationships $indo$, clic% -lose. B. (rag a field (typically the primary %ey) from one table to the common field (the foreign %ey) in the other table. To drag multiple fields, press the .T7A %ey, clic% each field, and then drag them. The 4dit %elationships dialog bo1 appears. #. -erify that the field names sho$n are the common fields for the relationship. )f a field name is incorrect, clic% the field name and select a ne$ field from the list. To enforce referential integrity for this relationship, select the 4nforce %eferential Integrity chec% bo1. 'or more information about referential integrity, see the ,nderstanding 7eferential )ntegrity and the /nforce 7eferential )ntegrity sections. C. .lic% -reate. The relationship line is dra$n bet$een the t$o tables. )f you selected the 4nforce %eferential Integrity chec% bo1, the line appears thic%er at each end. )n addition, again only if you selected the 4nforce %eferential Integrity chec% bo1, the number 1 appears over the thic% portion of the line on one side of the relationship, and the infinity symbol (3) appears over the thic% portion of the line on the other side.

1otes

To create a one9to9one relationship Doth of the common fields (usually the primary %ey and foreign %ey fields) must have a uni0ue inde1. This means the Inde:ed property for these fields should be set to ;es <1o &uplicates=. )f both fields have a uni0ue inde1, ccess creates a one-toone relationship. To create a one9to9many relationship The field on the 5one5 side (typically the primary %ey) of the relationship must have a uni0ue inde1. This means the Inde:ed property for this field should be set to ;es <1o &uplicates=. The field on the 5many5 side should not have a uni0ue inde1. )t can have an inde1, but it must allo$ duplicates. This means the Inde:ed property for this field should be set to either 1o, or ;es <&uplicates (>=. *hen one field has a uni0ue inde1 and the other does not, ccess creates a one-to-many relationship.

-reate a table relationship by using the Field ist pane

? )n Office ccess !""#, you can add a field to an e1isting table that is open in (atasheet vie$ by dragging it from the Field ist pane. The Field ist pane sho$s fields available in related tables and also fields available in other tables. *hen you drag a field from an 5other5 (unrelated) table and then complete the Aoo%up *i9ard, a ne$ one-to-many relationship is automatically created bet$een the table in the Field ist pane and the table to $hich you dragged the field. This relationship, created by ccess, does not enforce referential integrity by default. To enforce referential integrity, you must edit the relationship. :ee the section .hange a table relationship for more information. (pen a table in &atasheet 2ie$ <. .lic% the )icrosoft (ffice Button , and then clic% (pen.

!. )n the (pen dialog bo1, select and open the database. =. )n the 6avigation pane, right-clic% the table to $hich you $ant to add the field and create the relationship, and clic% &atasheet #ie$ on the shortcut menu. (pen the Field ist pane

On the &atasheet tab, in the Fields @ -olumns group, clic% .dd 4:isting Fields.

The Field ist pane appears. The Field ist pane sho$s all of the other tables in your database, grouped into categories. *hen you $or% $ith a table in (atasheet vie$, ccess displays fields in either of t$o categories in the Field ist pane2 Fields a2ailable in related tables and Fields a2ailable in other tables. The first category lists all of the tables that have a relationship $ith the table you are currently $or%ing $ith. The second category lists all of the tables $ith $hich your table does not have a relationship. )n the Field ist pane, $hen you clic% the plus sign (A) ne1t 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 $ant from the Field ist pane to the table in (atasheet vie$. .dd a field and create a relationship from the Field ist pane <. On the &atasheet tab, in the Fields @ -olumns group, clic% .dd 4:isting Fields.

The Field ist pane appears.

B <. ,nder Fields a2ailable in other tables, clic% the plus sign (A) ne1t to a table name to display the list of fields in that table. !. (rag the field that you $ant from the Field ist pane to the table that is open in (atasheet vie$. =. *hen the insertion line appears, drop the field in position. The ookup Wi6ard starts. >. 'ollo$ the instructions to complete the ookup Wi6ard. The field appears in the table in (atasheet vie$. *hen you drag a field from an 5other5 (unrelated) table and then complete the Aoo%up *i9ard, a ne$ oneto-many relationship is automatically created bet$een the table in the Field ist and the table to $hich you dragged the field. This relationship, created by ccess, does not enforce referential integrity by default. To enforce referential integrity, you must edit the relationship. :ee the section .hange a table relationship for more information. Top of 3age

&elete a table relationship


To remove a table relationship, you must delete the relationship line in the 7elationships $indo$. .arefully position the cursor so that it points at the relationship line, and then clic% the line. The relationship line appears thic%er $hen it is selected. *ith the relationship line selected, press (/A/T/. 6ote that $hen you remove a relationship, you also remove referential integrity support for that relationship, if it is enabled. s a result, ccess $ill no longer automatically prevent the creation of orphan records on the 5many5 side of a relationship. <. .lic% the )icrosoft (ffice Button , and then clic% (pen.

!. )n the (pen dialog bo1, select and open the database. =. On the &atabase Tools tab, in the *ho$+,ide group, clic% %elationships. The 7elationships $indo$ appears. )f you have not yet defined any relationships and this is the first time you are opening the 7elationships $indo$, the *ho$ Table dialog bo1 appears. )f the dialog bo1 appears, clic% -lose. >. On the &esign tab, in the %elationships group, clic% .ll %elationships. ll tables that have relationships are displayed, sho$ing relationship lines. ?. .lic% the relationship line for the relationship that you $ant to delete. The relationship line appears thic%er $hen it is selected. B. 3ress the (/A/T/ %ey. EorE 7ight-clic% and then clic% &elete.

1C #. ccess might display the message .re you sure you $ant to permanently delete the selected relationship from your database?. )f this confirmation message appears, clic% ;es.

1ote )f 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 $ill not be able to delete the relationship. You must first close any open objects that use these tables before you can remove the relationship. Top of 3age

-hange a table relationship


You change a table relationship by selecting it in the 7elationships $indo$ and then editing it. .arefully position the cursor so that it points at the relationship line, and then clic% the line to select it. The relationship line appears thic%er $hen it is selected. *ith the relationship line selected, double-clic% it or clic% 4dit %elationships in the Tools group on the &esign tab. The 4dit %elationships dialog bo1 appears. )ake your changes in the 4dit %elationships dialog bo: <. .lic% the )icrosoft (ffice Button , and then clic% (pen.

!. )n the (pen dialog bo1, select and open the database. =. On the &atabase Tools tab, in the *ho$+,ide group, clic% %elationships. The 7elationships $indo$ appears. )f you have not yet defined any relationships and this is the first time you are opening the 7elationships $indo$, the *ho$ Table dialog bo1 appears. )f the dialog bo1 appears, clic% -lose. >. On the &esign tab, in the %elationships group, clic% .ll %elationships. ll tables that have relationships are displayed, sho$ing relationship lines. 6ote that hidden tables (tables for $hich the ,idden chec% bo1 in the table;s /roperties dialog bo1 is selected) and their relationships $ill not be sho$n unless :ho$ 8idden Objects is selected in the 1a2igation (ptions dialog bo1. 'or more information about the :ho$ 8idden Objects option, see the article @uide to the 6avigation 3ane. ?. .lic% the relationship line for the relationship that you $ant to change. The relationship line appears thic%er $hen it is selected. B. (ouble-clic% the relationship line. EorE On the &esign tab, in the Tools group, clic% 4dit %elationships. The 4dit %elationships dialog bo1 appears.

11

#. Ma%e your changes, and then clic% (>. The 4dit %elationships dialog bo1 allo$s you to change a table relationship. :pecifically, you can change the tables or 0ueries 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. 'or more information about the join type and ho$ to set it, see the section :et the join type. 'or more information about ho$ to enforce referential integrity and choose a cascade option, see the section /nforce referential integrity.

*et the 0oin type


*hen you define a table relationship, the facts about the relationship inform your 0uery designs. 'or e1ample, if you define a relationship bet$een t$o tables, and you then create a 0uery that employs those tables, ccess automatically selects the default matching fields based upon the fields specified in the relationship. You can override these initial default values in your 0uery, but the values supplied by the relationship $ill often prove to be the correct ones. Decause matching and bringing together data from more than one table is something you $ill do fre0uently in all but the most simple databases, setting defaults by creating relationships can be time saving and beneficial. multiple table 0uery 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. 'or instance, suppose you $ant to display customer orders. You create a 0uery that joins the .ustomers table and the Orders table on the .ustomer )( field. The 0uery result contains customer information and order information for only those ro$s $here a corresponding match $as found. One of the values you can specify for each relationship is the join type. The join type tells ccess $hich records to include in a 0uery result. 'or e1ample, consider again a 0uery that joins the .ustomers table and the Orders table on the common fields that represent the .ustomer )(. ,sing the default join type (called an inner join), the 0uery returns only the .ustomer ro$s and the Order ro$s $here the common fields (also called the joined fields) are e0ual. 8o$ever, suppose you $ant to include all .ustomers & even those $ho have not yet placed any orders. To accomplish this, you must change the join type from an inner join to $hat is called a left outer join. left outer join returns all of the ro$s from the table on the left side of the relationship and only those that match from the table on the right. right outer join returns all of the ro$s on the right and only those that match on the left.

12 1ote )n this case, 5left5 and 5right5 refer to the position of the tables in the 4dit %elationships dialog bo1, not the 7elationships $indo$. You should thin% about the result you $ill most often $ant from a 0uery that joins the tables in this relationship, and then set the join type accordingly. *et the 0oin type <. )n the 4dit %elationships dialog bo1, clic% Doin Type. The Doin /roperties dialog bo1 appears. !. .lic% your choice, and then clic% (>. The follo$ing table (using the .ustomers and Orders tables) sho$s the three choices that are displayed in the Doin /roperties dialog bo1, the type of join they use, and $hether all ro$s or matching ro$s are included for each table. -hoice <. Only include ro$s $here the joined fields from both tables are e0ual. !. )nclude AA records from ;.ustomers; and only those records from ;Orders; $here the joined fields are e0ual. =. )nclude AA records from ;Orders; and only those records from ;.ustomers; $here the joined fields are e0ual. %elational 0oin )nner join Aeft outer join 7ight outer join eft table %ight table Matching ro$s ll ro$s Matching ro$s Matching ro$s Matching ro$s ll ro$s

*hen you choose option ! or option =, an arro$ is sho$n on the relationship line. This arro$ points to the side of the relationship that sho$s only matching ro$s.

)ake changes in the Doin /roperties dialog bo:


<. .lic% the )icrosoft (ffice Button , and then clic% (pen.

!. )n the (pen dialog bo1, select and open the database. =. On the &atabase Tools tab, in the *ho$+,ide group, clic% %elationships. The 7elationships $indo$ appears. )f you have not yet defined any relationships and this is the first time you are opening the 7elationships $indo$, the *ho$ Table dialog bo1 appears. )f the dialog bo1 appears, clic% -lose. >. On the &esign tab, in the %elationships group, clic% .ll %elationships. ll tables that have relationships are displayed, sho$ing relationship lines. 6ote that hidden tables (tables for $hich the ,idden chec% bo1 in the table;s /roperties dialog bo1 is selected) and their relationships $ill not be sho$n unless :ho$ 8idden Objects is selected in the 6avigation Options dialog bo1. 'or more information about the :ho$ 8idden Objects option, see the article @uide to the 6avigation 3ane.

13 ?. .lic% the relationship line for the relationship that you $ant to change. The relationship line appears thic%er $hen it is selected. B. (ouble-clic% the relationship line. EorE On the &esign tab, in the Tools group, clic% 4dit %elationships. The 4dit %elationships dialog bo1 appears. #. .lic% Doin Type C. )n the Doin /roperties dialog bo1, clic% an option, and then clic% (>.

F. Ma%e any additional changes to the relationship, and then clic% (>. Top of 3age

4nforce referential integrity


The purpose of using referential integrity is to to prevent orphan records and to %eep references synchroni9ed so that you don;t have any records that reference other records that no longer e1ist. You enforce referential integrity by enabling it for a table relationship. Once enforced, ccess rejects any operation that $ould violate referential integrity for that table relationship. ccess rejects updates that change the target of a reference, and also deletions that remove the target of a reference. To have ccess propagate referential updates and deletions so that all related ro$s are changed accordingly, see the section :et the cascade options.

Turn referential integrity on or off


<. .lic% the )icrosoft (ffice Button , and then clic% (pen.

!. )n the (pen dialog bo1, select and open the database. =. On the &atabase Tools tab, in the *ho$+,ide group, clic% %elationships. The 7elationships $indo$ appears. )f you have not yet defined any relationships and this is the first time you are opening the 7elationships $indo$, the *ho$ Table dialog bo1 appears. )f the dialog bo1 appears, clic% -lose.

1! >. On the &esign tab, in the %elationships group, clic% .ll %elationships. ll tables that have relationships are displayed, sho$ing relationship lines. 6ote that hidden tables (tables for $hich the ,idden chec% bo1 in the table;s /roperties dialog bo1 is selected) and their relationships $ill not be sho$n unless :ho$ 8idden Objects is selected in the 1a2igation (ptions dialog bo1. 'or more information about the :ho$ 8idden Objects option, see the article @uide to the 6avigation 3ane. ?. .lic% the relationship line for the relationship that you $ant to change. The relationship line appears thic%er $hen it is selected. B. (ouble-clic% the relationship line. EorE On the &esign tab, in the Tools group, clic% 4dit %elationships. The 4dit %elationships dialog bo1 appears. #. :elect the 4nforce %eferential Integrity chec% bo1. C. Ma%e any additional changes to the relationship, and then clic% (>. fter you have enforced referential integrity, the follo$ing rules apply2

You cannot enter a value in the foreign %ey field of a related table if that value doesn;t e1ist in the primary %ey field of the primary table & doing so creates orphan records. You cannot delete a record from a primary table if matching records e1ist in a related table. 'or e1ample, you cannot delete an employee record from the /mployees table if there are orders assigned to that employee in the Orders table. You can, ho$ever, choose to delete a primary record and all related records in one operation by selecting the -ascade &elete %elated %ecords chec% bo1. You cannot change a primary %ey value in the primary table if doing so $ould create orphan records. 'or e1ample, you cannot change an order number in the Orders table if there are line items assigned to that Order in the Order (etails table. You can, ho$ever, choose to update a primary record and all related records in one operation by selecting the -ascade "pdate %elated Fields chec% bo1.

1otes )f you have difficulty enabling referential integrity, note that the follo$ing conditions are re0uired in order to enforce referential integrity2

The common field from the primary table must be a primary %ey or have a uni0ue inde1. The common fields must have the same data type. The one e1ception is that an uto6umber field can be related to a 6umber field that has a Field*i6e property setting of ong Integer. Doth tables must e1ist in the same ccess database. 7eferential integrity cannot be enforced on lin%ed tables. 8o$ever, if the source tables are in ccess format, you can open the database in $hich they are stored and enable referential integrity in that database.

*et the cascade options

1' You might encounter a situation in $hich you have a valid need to change the value on the 5one5 side of a relationship. )n such a case, you need ccess to automatically update all of the effected ro$s as part of a single operation. That $ay, the update is completed in full so that your database is not left in an inconsistent state & $ith some ro$s updated and some not. ccess helps you avoid this problem by supporting the .ascade ,pdate 7elated 'ields option. *hen you enforce referential integrity and choose the .ascade ,pdate 7elated 'ields option, and you then update a primary %ey, ccess automatically updates all fields that reference the primary %ey. You might also need to delete a ro$ and all related records & for instance, a shipper record and all related orders for that shipper. 'or this reason, ccess supports the .ascade (elete 7elated 7ecords option. *hen you enforce referential integrity and choose the .ascade (elete 7elated 7ecords option, ccess automatically deletes all records that reference the primary %ey $hen you delete the record that contains the primary %ey.

Turn cascade update and+or cascade delete on or off


<. .lic% the )icrosoft (ffice Button , and then clic% (pen.

!. )n the (pen dialog bo1, select and open the database. =. On the &atabase Tools tab, in the *ho$+,ide group, clic% %elationships. The 7elationships $indo$ appears. )f you have not yet defined any relationships and this is the first time you are opening the 7elationships $indo$, the *ho$ Table dialog bo1 appears. )f the dialog bo1 appears, clic% -lose. >. On the &esign tab, in the %elationships group, clic% .ll %elationships. ll tables that have relationships are displayed, sho$ing relationship lines. 6ote that hidden tables (tables for $hich the ,idden chec% bo1 in the table;s /roperties dialog bo1 is selected) and their relationships $ill not be sho$n unless :ho$ 8idden Objects is selected in the 1a2igation (ptions dialog bo1. 'or more information about the :ho$ 8idden Objects option, see @uide to the 6avigation 3ane. ?. .lic% the relationship line for the relationship that you $ant to change. The relationship line appears thic%er $hen it is selected. B. (ouble-clic% the relationship line. EorE On the &esign tab, in the Tools group, clic% 4dit %elationships. The 4dit %elationships dialog bo1 appears. #. :elect the 4nforce %eferential Integrity chec% bo1. C. :elect either the -ascade "pdate %elated Fields or -ascade &elete %elated %ecords chec% bo1, or select both. F. Ma%e any additional changes to the relationship, and then clic% (>.

15 1ote )f the primary %ey is an uto6umber field, selecting the -ascade "pdate %elated Fields chec% bo1 $ill have no effect, because you cannot change the value in an uto6umber field. Top of 3age

S-ar putea să vă placă și