您的位置:首页 > 大数据 > 人工智能

约束的四模式

2014-03-11 14:08 225 查看
对原来的数据做检查 validate/novalidated

对建完约束后进来的数据做检查enable/disable

有四种模式:

ENABLE
 
VALIDATE


create table t1 (a int);

insert into t1 values(0);

alter table t1 add constraint t1_a check(a > 0);

alter table t1 add constraint t1_a check(a >= 0);

insert into table t1 values(-1);

 

不指明validate 或novalidate则默认为validate

 

ENABLE
 
NOVALIDATE
 (不对表加锁)

alter table t1 drop constraint t1_a;

alter table t1 add constraint t1_a check(a > 0) enablenovalidate;

insert intot_edvn(a) values(-1);

 

DISABLE
 
VALIDATE
(不建索引
,

而且不能直接truncate,delete等


alter table t1 drop constraint t1_a;

insert into t1 values(1);

insert into t1 values(2);

insert into t1 values(3);

alter table t1 add constraint t1_a  PRIMARY KEY (a) disable validate;

 

DISABLE
 
NOVALIDATE
(感觉没啥用)


 

 

tester@AD_TEST> alter table t1 add constraint t1_acheck(a > 0);

alter table t1 add constraint t1_a check(a > 0)

                             *

ERROR at line 1:

ORA-02293: cannot validate (TESTER.T1_A) - check constraintviolated

tester@AD_TEST> alter table t1 add constraint t1_acheck(a >= 0);

 

Table altered.

tester@AD_TEST> insert into table t1 values(-1);

insert into table t1 values(-1)

            *

ERROR at line 1:

ORA-00903: invalid table name

 

 

tester@AD_TEST> alter table t1 drop constraint t1_a;

 

Table altered.

 

tester@AD_TEST> alter table t1 add constraint t1_acheck(a > 0) enable novalidate;

 

Table altered.

 

tester@AD_TEST> insert into t_edvn(a) values(-1);

insert into t_edvn(a) values(-1)

*

ERROR at line 1:

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

 

 

tester@AD_TEST> @ind2 t1

 

OWNER                          TABLE_NAME                         BLOCKS   NUM_ROWS AVG_ROW_LEN LAST_ANALYZED

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

TESTER                         T1

 

1 row selected.

 

 

Display indexes where table or index name matches t1...

 

no rows selected

 

 

no rows selected

 

而且不能直接truncate

tester@AD_TEST> truncate table t1;

truncate table t1

*

ERROR at line 1:

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

 

 

tester@AD_TEST> alter table t1 drop constraint t1_a;

 

Table altered.

 

tester@AD_TEST> truncate table t1;

 

Table truncated.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  约束 constraint