SQL> create table t1 (id number not null, constraint t1_pk primary key(id));
Table created.
SQL> merge into t1 using (select 1 as id from dual) s on (t1.id = s.id)
when not matched then insert (id) values (s.id);
1 row merged.
SQL>
会话 2(等待会话 1):
SQL> merge into t1 using (select 1 as id from dual) s on (t1.id = s.id)
when not matched then insert (id) values (s.id);
第 1 节:
SQL> commit;
Commit complete.
SQL>
第 2 节:
merge into t1 using (select 1 as id from dual) s on (t1.id = s.id)
when not matched then insert (id) values (s.id)
*
ERROR at line 1:
ORA-00001: unique constraint (BP.T1_PK) violated
SQL>
如果 2 个事务尝试
MERGE
使用新值,则第二个事务将在ORA-00001
第一个事务提交后得到一个。第 1 节:
会话 2(等待会话 1):
第 1 节:
第 2 节:
但是,Oracle 提供了IGNORE_ROW_ON_DUPKEY_INDEX提示,这将抑制第二个事务,
ORA-00001
并且第二个事务可以完成而不会出现错误,并且不会插入有问题的行。第 1 节:
会话 2(等待会话 1):
第 1 节:
第 2 节: