ORA-02437 使对已有数据不检查的主键约束生效

--ORA-02437 使对已有数据不检查的主键约束生效
SYS@PROD1> desc t1
 Name						       Null?	Type
 ----------------------------------------------------- -------- ------------------------------------
 ID								NUMBER
 TEXT								VARCHAR2(2000)

SYS@PROD1> select * from t1;

	ID TEXT
---------- --------------------
	 1 a
	 2 b
	 
SYS@PROD1> alter table t1 add constraint t1_pk_1 primary key (id);

Table altered.

SYS@PROD1> select constraint_name,constraint_type,deferrable,deferred,status from user_constraints where table_name='T1';

CONSTRAINT_NAME 	       C DEFERRABLE	DEFERRED  STATUS
------------------------------ - -------------- --------- --------
T1_PK_1 		       P NOT DEFERRABLE IMMEDIATE ENABLED

SYS@PROD1> select index_name,uniqueness,index_type from user_indexes where table_name='T1';

INDEX_NAME		       UNIQUENES INDEX_TYPE
------------------------------ --------- ---------------------------
T1_PK_1 		       UNIQUE	 NORMAL

SYS@PROD1> insert into t1 values (1,'a');
insert into t1 values (1,'a')
*
ERROR at line 1:
ORA-00001: unique constraint (SYS.T1_PK_1) violated


SYS@PROD1> alter table t1 modify constraint t1_pk_1 disable;

Table altered.

SYS@PROD1> select constraint_name,constraint_type,deferrable,deferred,status from user_constraints where table_name='T1';

CONSTRAINT_NAME 	       C DEFERRABLE	DEFERRED  STATUS
------------------------------ - -------------- --------- --------
T1_PK_1 		       P NOT DEFERRABLE IMMEDIATE DISABLED

SYS@PROD1> select index_name,uniqueness,index_type from user_indexes where table_name='T1';

no rows selected

SYS@PROD1> insert into t1 values (1,'a');

1 row created.

SYS@PROD1> alter table t1 modify constraint t1_pk_1 enable novalidate;  --直接修改无法通过
alter table t1 modify constraint t1_pk_1 enable novalidate
*
ERROR at line 1:
ORA-02437: cannot validate (SYS.T1_PK_1) - primary key violated


SYS@PROD1> alter table t1 drop constraint t1_pk_1;  --删除主键约束

Table altered.

SYS@PROD1> alter table t1 add constraint t1_pk_2 primary key (id) deferrable initially deferred;  --由于有重复值所以不能通过validate
alter table t1 add constraint t1_pk_2 primary key (id) deferrable initially deferred
                              *
ERROR at line 1:
ORA-02437: cannot validate (SYS.T1_PK_2) - primary key violated


SYS@PROD1> alter table t1 add constraint t1_pk_2 primary key (id) deferrable initially deferred novalidate;

Table altered.

SYS@PROD1> select constraint_name,constraint_type,deferrable,deferred,status from user_constraints where table_name='T1';

CONSTRAINT_NAME 	       C DEFERRABLE	DEFERRED  STATUS
------------------------------ - -------------- --------- --------
T1_PK_2 		       P DEFERRABLE	DEFERRED  ENABLED

SYS@PROD1> select index_name,uniqueness,index_type from user_indexes where table_name='T1';

INDEX_NAME		       UNIQUENES INDEX_TYPE
------------------------------ --------- ---------------------------
T1_PK_2 		       NONUNIQUE NORMAL

SYS@PROD1> select * from t1;

	ID TEXT
---------- --------------------
	 1 a
	 2 b
	 1 a
	 
SYS@PROD1> insert into t1 values (1,'c');  --检验DEFERRED效果

1 row created.

SYS@PROD1> commit
  2  ;
commit
*
ERROR at line 1:
ORA-02091: transaction rolled back
ORA-00001: unique constraint (SYS.T1_PK_2) violated
	 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值