zoukankan      html  css  js  c++  java
  • Foreign key (referential) constraints on DB2 LUW v105

    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
    ConceptTerms
    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
  • 相关阅读:
    用python实现average()函数,使得当可变参数长度为0的时候,也能正确返回结果。
    请用python实现函数func,当参数类型为list时,返回list中所有数字类型元素的和,当参数类型为tuple时,返回tuple中所有数字类型元素的乘积。
    用python实现循环和递归的形式定义函数,求出1~100的和。
    python实现0~100的平方和,用sum()函数接收一个list作为参数,并返回list所有元素之和。请计算 1*1 + 2*2 + 3*3 + ... + 100*100。
    用python实现线性回归
    IE11用JS检测判断
    Google Chrome的CSS hack写法
    javascript间窗口互相通信,window.open新建窗口保存父窗口对象
    解决IE6不支持css min-width与min-height
    CSS实现兼容性的渐变背景(gradient)效果(转)
  • 原文地址:https://www.cnblogs.com/sthv/p/5752459.html
Copyright © 2011-2022 走看看