States of Integrity Constraints
States of Integrity Constraints
As part of constraint definition, you can specify how and when Oracle Database should enforce the constraint, thereby determining the constraint state.
Checks for Modified and Existing Data
The database enables you to specify whether a constraint applies to existing data or future data. If a constraint is enabled, then the database checks new data as it is entered or updated. Data that does not conform to the constraint cannot enter the database. For example, enabling a NOT NULL constraint on employees.department_id guarantees that every future row has a department ID. If a constraint is disabled, then the table can contain rows that violate the constraint.
You can set constraints to validate (VALIDATE) or not validate (NOVALIDATE) existing data. If VALIDATE is specified, then existing data must conform to the constraint. For example, enabling a NOT NULL constraint on employees.department_id and setting it to VALIDATE checks that every existing row has a department ID. If NOVALIDATE is specified, then existing data need not conform to the constraint.
The behavior of VALIDATE and NOVALIDATE always depends on whether the constraint is enabled or disabled. Table 5-4 summarizes the relationships.
Table 5-4 Checks on Modified and Existing Data
| Modified Data | Existing Data | Summary |
|---|---|---|
|
|
|
Existing and future data must obey the constraint. An attempt to apply a new constraint to a populated table results in an error if existing rows violate the constraint. |
|
|
|
The database checks the constraint, but it need not be true for all rows. Thus, existing rows can violate the constraint, but new or modified rows must conform to the rules. |
|
|
|
The database disables the constraint, drops its index, and prevents modification of the constrained columns. |
|
|
|
The constraint is not checked and is not necessarily true. |
See Also:
Oracle Database SQL Language Reference to learn about constraint states
Deferrable Constraints
Every constraint is either in a not deferrable (default) or deferrable state. This state determines when Oracle Database checks the constraint for validity. The following graphic depicts the options for deferrable constraints.

Description of the illustration cncpt313.gif
Nondeferrable Constraints
If a constraint is not deferrable, then Oracle Database never defers
the validity check of the constraint to the end of the transaction.
Instead, the database checks the constraint at the end of each
statement. If the constraint is violated, then the statement rolls back.
For example, assume that you create a nondeferrable NOT NULL constraint for the employees.last_name
column. If a user attempts to insert a row with no last name, then the
database immediately rolls back the statement because the NOT NULL constraint is violated. No row is inserted.
Deferrable Constraints
A deferrable constraint permits a transaction to use the SET CONSTRAINT clause to defer checking of this constraint until a COMMIT
statement is issued. If you make changes to the database that might
violate the constraint, then this setting effectively lets you disable
the constraint until all the changes are complete.
You can set the default
behavior for when the database checks the deferrable constraint. You can
specify either of the following attributes:
INITIALLY IMMEDIATEThe database checks the constraint immediately after each statement
executes. If the constraint is violated, then the database rolls back
the statement.INITIALLY DEFERREDThe database checks the constraint when a
COMMITis issued. If the constraint is violated, then the database rolls back the transaction.
Assume that a deferrable NOT NULL constraint on employees.last_name is set to INITIALLY DEFERRED. A user creates a transaction with 100 INSERT statements, some of which have null values for last_name. When the user attempts to commit, the database rolls back all 100 statements. However, if this constraint were set to INITIALLY IMMEDIATE, then the database would not roll back the transaction.
If a constraint causes an action, then the database considers this
action as part of the statement that caused it, whether the constraint
is deferred or immediate. For example, deleting a row in departments causes the deletion of all rows in employees that reference the deleted department row. In this case, the deletion from employees is considered part of the DELETE statement executed against departments.
See Also:
Oracle Database SQL Language Reference for information about constraint attributes and their default values
Examples of Constraint Checking
Some examples may help illustrate when Oracle Database performs the checking of constraints. Assume the following:
The
employeestable has the structure shown in Figure 5-2.The self-referential constraint makes entries in the
manager_idcolumn dependent on the values of theemployee_idcolumn.
Insertion of a Value in a Foreign Key Column When No Parent Key Value Exists
Consider the insertion of the first row into the employees table. No rows currently exist, so how can a row be entered if the value in the manager_id column cannot reference any existing value in the employee_id column? Some possibilities are:
A null can be entered for the
manager_idcolumn of the first row, if themanager_idcolumn does not have aNOTNULLconstraint defined on it.Because nulls are allowed in foreign keys, this row is inserted into the table.
The same value can be entered in the
employee_idandmanager_idcolumns, specifying that the employee is his or her own manager.This case reveals that Oracle Database performs its constraint checking after
the statement has been completely run. To allow a row to be entered
with the same values in the parent key and the foreign key, the database
must first run the statement (that is, insert the new row) and then
determine whether any row in the table has anemployee_idthat corresponds to themanager_idof the new row.A multiple row
INSERTstatement, such as anINSERTstatement with nestedSELECTstatement, can insert rows that reference one another.For example, the first row might have 200 for employee ID and 300 for
manager ID, while the second row has 300 for employee ID and 200 for
manager. Constraint checking is deferred until the complete execution of
the statement. All rows are inserted first, and then all rows are
checked for constraint violations.
Default values are included as part of an INSERT statement before the statement is parsed. Thus, default column values are subject to all integrity constraint checking.
An Update of All Foreign Key and Parent Key Values
Consider the same self-referential integrity constraint in a
different scenario. The company has been sold. Because of this sale, all
employee numbers must be updated to be the current value plus 5000 to
coordinate with the employee numbers of the new company. Because manager
numbers are really employee numbers (see Figure 5-3), the manager numbers must also increase by 5000.
Figure 5-3 The employees Table Before Updates

Description of "Figure 5-3 The employees Table Before Updates"
You could execute the following SQL statement to update the values:
UPDATE employees SET employee_id = employee_id + 5000,
manager_id = manager_id + 5000;
Although a constraint is defined to verify that each manager_id value matches an employee_id value, the preceding statement is legal because the database effectively checks constraints after the statement completes. Figure 5-4 shows that the database performs the actions of the entire SQL statement before checking constraints.
Figure 5-4 Constraint Checking
The examples in this section illustrate the constraint checking mechanism during INSERT and UPDATE
statements, but the database uses the same mechanism for all types of
DML statements. The same mechanism is used for all types of constraints,
not just self-referential constraints.
Note:
Operations on a view or synonym are subject to the integrity constraints defined on the base tables.
States of Integrity Constraints的更多相关文章
- Foreign key (referential) constraints on DB2 LUW v105
oreign key constraints (also known as referential constraints or referential integrity constraints) ...
- 复制Informational constraints on LUW DB2 v105
An informational constraint is a constraint attribute that can be used by the SQL compiler to improv ...
- Chapter 7 Integrity(完整性), Views(视图), Security(安全性), and Catalogs(目录)
from Database Design to Physical Form CREATE TABLE integrity constraints (完整性约束) CREATE VIEW Securit ...
- 架构:Eventually Consistent - Revisited
原文地址:http://i.cnblogs.com/EditPosts.aspx?opt=1. I wrote a first version of this posting on consisten ...
- Eventually Consistent(最终一致性)(转)
应该说搞分布式系统必读的文章了,转过来,这是2008年12月Werner revise过的版本,先贴上内容简介:分布式系统的CAP理论 CAP理论(data consistency, system a ...
- 斯坦福CS课程列表
http://exploredegrees.stanford.edu/coursedescriptions/cs/ CS 101. Introduction to Computing Principl ...
- Oracle数据加载之外部表的介绍
环境: 服务端:RHEL6.4 + Oracle 11.2.0.4 目录: 一. 创建外部表 1.1 创建外部表需要的目录 1.2 创建外部表 1.3 创建外部表源文件 1.4 查询外部表 二. 加载 ...
- SSIS 处理NULL
不同于SQL Server中NULL表示值是未知的(Unknown Value),没有数据类型,但是,在SSIS中,NULL是有数据类型的,要获取某一个NULL值,必须指定数据类型,例如,变量 Int ...
- ORACLE基本数据类型总结
ORACLE基本数据类型(亦叫内置数据类型 built-in datatypes)可以按类型分为:字符串类型.数字类型.日期类型.LOB类型.LONG RAW& RAW类型.ROWID &am ...
随机推荐
- luogu1052 过河
题目大意 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...
- P2932 [USACO09JAN]地震造成的破坏Earthquake Damage 爆搜
这题怎么这么水~~~本来以为挺难的一道题,结果随便一写就过了...本来还不知道损坏的牛棚算不算,结果不明不白就过了... 题干: 农夫John的农场遭受了一场地震.有一些牛棚遭到了损坏,但幸运地,所有 ...
- C++ this指针 全部
在每一个成员函数中都包含一个特殊的指针,这个指针的名字是固定的.叫做this.它是指向本类对象的指针,它的值是当前被调用的成员函数所在的对象的起 始地址.例如:当调用成员函数a.volume ...
- LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现
1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...
- 小白写的一个ASP.NET分页控件,仅供娱乐
无聊,第一次写博客,自己动手写了一个分页控件.由于我是新手,有很多地方写得不够好,希望各位大牛多多指正.哈哈哈 /// <summary> /// 分页控件 /// </summar ...
- C#接入第三方支付一些小问题
13年第一次接入支付宝的时候,支付宝的api还不是很好用,费了些劲才完成,本月再次接入的时候发现已经很好用了,接入过程非常顺畅,只出现了一个小问题,我的金额默认是保留了4位小数,支付宝api只接受最多 ...
- 最近积累的JS 东西,分享一下
js 关闭页面 var browserName=navigator.appName; if (browserName=="Netscape") { window.open('',' ...
- Android 应用安装成功之后删除apk文件
问题: 在应用开发中遇到需要这样的需求:在用户下载我们的应用安装之后删除安装包. 解决: android会在每个外界操作APK的动作之后发出系统级别的广播,过滤器名称: android.intent. ...
- js实现“级联菜单”
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- VMWare虚拟机移动
1. 背景: 虚拟机:VM3 原安装路径:C:\Users\Administrator\Documents\Virtual Machines 移动到目标路径:D:\Virtual Machines ...
