Oracle7 Server Application Developer's Guide
Managing FOREIGN KEY Integrity Constraints
General information about defining, enabling, disabling, and dropping all types of integrity constraints is given in the previous sections. The following section supplements this information, focusing specifically on issues regarding FOREIGN KEY integrity constraints.
Defining FOREIGN KEY Integrity Constraints
The following topics are of interest when defining FOREIGN KEY integrity constraints.
Matching of Datatypes
When defining referential integrity constraints, the corresponding column names of the dependent and referenced tables do not need to match. However, they must be of the same datatype.
Composite Foreign Keys
Because foreign keys reference primary and unique keys of the parent table, and PRIMARY KEY and UNIQUE key constraints are enforced using indexes, composite foreign keys are limited to 16 columns.
Implied Referencing of a Primary Key
If the column list is not included in the REFERENCES option when defining a FOREIGN KEY constraint (single column or composite), Oracle assumes that you intend to reference the primary key of the specified table. Alternatively, you can explicitly specify the column(s) to reference in the parent table within parentheses. Oracle automatically checks to verify that this column list references a primary or unique key of the parent table. If it does not, an informative error is returned.
Privileges Required for FOREIGN KEY Integrity Constraints
To create a FOREIGN KEY constraint, the creator of the constraint must have privileged access to both the parent and the child table.
- The Parent Table The creator of the referential integrity constraint must own the parent table or have REFERENCES object privileges on the columns that constitute the parent key of the parent table.
- The Child Table The creator of the referential integrity constraint must have the ability to create tables (that is, the CREATE TABLE or CREATE ANY TABLE system privilege) or the ability to alter the child table (that is, the ALTER object privilege for the child table or the ALTER ANY TABLE system privilege).
In both cases, necessary privileges cannot be obtained via a role; they must be explicitly granted to the creator of the constraint.
These restrictions allow
- the owner of the child table to explicitly decide what constraints are enforced on her or his tables and the other users that can create constraints on her or his tables
- the owner of the parent table to explicitly decide if foreign keys can depend on the primary and unique keys in her tables
Specifying Referential Actions for Foreign Keys
Oracle allows two different types of referential integrity actions to be enforced, as specified with the definition of a FOREIGN KEY constraint:
- The UPDATE/DELETE No Action Restriction This action prevents the update or deletion of a parent key if there is a row in the child table that references the key. By default, all FOREIGN KEY constraints enforce the no action restriction; no option needs to be specified when defining the constraint to enforce the no action restriction. For example:
CREATE TABLE emp (
. . .,
FOREIGN KEY (deptno) REFERENCES dept);
- The ON DELETE CASCADE Action This action allows referenced data in the parent key to be deleted (but not updated). If referenced data in the parent key is deleted, all rows in the child table that depend on the deleted parent key values are also deleted. To specify this referential action, include the ON DELETE CASCADE option in the definition of the FOREIGN KEY constraint. For example:
CREATE TABLE emp ( . . .,
FOREIGN KEY (deptno) REFERENCES dept
ON DELETE CASCADE);
Enabling FOREIGN KEY Integrity Constraints
FOREIGN KEY integrity constraints cannot be enabled if the referenced primary or unique key's constraint is not present or not enabled.