大慧博客

大慧的技术专栏

oracle constraint 约束 状态 详解

1.约束一共有四种状态

a、enable validate-要求新旧数据必须同时满足约束规则-在规则正在进行中时是不容许在表上进行任何 DML 操作的
b、enable novalidate-已存在数据可以不满足,但是新数据必须满足
c、disable validate-不容许在表上进行任何 DML 操作,对主键和唯一约束来说,会删除相应的唯一索引,但 约束规则仍然有效
d、disable novalidate-数据不满足约束规则,对主键和唯一约束来说,会删除相应的唯一索引

记忆方式:

DISABLE 和ENABLE 是针对新增的数据是否符合约束,NOVALIDATE 和VALIDATE是针对已经存在的数据是否符合约束。
如果是DISABLE 就是允许新数据进来,那么唯一索引就得删除。
如果是validate就是保证现有数据完整性,那么就要锁表,不允许任何DML。

You can specify that a constraint is enabled (ENABLE) or disabled (DISABLE). If a constraint is enabled, data is checked as it is entered or updated in the database, and data that does not conform to the constraint is prevented from being entered. If a constraint is disabled, then data that does not conform can be allowed to enter the database.

Additionally, you can specify that existing data in the table must conform to the constraint (VALIDATE). Conversely, if you specifyNOVALIDATE, you are not ensured that existing data conforms.

An integrity constraint defined on a table can be in one of the following states:

ENABLE, VALIDATE

ENABLE, NOVALIDATE

DISABLE, VALIDATE

DISABLE, NOVALIDATE

2.检测时间
INITIALLY IMMEDIATE 初始化立即执行–在每条语句执行结束时检验约束
INITALLY DEFERRED 初始化延迟执行,一直等到事务完成后(或者调用 set constraint immediate 语句时)才检验约束

3.使用场景:

  • DISABLE NOVALIDATE
    不检查新数据和现有数据,因此这些数据可能不符合约束条件。
    当数据来自验证过的源,而且表是只读表时,通常会使用此状态。因此,不会将新数据输入表中。在已清理了数据的数据仓库环境中使用NOVALIDATE。此时不需要进行验证,因而可以节省很多时间。
  • DISABLE VALIDATE
    如果约束条件处于此状态,则不允许对有约束条件的列进行任何修改。因为如果在验证现有数据后,又允许将未经检查的数据输入表中,就会出现不一致的情况。通常,在必须验证现有数据但不进行修改时,以及不需要索引来提高性能时,可使用此状态。
  • ENABLE NOVALIDATE
    新数据符合约束条件,但现有数据处于未知状态。通常在确信表中只存在纯净数据和符合规则的数据的情况下使用该状态,此时不需要进行验证。但是,不允许将违反规则的新数据输入到系统中。
  • ENABLE VALIDATE
    新数据与现有数据均符合约束条件。这是约束条件的典型状态和默认状态。

eg1:

A constraint in a table is defined with the INITIALLY IMMEDIATE clause. You executed the
ALTER TABLE command with the ENABLE VALIDATE option to enable the constraint that was
disabled.What are the two effects of this command? (Choose two.)
选项
A.It fails if any existing row violates the constraint.
B.It does not validate the existing data in the table.
C.It enables the constraint to be enforced at the end of each transaction.
D.It prevents insert, update, and delete operations on the table while the constraint is in the
process of being enabled.

Correct Answers: A D

eg2:

Examine the following statement that is used to modify the primary key constraint on the
SALES table:
SQL> ALTER TABLE SALES MODIFY CONSTRAINT pk DISABLE VALIDATE;
Which three statements are true regarding the above command? (Choose three.)
选项
A.The constraint remains valid.
B.The index on the constraint is dropped.
C.It allows the loading of data into the table using SQL *Loader.
D.New data conforms to the constraint, but existing data is not checked.
E.It allows the data manipulation on the table using INSERT/UPDATE/DELETE SQL statements.

Correct Answers: A B C

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nirvana52/article/details/59078327
文章标签: oracle
个人分类: oracle
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭