Integrity Rules
The relational model defines several integrity rules that, while not part of the definition of the Normal Forms are nonetheless a necessary part of any relational database. There are two types of integrity rules: general and database-specific.
General Integrity Rules
The relational model specifies two general integrity rules. They are referred to as general rules, because they apply to all databases. They are: entity integrity and referential integrity.
The entity integrity rule is very simple. It says that primary keys cannot contain null (missing) data. The reason for this rule should be obvious. You can’t uniquely identify or reference a row in a table, if the primary key of that table can be null. It’s important to note that this rule applies to both simple and composite keys. For composite keys, none of the individual columns can be null. Fortunately, Microsoft Access automatically enforces the entity integrity rule for you. No component of a primary key in Microsoft Access can be null.
The referential integrity rule says that the database must not contain any unmatched foreign key values. This implies that:
- A row may not be added to a table with a foreign key unless the referenced value exists in the referenced table.
- If the value in a table that’s referenced by a foreign key is changed (or the entire row is deleted), the rows in the table with the foreign key must not be “orphaned.”
In general, there are three options available when a referenced primary key value changes or a row is deleted. The options are:
- Disallow. The change is completely disallowed.
- Cascade. For updates, the change is cascaded to all dependent tables. For deletions, the rows in all dependent tables are deleted.
- Nullify. For deletions, the dependent foreign key values are set to Null.
Microsoft Access allows you to disallow or cascade referential integrity updates and deletions using the Edit | Relationships command (see Figure 13). Nullify is not an option.
Note: When you wish to implement referential integrity in Microsoft Access, you must perform one additional step outside of the Edit | Relationships dialog: in table design, you must set the Required property for the foreign key column to Yes. Otherwise, Microsoft Access will allow your users to enter a Null foreign key value, thus violating strict referential integrity.
Database-Specific Integrity Rules
All integrity constraints that do not fall under entity integrity or referential integrity are termed database-specific rules or business rules. These type of rules are specific to each database and come from the rules of the business being modeled by the database. It is important to note that the enforcement of business rules is asimportant as the enforcement of the general integrity rules discussed in the previous section.
Note: Rules in Microsoft Access 2.0 are now enforced at the engine level, which means that forms, action queries and table imports can no longer ignore your rules. Because of this change, however, column rules can no longer reference other columns or use domain, aggregate, or user-defined functions.
Without the specification and enforcement of business rules, bad data will get in the database. The old adage, “garbage in, garbage out” applies aptly to the application (or lack of application) of business rules. For example, a pizza delivery business might have the following rules that would need to be modeled in the database:
- Order date must always be between the date the business started and the current date.
- Order time and delivery time can be only during business hours.
- Delivery time must be greater than or equal to Order time.
- New orders cannot be created for discontinued menu items.
- Customer zip codes must be within a certain range—the delivery area.
- The quantity ordered can never be less than 1 or greater than 50.
- Non-null discounts can never be less than 1 percent or greater than 30 percent.
Microsoft Access 2.0 supports the specification of validation rules for each column in a table. For example, the first business rule from the above list has been specified in Figure 14.
Microsoft Access 2.0 also supports the specification of a global rule that applies to the entire table. This is useful for creating rules that cross-reference columns as the example in Figure 15 demonstrates. Unfortunately, you’re only allowed to create one global rule per table, which could make for some awful validation error messages (e.g., “You have violated one of the following rules: 1. Delivery Date > Order Date. 2. Delivery Time > Order Time….”).
Although Microsoft Access business-rule support is better than most other desktop DBMS programs, it is still limited (especially the limitation of one global table rule), so you will typically build additional business rule logic into applications, usually in the data entry forms. This logic should be layered on top of any table-based rules and can be built into the application using combo boxes, list-boxes and option groups that limit available choices, form-level and field-level validation rules, and event procedures. These application-based rules, however, should be used only when the table-based rules cannot do the job. The more you can build business rules in at the table level, the better, because these rules will always be enforced and will require less maintenance.