`
fantasyday
  • 浏览: 32944 次
  • 性别: Icon_minigender_1
  • 来自: 东京
社区版块
存档分类
最新评论

Initially immediate vs initially deferred

阅读更多
Initially immediate

This is the default when neither initially immediate nor initially deferred has been specified.
The constraint is checked after each SQL statement.
create table init_immediate (
  id   number primary key initially immediate,
  data varchar2(50)
);


Initially deferred

The constraint is checked when a transaction ends.
drop table init_immediate;

create table init_immediate (
  id   number       primary key initially deferred,
  data varchar2(50) not null initially deferred
);


insert into init_immediate values ( null, 'one');
insert into init_immediate values (    3, null);
insert into init_immediate values (    8, 'eight');
insert into init_immediate values (    3, 'two');

update init_immediate set id = 1 where data = 'one';
update init_immediate set id = 2 where data = 'two';
update init_immediate set data = 'three' where id = 3;

commit;

It will be OK.

ORA-02447

A not deferrable constraint cannot be set to initially deferred,
it raises an ORA-02447: cannot defer a constraint that is not deferrable which seems logical:
create table no_no_ora_02447 (
  id   number primary key not deferrable initially deferred,
  data varchar2(50)
);


Deferrable constraint
create table cons_deferrable_pk_tab (
  a number,
  b varchar2(10),
  c number not null deferrable,
  d date,
  primary key (a, b) deferrable
);

create table cons_deferrable_fk_tab (
  z,
  y,
  x varchar2(10),
  foreign key (z,y) references cons_deferrable_pk_tab deferrable
);


Now we insert data to the table will fails, because reference table has not been inserted data yet.
insert into cons_deferrable_fk_tab values (1, 'one', 'foo');

ORA-02291: 整合性制約(ZOLO.SYS_C005382)に違反しました - 親キーがありません

set constraints all deferred


And now the constraints are deferred, so we can insert data which still not inserted to the reference table.

insert into cons_deferrable_fk_tab values (1, 'one', 'foo');


Though commit will fail.

set constraints [Oracle SQL]
set constraint[s] all { immediate | deferred };
set constraint[s] constraint-name-1 [, constraint-name-n ...] { immediate | deferred };
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics