![]() |
Foreign Keys |
Introduction |
We stated earlier that the essence of a relational database was to get the information to flow from one list to another. To make this possible, a relationship must be created between both tables. After creating a primary key on the first table, to prepare the other table for the relationship, it must have a column that would hold and manage this relationship. This column would be used to "represent" the records of the first table. For this reason, such a column is referred to as a foreign key. To create a foreign key, on the table that would receive the data, add a column that holds the same data type as the column of the primary key of the first table. Also, the name of the foreign key is usually the same as the name of the column that holds the primary key in the other table. |
Creating a column that has the same name and data type as its corresponding primary key is the traditional technique of adding a foreign key to a table. Microsoft Access provides another technique that assists you with configuring the relationship between the primary key from the original table and the foreign key from the new table. To use it, after creating a column, set its Data Type to Lookup Wizard. A wizard would start where you can select the table that holds the primary key and the column that would show the desired value of the first table.
A valuable utility you can use to create and manage the relationships of your tables is the Relationships window. Before using it, you should have created the necessary tables of your database. You also should have created the primary and foreign keys of the tables whose relationships you want to configure. To access the Relationships window, when displaying the Database window, on the Database toolbar, you can click the Relationships button. Depending on your database, a Show Table may appear with the list of tables of your database. The Show Table dialog box may not appear too. In this case, to display it, you can right-click the window and click Show Table... From the Show Table dialog box, to add a table to the window, click it and click Add. After adding the tables, click Close. When adding the tables to the Relationships window, if a relationship had already been created between the tables, the Relationship window would recognize it and create a link between them: If a necessary relationship doesn't show between two tables, to create one between a primary key of a table and its corresponding foreign key in the other table, drag the primary key column and drop it on the foreign key of the other table. The Edit Relationship dialog box would come up. You can examine it to make sure that the right columns have been selected: If the columns are the valid ones, you can click OK. If they are not, you can click the arrow of each combo box in the lists and select the desired columns, then click OK. After creating a relationship, to examine, configure, or change it, you can double-click the line that represents a link between two tables, or you can right-click that line and click Edit Relationship.
|
Relationships and Data Integrity |
As mentioned in previous sections, relationships allow information to flow from one list, the parent table, to another list, the child table. When maintaining records, sometimes a piece of information may become obsolete. An employee may decide to change or to delete data from the parent table. This would cause the record in the child table to become orphan. When this happens, you need to take appropriate actions. Referential integrity is the ability to take care of necessary details when data from a table gets changed or deleted. When a piece of information is changed in a parent table, you need to make sure that the change is replicated to the related child table. If you are creating or troubleshooting a relationship in the Relationships window, after displaying the Edit Relationship dialog box for a particular relationship, you can click the Enforce Referential Integrity check box. This makes available two other check boxes: Cascade Update Related Fields and Cascade Delete Related Fields. |
|
|
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|