Oracle约束检验

今天研究了一下CONSTRAINT的检验,分为两种:immediate--指在执行DML语句时就进行验证,
defer--指在执行commit后才进行验证.默认条件下,会话的constraint验证类型为immediate,
但是我们可以将它改为defered;
SQL>alter session set constraints = deferred;
也可以将它改回来:alter session set constraints = immediate;
测试如下:
SQL> Create table test(id varchar(20),name varchar(20));
SQL> alter table TEST
  add constraint PK_TEST primary key (ID)
  using index
  tablespace Users
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
SQL> select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,DEFERRABLE,DEFERRED,VALIDATED from user_constraints
  2  /
 
CONSTRAINT_NAME                CONSTRAINT_TYPE TABLE_NAME                     DEFERRABLE     DEFERRED  VALIDATED
------------------------------ --------------- ------------------------------ -------------- --------- -------------
PK_TEST                        P               TEST                           NOT DEFERRABLE IMMEDIATE VALIDATED
DEFERRABLE为not DEFERRABLE, DEFERRED为IMMEDIATE。
SQL> insert into test values('1','guojianjun')
  2  /
1 row inserted
SQL>  insert into test values('1','guojianjun')
  2  /
insert into test values('1','guojianjun')
 
ORA-00001: 违反唯一约束条件 (STUDY.PK_TEST)
在执行DML时进行验证.
现在我们把他改为DEFERRED。
SQL> set CONSTRAINTS  PK_TEST immediate
  2  /
set CONSTRAINTS  PK_TEST immediate
ORA-02447: cannot defer a constraint that is not deferrable
--索引不能延迟验证
SQL> alter table test drop constraints pk_test
  2  /
Table altered
SQL> alter table TEST
  2    add constraint PK_TEST primary key (ID)
  3    deferrable
  4    using index
  5    tablespace users
  6    pctfree 10
  7    initrans 2
  8    maxtrans 255
  9    storage
 10    (
 11      initial 64K
 12      minextents 1
 13      maxextents unlimited
 14    );
 
Table altered

SQL> select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,DEFERRABLE,DEFERRED,VALIDATED from user_constraints
  2  /
 
CONSTRAINT_NAME                CONSTRAINT_TYPE TABLE_NAME                     DEFERRABLE     DEFERRED  VALIDATED
------------------------------ --------------- ------------------------------ -------------- --------- -------------
PK_TEST                        P               TEST                           DEFERRABLE     IMMEDIATE VALIDATED
DEFERRABLE为DEFERRABLE, DEFERRED为IMMEDIATE。
SQL>  insert into test values('1','guojianjun')
  2  /
insert into test values('1','guojianjun')
 
ORA-00001: 违反唯一约束条件 (STUDY.PK_TEST)
--执行DML时验证。
SQL> alter table test drop constraints pk_test
  2  /
 
Table altered
 
SQL>
SQL> alter table TEST
  2    add constraint PK_TEST primary key (ID)
  3    deferrable initially deferred
  4    using index
  5    tablespace Users
  6    pctfree 10
  7    initrans 2
  8    maxtrans 255
  9    storage
 10    (
 11      initial 64K
 12      minextents 1
 13      maxextents unlimited
 14    );
 
Table altered
 
SQL> select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,DEFERRABLE,DEFERRED,VALIDATED from user_constraints
  2  /
 
CONSTRAINT_NAME                CONSTRAINT_TYPE TABLE_NAME                     DEFERRABLE     DEFERRED  VALIDATED
------------------------------ --------------- ------------------------------ -------------- --------- -------------
PK_TEST                        P               TEST                           DEFERRABLE     DEFERRED  VALIDATED
SQL> insert into test values('1','guojianjun')
  2  /
 
1 row inserted
 
SQL> commit
  2  /
 
commit
 
ORA-02091: 事务处理已回退
ORA-00001: 违反唯一约束条件 (STUDY.PK_TEST)
 
--Commit时才验证.
切换到立即验证状态:
SQL> set CONSTRAINTS  PK_TEST immediate
/
SQL>  insert into test values('1','guojianjun')
  2  /
insert into test values('1','guojianjun')
 
ORA-00001: 违反唯一约束条件 (STUDY.PK_TEST)
--执行DML时验证。
 
 

你可能感兴趣的:(oracle,职场,休闲)