Beruflich Dokumente
Kultur Dokumente
When one table has a foreign key to another table, the concept of referential integrity states that you may not add a record to the table that contains the foreign key unless there is a corresponding record in the linked table. It also includes the techniques known as cascading update and cascading delete, which ensure that changes made to the linked table are reflected in the primary table. Consider the situation where we have two tables: Employees and Managers. The Employees table has a foreign key attribute entitled Managed By which points to the record for that employees manager in the Managers table. Referential integrity enforces the following three rules:
1.
We may not add a record to the Employees table unless the Managed By attribute poi
nts to a valid record in the Managers table. 2. If the primary key for a record in the Managers table changes, all corresponding records in the Employees table must be modified using a cascading update. 3. If a record in the Managers table is deleted, all corresponding records in the Employees table must be deleted using a cascading delete.
Referential Integrity
Advantage Concepts Referential Integrity (RI) is the means by which primary/foreign key relationships are enforced in a database. By specifying RI rules you can have the database guarantee, for example, that every sales representative is assigned to a valid office. Through the use of RI constraints, many business rules can be enforced by the database server, instead of your application. The terms "primary key" and "foreign key" are used throughout this documentation. Primary Key - A unique identifier for a table. A column or column combination with the property that, at any given time, no two rows of the table contain the same value in that column or column combination. Foreign Key - A foreign key is a column or combination of columns whose values match the primary key of some other table. A foreign key does not have to be unique; in fact, foreign keys are often in a many-to-one relationship to a primary key. Foreign key values should be copies of the primary key values; no value in the foreign key except NULL should ever exist unless the same value exists in the primary key. A foreign key may be NULL; if any part of a composite foreign key is NULL, the entire foreign key is NULL.
Referential Integrity rules are stored in an Advantage Data Dictionary. Note Referential Integrity is only supported with the Advantage proprietary tables and Visual FoxPro (VFP) tables. When using RI with VFP tables, you should add "!DELETED()" conditions to your primary and foreign keys. This will make sure that cascades are triggered when records are deleted. Without the !DELETED() clause, the key value will not change when the record is deleted, and RI operation will not be triggered.
Example
Lets look at a simple example using two tables: SALES_REPS and OFFICES. The following SQL statement is syntactically correct, and with the current state of our example database this statement would execute and add a new sales rep, "Doug Henry", who works in office number 45: INSERT INTO SALES_REPS (EMPL_NUM, NAME, REP_OFFICE)
VALUES (69, Doug Henry, 45) No validity checking has been enforced, and even if office number 45 does not exist in the OFFICES table, Doug Henry will still exist in our database. To remedy this situation, well define one RI rule linking the OFFICES.OFFICE field (as the primary key) to the SALES_REPS.REP_OFFICE field (as the foreign key). With this rule in place, the previous SQL statement would not execute without returning an error. Before adding Doug to the SALES_REPS table, the Advantage Database Server will first ensure that all foreign keys in this new row reference existing primary keys in their parent tables. Because office number 45 does not exist, the INSERT operation will fail. The application developer does not write any code to enforce this rule. The database server does all the work; the developer can simply catch this error, notify the user of the violation, and request correct data.
Update Rules
SET_DEFAULT - When a primary key is updated, automatically set all foreign key values to their default values. See Advantage Data Dictionary for more information on default field values. If applied to our example above, this rule would assign sales representatives to some default office if their office number were ever updated. The default office is stored within the data dictionary and is the default field value for the office field.
NULL Values
Advantage primary keys can contain one NULL value. Advantage foreign keys (as long as they are not defined with the unique index type) can contain multiple NULL values. NULL values in foreign keys are often a necessity when dealing with updates to a database that utilizes RI constraints to break a dependency between primary and foreign keys.
Server Configuration
There is no additional server configuration related to the use of referential integrity. Note, though, that the Advantage Database Server will use more work areas when enforcing referential integrity rules. Also, cascading deletes may often need to acquire a large number of record locks (depending on the number of records affected by the cascade) before performing the actual cascade operation. Beginning with v9.0, Advantage will automatically increase the configuration sizes as necessary, so you should not need to adjust any parameters. For more information on configuration parameters, see Advantage Database Server Configuration