Suppose I have the following table with the following restrictions:
create table test as ( select 1 as id, 'a' as name from dual union all select 2, 'b' from dual union all select 3, 'c' from dual ); create unique index ind on test(name); alter table test add constraint constr unique (name); select * from test; ID NAME
Suppose now that I am doing the following MERGE
:
merge into test t using ( select 4 as id, 'b' as name from dual union all select 2 as id, null as name from dual ) s on (s.id = t.id) when matched then update set t.name = s.name when not matched then insert(t.id, t.name) values(s.id, s.name) select * from test; ID NAME
Is it off above MERGE
? If it is UPDATE
first and then INSERT
s, the index / constraint will not be canceled at run time. But if this is the first INSERT
s and then UPDATE
s, the index will be temporarily invalid and the statement may fail ?.
Can someone explain in detail (or point in the right direction) how Oracle RDBMS deals with such problems? Also, is the same processed when using the LOG ERRORS INTO
?
The main reason I ask this question and why I need a solution: I have MERGE instructions that work for several hours with the LOG ERRORS INTO clause. Error logging seems to work like a standalone transaction. Some unique constraint errors (based on unique indexes) are logged long before the statement completes (among other things, I see that the sequence goes up), and I don't know why (although in the end, after completion, no unique constraint should be canceled). When I look at the ERROR table, I see ORA-00001: a unique constraint (XXX.YYY) is violated during an INSERT operation. I can insert this record from the ERROR table into the main table without causing a unique constraint failure. So I wonder why the error is logged in the first place.
EDIT:. The answers below state that when executing an instruction, restrictions are given at the end of the instruction. I understand and agree (although I would like to know more details about index maintenance in such scenarios). The fact that I do not understand and why this question still has not been answered is the reason that I have these ORA-00001: a unique restriction (XXX.YYY) violated the registered errors while they should not exist. It seems that the error logging mechanism does not behave atomically.
EDIT2:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production PL/SQL Release 11.2.0.4.0 - Production CORE 11.2.0.4.0 Production TNS for Solaris: Version 11.2.0.4.0 - Production NLSRTL Version 11.2.0.4.0 - Production
EDIT3: I played a little and was able to reproduce this error:
drop table test; drop table err_test; create table test as ( select 1 as id, 'a' as name from dual union all select 2, 'b' from dual union all select 3, 'c' from dual ); create unique index ind on test(name); alter table test add constraint constr unique (name);
In the last select * from err_test;
I always get: ORA-00001: unique constraint (XXX.CONSTR) violated
. Now itโs strange that the real MERGE statement (in production) no longer works in PARALLEL, and I still sometimes get this error ...
EDIT4: The best answer that I designated as accepted, although the question itself does not fully answer. This seems to be just a mistake in Oracle.