Foreign Key Constraints
Class
RWDBForeignKey encapsulates a foreign key constraint and is similar in architecture to
RWDBSchema.
A foreign key in one table references a primary key in another table, linking the tables. For example, the custID primary key in a table Customers could also appear as a custID column in an Invoice table in order to link a specific customer with a specific transaction.
A foreign key constraint ensures
referential integrity between the data in the two tables, meaning that any data in the foreign key column in Invoice must exist in the primary key column in Customers. The table with the foreign key (Invoice) is the “referencing table,” and the table it references (Customers) is, logically, the “referenced table.” For example:
Any changes to a Customers record that would invalidate the link with the corresponding record(s) in Invoice will invoke a particular
referential action, as defined by the enumerator
RWDBForeignKey::Constraint passed to the
RWDBForeignKey constructor and described in
Table 6.
Table 6 – Foreign Key Referential Actions defined in enum RWDBForeignKey::Constraint
Referential Action | Description |
---|
Restrict | Prevents the update or delete operation, resulting in failure. This is the default. |
Cascade | Modifies or deletes the corresponding records in the referencing table, so that the operation invoked on the referenced table is “cascaded” to the referencing table, thus still maintaining referential integrity. |
Nullify | Sets the corresponding rows in the referencing table to have a NULL value for the foreign key. |
Defaultify | Sets the corresponding rows in the referencing table to the default value for the foreign key column(s). |
For example, if updates or deletes to Customers would result in an update or delete of a custID that is referenced by at least one row in the Invoice table, one of the above actions will occur.
An
RWDBForeignKey instance is composed of any columns in the referencing table that are part of the foreign key. There may be one or more columns in a foreign key.
Class
RWDBForeignKeyList holds any number of
RWDBForeignKey instances. An
RWDBSchema object always contains a single
RWDBForeignKeyList containing zero or more foreign key constraints relevant to that schema.