Oracle约束的4种状态小结

约束的4种状态:

disable novalidate 既不会约束新增数据也不会验证已有数据,等同于disable

enable novalidate 约束新增数据但不会验证已有数据

disable validate 约束新增数据但不会验证已有数据,启用后禁止DML

enable validate 约束新增数据并验证已有数据,等同于enable


扩展解释:

disable / enable 针对新增数据(对于唯一性约束,enable会自动创建唯一性索引,disable会自动drop索引);

novalidate / validate 针对已有数据。


测试:

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

Connected as system
 
SQL> drop table dept2;
 
Table dropped
 
SQL> create table dept2 as select * from scott.dept;
 
Table created
 
SQL> select * from dept2 order by deptno;
 
DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON
 
SQL> alter table dept2 add constraint dept2_u1 unique(deptno);
 
Table altered
 
SQL> select index_name,table_name,uniqueness from dba_indexes where index_name = 'DEPT2_U1';
 
INDEX_NAME                     TABLE_NAME                     UNIQUENESS
------------------------------ ------------------------------ ----------
DEPT2_U1                       DEPT2                          UNIQUE
 
SQL> select constraint_name,status,validated from dba_constraints where constraint_name = 'DEPT2_U1';
 
CONSTRAINT_NAME                STATUS   VALIDATED
------------------------------ -------- -------------
DEPT2_U1                       ENABLED  VALIDATED
 
SQL> insert into dept2(deptno) values(10);
 
insert into dept2(deptno) values(10)
 
ORA-00001: unique constraint (SYSTEM.DEPT2_U1) violated
 
SQL> select * from dept2 order by deptno;
 
DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON
 
SQL> alter table dept2 modify constraint dept2_u1 disable novalidate;
 
Table altered
 
SQL> select index_name,table_name,uniqueness from dba_indexes where index_name = 'DEPT2_U1';
 
INDEX_NAME                     TABLE_NAME                     UNIQUENESS

------------------------------ ------------------------------ ----------

(disable自动移除索引)

 

SQL> select constraint_name,status,validated from dba_constraints where constraint_name = 'DEPT2_U1';
 
CONSTRAINT_NAME                STATUS   VALIDATED
------------------------------ -------- -------------
DEPT2_U1                       DISABLED NOT VALIDATED
 
SQL> insert into dept2(deptno) values(10);
 
1 row inserted
 
SQL> select * from dept2 order by deptno;
 
DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    10                
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON
 
SQL> alter table dept2 modify constraint dept2_u1 enable novalidate;
 
alter table dept2 modify constraint dept2_u1 enable novalidate
 
ORA-02299: cannot validate (SYSTEM.DEPT2_U1) - duplicate keys found
 (因为enable会去创建唯一性索引,而已有数据deptno存在重复数据10,所以这里不能enable)

SQL> delete from dept2 where deptno=10 and dname is null;
 
1 row deleted
 
SQL> alter table dept2 modify constraint dept2_u1 enable novalidate;
 
Table altered
 
SQL> select index_name,table_name,uniqueness from dba_indexes where index_name = 'DEPT2_U1';
 
INDEX_NAME                     TABLE_NAME                     UNIQUENESS
------------------------------ ------------------------------ ----------

DEPT2_U1                       DEPT2                          UNIQUE

(enable会自动创建唯一性索引)


SQL> select constraint_name,status,validated from dba_constraints where constraint_name = 'DEPT2_U1';
 
CONSTRAINT_NAME                STATUS   VALIDATED
------------------------------ -------- -------------
DEPT2_U1                       ENABLED  NOT VALIDATED
 
SQL> insert into dept2(deptno) values(10);
 
insert into dept2(deptno) values(10)
 
ORA-00001: unique constraint (SYSTEM.DEPT2_U1) violated
 
SQL> alter table dept2 modify constraint dept2_u1 disable validate;
 
Table altered
 
SQL> select index_name,table_name,uniqueness from dba_indexes where index_name = 'DEPT2_U1';
 
INDEX_NAME                     TABLE_NAME                     UNIQUENESS
------------------------------ ------------------------------ ----------
 
SQL> select constraint_name,status,validated from dba_constraints where constraint_name = 'DEPT2_U1';
 
CONSTRAINT_NAME                STATUS   VALIDATED
------------------------------ -------- -------------
DEPT2_U1                       DISABLED VALIDATED
 
SQL> insert into dept2(deptno) values(10);
 
insert into dept2(deptno) values(10)
 

ORA-25128: No insert/update/delete on table with constraint (SYSTEM.DEPT2_U1) disabled and validated

(disable validate后禁止DML)


SQL> select * from dept2 order by deptno;
 
DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON
 
SQL> alter table dept2 modify constraint dept2_u1 enable validate;
 
Table altered
 
SQL> select index_name,table_name,uniqueness from dba_indexes where index_name = 'DEPT2_U1';
 
INDEX_NAME                     TABLE_NAME                     UNIQUENESS
------------------------------ ------------------------------ ----------
DEPT2_U1                       DEPT2                          UNIQUE
 
SQL> select constraint_name,status,validated from dba_constraints where constraint_name = 'DEPT2_U1';
 
CONSTRAINT_NAME                STATUS   VALIDATED
------------------------------ -------- -------------
DEPT2_U1                       ENABLED  VALIDATED
 
SQL> insert into dept2(deptno) values(10);
 
insert into dept2(deptno) values(10)
 
ORA-00001: unique constraint (SYSTEM.DEPT2_U1) violated
 
SQL> select * from dept2 order by deptno;
 
DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON
 

SQL>


REF: <<Expert Oracle Database 11g Administration>> Page 308, Integrity Constraint States

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值