SQL:deferrable initially deferred

SQL> create table cust(id number,name varchar2(10));
Table created

SQL> alter table cust add constraint cust_id_pk primary key(id) deferrable initially deferred;--延缓约束
Table altered

SQL> insert into cust values(1,'raj');
1 row inserted

SQL> insert into cust values(1,'sam');
1 row inserted

SQL> commit; --只有在提交的时候才会使用约束条件
commit
ORA-02091: 事务处理已回退
ORA-00001: 违反唯一约束条件 (OE.CUST_ID_PK)

SQL> select * from cust;  --提交时违反约束条件则全部回退。
ID NAME
---------- ----------

SQL> set constraint cust_id_pk immediate;  --修改约束为立即模式,即插入数据时立即使用约束条件
Constraints set

SQL> insert into cust values(1,'lata');
1 row inserted

SQL> insert into cust values(2,'king');
1 row inserted

SQL> insert into cust values(2,'test');
insert into cust values(2,'test')
ORA-00001: 违反唯一约束条件 (OE.CUST_ID_PK)

SQL> commit;
Commit complete

SQL> select * from cust;
ID NAME
---------- ----------
1 lata
2 king

SQL>

原文地址:https://www.cnblogs.com/rusking/p/4599414.html