一、状态
disable/enable对新的数据是否检查约束条件,
validate/invalidate对表中已有的旧数据是否校验约束条件。
二、启用、禁用约束
启用约束:
enable( validate) :启用约束,创建索引,对已有及新加入的数据执行约束,validate关键字可省略。
enable novalidate :启用约束,创建索引,仅对新加入的数据强制执行约束,而不管表中的现有数据。
禁用约束:
disable( novalidate):关闭约束,删除索引,可以对约束列的数据进行修改等操作,novalidate关键字可省略。
disable validate :关闭约束,删除索引,不能对表进行 插入/更新/删除等操作。
三、实例
创建测试表
SQL>
create table t1 as (select * from scott.emp where rownum<=5);
Table created
SQL>
select * from t1 order by empno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
1、ENABLE VALIDATE
启用约束,创建索引,对已有及新加入的数据执行约束,validate关键字可省略。
SQL>
alter table t1 add constraints pk_t1 primary key(empno) enable validate;
Table altered
SQL>
commit;
Commit complete
SQL>
select constraint_name,constraint_type,table_name,status,validated,index_name
from user_constraints where constraint_name='PK_T1';
CONSTRAINT_NAME CONSTRAINT_TYPE TABLE_NAME STATUS VALIDATED INDEX_NAME
--------------- --------------- ---------- ---------- --------------- ----------
PK_T1 P T1 ENABLED VALIDATED PK_T1
SQL>
insert into t1 values(7369,'WANG','CLERK',7698,TO_DATE('1984-1-19','YYYY-MM-DD'),2000,1200,20);
insert into t1 values(7369,'WANG','CLERK',7698,TO_DATE('1984-1-19','YYYY-MM-DD'),2000,1200,20)
ORA-00001: unique constraint (SYS.PK_T1) violated
2、ENABLE NOVALIDATE
启用约束,创建索引,仅对新加入的数据强制执行约束,而不管表中的现有数据。
SQL>
alter table t1 drop constraints pk_t1;
Table altered
SQL>
alter table t1 add constraint pk_t1 primary key(empno) enable novalidate;
Table altered
SQL>
select constraint_name,constraint_type,table_name,status,validated,index_name
from user_constraints where constraint_name='PK_T1';
CONSTRAINT_NAME CONSTRAINT_TYPE TABLE_NAME STATUS VALIDATED INDEX_NAME
--------------- --------------- ---------- ---------- --------------- ----------
PK_T1 P T1 ENABLED NOT VALIDATED PK_T1
SQL>
insert into t1 values(7369,'WANG','CLERK',7698,TO_DATE('1984-1-19','YYYY-MM-DD'),2000,1200,20);
insert into t1 values(7369,'WANG','CLERK',7698,TO_DATE('1984-1-19','YYYY-MM-DD'),2000,1200,20)
ORA-00001: unique constraint (SYS.PK_T1) violated
3、DISABLE VALIDATE
关闭约束,删除索引,不能对表进行 插入/更新/删除等操作。
SQL>
alter table t1 drop constraints pk_t1;
Table altered
SQL>
alter table t1 add constraint pk_t1 primary key(empno) disable validate;
Table altered
SQL>
select constraint_name,constraint_type,table_name,status,validated,index_name
from user_constraints where constraint_name='PK_T1';
CONSTRAINT_NAME CONSTRAINT_TYPE TABLE_NAME STATUS VALIDATED INDEX_NAME
--------------- --------------- ---------- ---------- --------------- ----------
PK_T1 P T1 DISABLED VALIDATED
SQL>
insert into t1 values(7360,'WANG','CLERK',7698,TO_DATE('1984-1-19','YYYY-MM-DD'),2000,1200,20);
insert into t1 values(7360,'WANG','CLERK',7698,TO_DATE('1984-1-19','YYYY-MM-DD'),2000,1200,20)
ORA-25128: No insert/update/delete on table with constraint (SYS.PK_T1) disabled and validated
4、DISABLE NOVALIDATE
关闭约束,删除索引,可以对约束列的数据进行修改等操作,novalidate关键字可省略。
SQL>
alter table t1 drop constraints pk_t1;
Table altered
SQL>
alter table t1 add constraints pk_t1 primary key(empno) disable novalidate;
Table altered
SQL>
insert into t1 values(7369,'WANG','CLERK',7698,TO_DATE('1984-1-19','YYYY-MM-DD'),2000,1200,20);
1 row inserted
SQL>
select constraint_name,constraint_type,table_name,status,validated,index_name
from user_constraints where constraint_name='PK_T1';
CONSTRAINT_NAME CONSTRAINT_TYPE TABLE_NAME STATUS VALIDATED INDEX_NAME
--------------- --------------- ---------- ---------- --------------- ----------
PK_T1 P T1 DISABLED NOT VALIDATED
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25264937/viewspace-693692/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/25264937/viewspace-693692/