oreign key constraints (also known as referential constraints or referential integrity constraints) enable you to define required relationships between and within tables.

For example, a typical foreign key constraint might state that every employee in the EMPLOYEE table must be a member of an existing department, as defined in the DEPARTMENT table.

Referential integrity is the state of a database in which all values of all foreign keys are valid. A foreign key is a column or a set of columns in a table whose values are required to match at least one primary key or unique key value of a row in its parent table. A referential constraint is the rule that the values of the foreign key are valid only if one of the following conditions is true:

  • They appear as values of a parent key.
  • Some component of the foreign key is null.

To establish this relationship, you would define the department number in the EMPLOYEE table as the foreign key, and the department number in the DEPARTMENT table as the primary key.

The table containing the parent key is called the parent table of the referential constraint, and the table containing the foreign key is said to be a dependent of that table.

Referential constraints can be defined in the CREATE TABLE statement or the ALTER TABLE statement. Referential constraints are enforced by the database manager during the execution of INSERT, UPDATE, DELETE, ALTER TABLE, MERGE, ADD CONSTRAINT, and SET INTEGRITY statements.

Table 1. Referential integrity terms
Concept Terms
Parent key A primary key or a unique key of a referential constraint.
Parent row A row that has at least one dependent row.
Parent table A table that contains the parent key of a referential constraint. A table can be a parent in an arbitrary number of referential constraints. A table that is the parent in a referential constraint can also be the dependent in a referential constraint.
Dependent table A table that contains at least one referential constraint in its definition. A table can be a dependent in an arbitrary number of referential constraints. A table that is the dependent in a referential constraint can also be the parent in a referential constraint.
Descendent table A table is a descendent of table T if it is a dependent of T or a descendent of a dependent of T.
Dependent row A row that has at least one parent row.
Descendent row A row is a descendent of row r if it is a dependent of r or a descendent of a dependent of r.
Referential cycle A set of referential constraints such that each table in the set is a descendent of itself.
Self-referencing table A table that is a parent and a dependent in the same referential constraint. The constraint is called a self-referencing constraint.
Self-referencing row

A row that is a parent of itself.

The purpose of a referential constraint is to guarantee that table relationships are maintained and that data entry rules are followed. This means that as long as a referential constraint is in effect, the database manager guarantees that for each row in a child table that has a non-null value in its foreign key columns, a row exists in a corresponding parent table that has a matching value in its parent key.

When an SQL operation attempts to change data in such a way that referential integrity will be compromised, a foreign key (or referential) constraint could be violated. The database manager handles these types of situations by enforcing a set of rules that are associated with each referential constraint. This set of rules consist of:

  • An insert rule
  • An update rule
  • A delete rule
When an SQL operation attempts to change data in such a way that referential integrity will be compromised, a referential constraint could be violated. For example,

  • An insert operation could attempt to add a row of data to a child table that has a value in its foreign key columns that does not match a value in the corresponding parent table's parent key.
  • An update operation could attempt to change the value in a child table's foreign key columns to a value that has no matching value in the corresponding parent table's parent key.
  • An update operation could attempt to change the value in a parent table's parent key to a value that does not have a matching value in a child table's foreign key columns.
  • A delete operation could attempt to remove a record from a parent table that has a matching value in a child table's foreign key columns.
The database manager handles these types of situations by enforcing a set of rules that are associated with each referential constraint. This set of rules consists of:

  • An insert rule
  • An update rule
  • A delete rule

Foreign key (referential) constraints on DB2 LUW v105的更多相关文章

  1. MySQL Foreign Key

    ntroduction to MySQL foreign key A foreign key is a field in a table that matches another field of a ...

  2. InnoDB和Foreign KEY Constraints

    InnoDB表中中Foreign Key定义 1. InnoDB允许a foreign key引用一个索引列或者索引组列. 2. InnoDB现在并不支持用户定义的分区表有foreign keys,这 ...

  3. Database Primary key and Foreign key [From Internet]

    Database Primary key and Foreign key --Create Referenced Table CREATE TABLE Department ( DeptID int ...

  4. 数据库中的參照完整性(Foreign Key)

    之前在项目中遇到了这样一个问题,我举得简单的样例来说明. 比方我们有两个表,一个表(department)存放的是部门的信息,比如部门id,部门名称等:还有一个表是员工表(staff),员工表里面肯定 ...

  5. oracle primary key & foreign key

    主键:一个表中只有一个主键约束,但是一个主键约束可以由数据表中的多个列组成:primary key alter table TName add constraints pk_name PRIMARY ...

  6. Is it bad to rely on foreign key cascading? 外键 级联操作

    Is it bad to rely on foreign key cascading? I'll preface前言 this by saying that I rarely delete rows ...

  7. SQL PRIMARY KEY,SQL FOREIGN KEY

    A primary key is defined as a column or a group of column that their value are always be unique. Nor ...

  8. admin添加用户时报错:(1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_l

    在stackoverflow找到答案: DATABASES = { 'default': { ... 'OPTIONS': { "init_command": "SET ...

  9. foreign key

    http://sevenseacat.net/2015/02/24/add_foreign_key_gotchas.html https://robots.thoughtbot.com/referen ...

随机推荐

  1. jQuery学习:用按键移动方块

    <!doctype html> <html> <head> <meta charset="utf-8"> <style typ ...

  2. BZOJ1483——[HNOI2009]梦幻布丁

    1.题目大意:这题就是给你一个序列,有两个操作,一个是询问序列中的连续段数,比如序列 1 2 2 1就是三段.. 1是一段,2 2 又是一段,1又是一段,就是相同的在一起,第二个操作就是将其中的一种数 ...

  3. ES6中Arguments和Parameters用法解析

    原文链接 译文 ECMAScript 6 (也称 ECMAScript 2015) 是ECMAScript 标准的最新版本,显著地完善了JS中参数的处理方式.除了其它新特性外,我们还可以使用rest参 ...

  4. 贴一下WC总结里提到的那道裸题吧。。。

    [bzoj4034][HAOI2015]T2 试题描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 ...

  5. OpenCV成长之路(5):图像直方图的应用

    正如第4篇文章所说的图像直方图在特征提取方面有着很重要的作用,本文将举两个实际工程中非常实用的例子来说明图像直方图的应用. 一.直方图的反向映射. 我们以人脸检测举例,在人脸检测中,我们第一步往往需要 ...

  6. C# 基于json通讯中的中文的处理

    如果通讯中产生了\\u4e00-\\u9fa5范围的中文的unicode代码,而不是\u4e00-\u9fa5范围的,那么c#的处理就比较麻烦了. 破解方法: 机制 它会把\\u4e00拆成部分来识别 ...

  7. 数论v2

    #include <cmath> #include <cstdio> #include <cstring> #include <algorithm> # ...

  8. ubuntu14.04 server安装vncserver

    $ sudo apt-get install -y tightvncserver $ vncserver 转自: https://www.liquidweb.com/kb/how-to-install ...

  9. svn迁移到git仓库并保留commit历史记录

    svn迁移到git仓库并保留commit历史记录 最近在做svn迁移到gitlab,由于之前一直是由svn做版本控制.最简单的方式是将svn的内容export出来,然后添加到gitlab即可.但是,如 ...

  10. ansible中tag的用法

    Tags 根据官方文档介绍: ansible允许通过自定义的关键字来给playbook中的资源打上标签,然后只运行标签标记的那个task任务. 例如,可能有个完成的OS配置,然后特定的步骤标记为“nt ...