有两个具有外键关系但没有外键约束的远程表A_REM和B_REM,以及表A_REM中的大约10,000行和表B_REM中的每天50,000行。大多数操作是插入操作。现在,我想将数据从A_REM和B_REM移动到本地表A_LOC和B_LOC,并在处理过程中锁定行。移动后,应删除表A_REM和B_REM中的行。
A_REM B_REM
1 ----- |_ 1
|_ 2
|_ 3
--------------------
2 ----- |_ 4
|_ 5 rows 2 and 4-6 are locked while moving
|_ 6
--------------------
3 ----- |_ 7
|_ 8
|_ 9什么是移动数据的最佳方法,同时保持表A_REM和B_REM中的数据之间的关系(一致性)。如果我只有一个表,我将结合游标(01/appdev.112/e25519/static.htm#CHDGEHBF)使用"FOR“语句。
在此之前,非常感谢您。
发布于 2013-05-03 17:44:45
如果事务中只有一个远程数据库,则它不是分布式事务,因此它的行为与本地事务相同(与跨多个远程数据库的分布式事务相反)。下面的示例演示如何获得正确的行级锁,并以所需的数据一致性在单个工作单元中移动数据。
create table a_rem ( aid number, acontent varchar2(10) );
create table b_rem ( bid number, aid number, bcontent varchar(10) );
create table a_loc ( aid number, acontent varchar2(10) );
create table b_loc ( bid number, aid number, bcontent varchar(10) );
insert into a_rem values ( 1, 'A-One' );
insert into a_rem values ( 2, 'A-Two' );
insert into a_rem values ( 3, 'A-Three' );
insert into b_rem values ( 1, 1, 'B-One' );
insert into b_rem values ( 2, 1, 'B-Two' );
insert into b_rem values ( 3, 2, 'B-Three' );
insert into b_rem values ( 4, 3, 'B-Four' );
insert into b_rem values ( 5, 3, 'B-Five' );
commit;
-- look Ma, no data integrity! :(
-- let us pretend I want to move a_rem.aid = 2 information from both tables
declare
cursor row_level_locks is
select a.*, b.*
from a_rem a, b_rem b
where a.aid = b.aid and a.aid = 2
for update;
begin
open row_level_locks; -- begins transaction, obtains proper row level locking
insert into a_loc select * from a_rem where aid = 2;
insert into b_loc select * from b_rem where aid = 2;
delete a_rem where aid = 2;
delete b_rem where aid = 2;
commit;
close row_level_locks;
end;最后注意:如果两个表之间的数据完整性很重要,那么继续在B_REM引用A_REM上创建外键约束。如果不能/不会,那么数据完整性就不重要了。这不可能既是重要的,又是微不足道的一步,以确保它不采取。
https://stackoverflow.com/questions/16286533
复制相似问题