Foreign Key Constraints
Whenever two tables contain one or more common columns, Oracle Database can enforce the relationship between the two tables through a foreign key constraint, also called a referential integrity constraint. The constraint requires that for each value in the column on which the constraint is defined, the value in the other specified other table and column must match. An example of a referential integrity rule is an employee can work for only an existing department.
只要两张表包含一个或多个公共的列,Oracle数据库都可以通过外键约束(也叫引用完整性约束)来强制两个表的关系。约束要求,在定义约束的列中的每个值,在另一个指定的表和列中必须有相对应的值(子表A键引用父表B键,在子表A键建立外键约束。子表A列中的每个值,父表B键都要有相应的值)。一个关于描述引用约束规则的例子:员工只能为现有部门工作(即员工与部门间的关系)。
Table 5-2 lists terms associated with referential integrity constraints.
下表列出了与引用完整性约束相关的术语。
外键:
引用 被引用键的 单列或列集(键:代表列。外键:引用其他表.列的列,即子表中的列)。
例如,employees员工表的department_id部门编号列是外键,其引用departments部门表的department_id部门编号列。
外键可以被定义为多列。无论如何,一个多列组成的外键,必须引用一个多列组成的主键或唯一键,主键或唯一键的列个数及数据类型要与外键一致。
外键的值可以与引用的主键或唯一键的值相匹配,也可以与null匹配。如果组成外键的列中有一列为null,则外键非null的部分不必和父键的任何相应部门相匹配。(如果复合外键的任何列为空,则该键的非空部分不必与父键的任何相应部分匹配。)
引用的键/列
表中的唯一键或主键被一个外键引用。比如departments表的department_id列,是employees表department_id列的引用键。(被引用键,父表的主键或唯一键)
从属表或子表/依赖表
包含外键的表。这个表依赖于引用的主键或唯一键中的值。例如,employees表是departments表的子表
引用的表或父表
被子表中外键引用的表。这个表的引用键决定是否允许子表中INSERT或UPDATE操作。例如,departments表时employees表的父表。
Figure 5-1 shows a foreign key on the employees.department_id
column. It guarantees that every value in this column must match a value in the departments.department_id
column. Thus, no erroneous department numbers can exist in the employees.department_id
column.