ALLBASE/SQL Reference Manual (36216-90216)

166 Chapter4
Constraints, Procedures, and Rules
Using Rules
Differences between Rules and Integrity Constraints
Rules are similar to integrity constraints in that when a rule is created, all existing
INSERT, UPDATE, and DELETE statements will be affected by the rule (if the statement type
is appropriate to the rule). Rules are viewed as changes to the table definition, and so all
existing sections depending on the table are invalidated when a rule is created. When
these sections are next revalidated, the rule definition is picked up and compared to the
section; appropriate rules are then included in the revalidated section for checking at
statement execution time.
The following are some of the most important ways in which rules differ from integrity
constraints:
Rules are entirely reactive. They are not fired at CREATE RULE time against the
existing rows in the table. Moreover, after DISABLE RULES, no record is kept of rows
the rule would have fired on; so, when the ENABLE RULES statement is next issued, the
rule is not fired retroactively. Integrity constraints, on the other hand, are always
checked when an ALTER TABLE statement is issued with the ADD CONSTRAINT clause,
and when SET CONSTRAINTS IMMEDIATE is executed.
Rules only fire on the statement types they are defined to fire on, whereas integrity
constraints will be checked on all data change operations.
Rules do not use index structures to enforce the constraints they define; some integrity
constraints build special indexes.
The only side effect of the integrity constraint is an error, while a rule can have many
different side effects depending on the actions of the procedure it invokes.
In addition to providing a general way of implementing constraints, rules can be used to
define more abstract tasks such as logging the changes made to a table or enforcing
stricter security measures developed by the database designer. Rules are most useful in
defining complex relationships that cannot be modeled with existing check, unique, or
referential constraints.