[英]Copy new records from one database to another
I have local and remote database. 我有本地和远程数据库。 The local database is a replica of remote database. 本地数据库是远程数据库的副本。 I have to insert new records from remote database into local database. 我必须将远程数据库中的新记录插入本地数据库。 There are 14 tables in remote database, so i need to track changes in all 14 tables. 远程数据库中有14个表,因此我需要跟踪所有14个表中的更改。
I know I can select every table in remote database and check if record exists in local database if not then insert it. 我知道我可以选择远程数据库中的每个表,如果没有,则检查本地数据库中是否存在记录然后插入。
Is there another way to do it? 还有另一种方法吗? In java maybe? 在java中可能吗? What approach would be the best? 什么方法最好?
Let's start with DBlink to remote DB 让我们从DBlink开始到远程数据库
CREATE PUBLIC DATABASE LINK REMO
CONNECT TO <user_name>
IDENTIFIED BY <password>
USING '<service_name>';
then insert with loop over all tables from user schema. 然后在用户模式的所有表中插入循环。 Or if you want insert only some tables please define proper condition for those 14 tables. 或者如果您只想插入一些表,请为这14个表定义适当的条件。
begin
for x in (select table_name from user_tables) loop
execute immediate 'insert into ' || x.table_name || ' (select * from ' || x.table_name || '@REMO minus select * from ' || x.table_name || ')';
commit;
end loop;
end;
/
This will insert only new records (in fact records that exists (or were changed) on remote and not exists on local). 这将仅插入新记录(实际上是在远程上存在(或已更改)的记录而在本地不存在)。 If taht's what you need this will work. 如果这是你需要的,这将是有效的。 If you need synchronisation this is not the solution. 如果您需要同步,这不是解决方案。
if you have only one local database and it runs all the time - you can use triggers from remote db to insert to the local db. 如果您只有一个本地数据库并且它一直运行 - 您可以使用远程数据库中的触发器插入到本地数据库。
BTW: Go from local to check remote manually is not a good idea. BTW:从本地去手动检查远程不是一个好主意。 What if existing remote record changed, but local still have old values? 如果现有远程记录发生了变化,但本地仍有旧值,该怎么办?
Also there bunch of Oracle tools to make replications and synchronizations between databases. 还有一堆Oracle工具可以在数据库之间进行复制和同步。 It is best to dig in it. 最好深入挖掘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.