ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 讨论关于Constraint states

讨论关于Constraint states

原创 Linux操作系统 作者:gdutllf2006 时间:2009-10-22 22:01:06 0 删除 编辑

讨论关于Constraint states:

<>P649

 

最佳实践:

Integrity Constraint States: Procedures and Benefits

Using integrity constraint states in the following order can ensure the best benefits:

1. Disable state.

2. Perform. the operation (load, export, import).

3. Enable NOVALIDATE state.

4. Enable state.

Some benefits of using constraints in this order are:

 No locks are held.

 All constraints can go to enable state concurrently.

 Constraint enabling is done in parallel.

 Concurrent activity on table is permitted.

 

Constraint states include:

 

ENABLE ensures that all incoming data conforms to the constraint

 

DISABLE allows incoming data, regardless of whether it conforms to the constraint

 

VALIDATE ensures that existing data conforms to the constraint

 

NOVALIDATE means that some existing data may not conform. to the constraint

 

In addition:

 

ENABLE VALIDATE is the same as ENABLE. The constraint is checked and is guaranteed to hold for all rows.

 

ENABLE NOVALIDATE means that the constraint is checked, but it does not have to be true for all rows. This allows existing rows to violate the constraint, while ensuring that all new or modified rows are valid.

 

In an ALTER TABLE statement, ENABLE NOVALIDATE resumes constraint checking on disabled constraints without first validating all data in the table.

 

DISABLE NOVALIDATE is the same as DISABLE. The constraint is not checked and is not necessarily true.

 

DISABLE VALIDATE disables the constraint, drops the index on the constraint, and disallows any modification of the constrained columns.

 

Transitions between these states are governed by the following rules:

 

l         ENABLE implies VALIDATE, unless NOVALIDATE is specified.

 

l         DISABLE implies NOVALIDATE, unless VALIDATE is specified.

 

l         VALIDATE and NOVALIDATE do not have any default implications for the ENABLE and DISABLE states.

 

When a unique or primary key moves from the DISABLE state to the ENABLE state, if there is no existing index, a unique index is automatically created. Similarly, when a unique or primary key move from ENABLE to DISABLE and it is enabled with a unique index, the unique index is dropped.

When any constraint is moved from the NOVALIDATE state to the VALIDATE state, all data must be checked. (This can be very slow.) However, moving from VALIDATE to NOVALIDATE simply forgets that the data was ever checked.

 

Moving a single constraint from the ENABLE NOVALIDATE state to the ENABLE VALIDATE state does not block reads, writes, or other DDL statements. It can be done in parallel.

 

test example:

 

create table t1(id number primary key  );

 

col OWNER format a20;

col CONSTRAINT_NAME format a20;

col Table_NAME format a20;

idle> select TABLE_NAME, constraint_name, status, validated from user_constraints where table_name='T1';

T1                   SYS_C002055          ENABLED  VALIDATED

 

 

insert into t1 values(1);

insert into t1 values(2);

commit;

 

idle> insert into t1 values(1);

insert into t1 values(1)

*

ERROR at line 1:

ORA-00001: unique constraint (TEST.SYS_C002055) violated

 

alter table t1 rename constraint SYS_C002055  to t1_pk;

idle> alter table t1 rename constraint SYS_C002055  to t1_pk;

 

Table altered.

 

alter table t1 disable constraint t1_pk;

idle> alter table t1 disable constraint t1_pk;

 

Table altered.

 

 

idle> select TABLE_NAME, constraint_name, status, validated from user_constraints where table_name='T1';

T1                   T1_PK                DISABLED NOT VALIDATED

一旦disable, 随之也NOVALITDATED,除非Validate is specifed.

 

 

insert into t1 values(1);

commit;

 

 

idle> ALTER TABLE t1 ENABLE  CONSTRAINT t1_pk;

ALTER TABLE t1 ENABLE  CONSTRAINT t1_pk

*

ERROR at line 1:

ORA-02437: cannot validate (TEST.T1_PK) - primary key violated

因为t1中已存在有重复的key.所以不能enable validate

 

 

idle> ALTER TABLE t1 ENABLE NOVALIDATE CONSTRAINT t1_pk;

ALTER TABLE t1 ENABLE NOVALIDATE CONSTRAINT t1_pk

*

ERROR at line 1:

ORA-02437: cannot validate (TEST.T1_PK) - primary key violated

 

idle> ALTER TABLE t1  NOVALIDATE CONSTRAINT t1_pk;

ALTER TABLE t1  NOVALIDATE CONSTRAINT t1_pk

                *

ERROR at line 1:

ORA-01735: invalid ALTER TABLE option

 

idle> drop table t1;

 

Table dropped.

 

 

idle> create table t1(id number primary key disable );

 

Table created.

idle> select TABLE_NAME, constraint_name, status, validated from user_constraints where table_name='T1';

T1                   SYS_C002056          DISABLED NOT VALIDATED

此時的狀態為 Disabled NOVALIDATED

 

insert into t1 values(1);

insert into t1 values(2);

commit;

 

 

idle> insert into t1 values(1);

 

1 row created.

 

 

再次插入

idle> insert into t1 values(1);

 

1 row created.

 

这时并没有报错。因为状态为Disabled NOVALIDATED

 

 

idle> ALTER TABLE T1 ENABLE CONSTRAINT SYS_C002056;

ALTER TABLE T1 ENABLE CONSTRAINT SYS_C002056

*

ERROR at line 1:

ORA-02437: cannot validate (TEST.SYS_C002056) - primary key violated

不能变为Validate,因为existing rows viloate the primary rule.

idle> select * from t1;

         1

         1

 

idle> select rowid from t1;

AAAHCLAAGAAAAAMAAA

AAAHCLAAGAAAAAMAAB

idle> delete t1 where rowid='AAAHCLAAGAAAAAMAAA';

 

1 row deleted.

 

idle> commit;

 

Commit complete.

删除重复行

 

idle>  ALTER TABLE T1 ENABLE CONSTRAINT SYS_C002056;

 

Table altered.

恢复正常。

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10248702/viewspace-617235/,如需转载,请注明出处,否则将追究法律责任。

下一篇: Samba Install in linux
请登录后发表评论 登录
全部评论

注册时间:2009-07-08

  • 博文量
    128
  • 访问量
    379089