简体   繁体   English

将新记录从一个数据库复制到另一个数

[英]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.

相关问题 将一个人的记录从一个数据库复制到另一个数据库 - Oracle 和 Java - Copy records of a person from one database to another database - Oracle and Java 将包含数百万条记录的表从一个数据库复制到另一个数据库 - Spring Boot + Spring JDBC - Copy table having millions of records from one database to another - Spring Boot + Spring JDBC 如何将表从一个数据库复制到另一个数据库? - How to copy table from one database to another? 将数据库从一个应用程序复制到另一个应用程序 - Copy database from one app to another app 将数据库设置从一个项目复制到另一个项 - copy database setup from one project to another 如何仅将新项目从一个列表复制到另一个 - How to copy only new items from one list to another 如何将选择性数据从一个数据库复制到另一个数据库(ORACLE) - How to copy selective data from one database to another (ORACLE) 我可以从另一个数据库复制一个数据库中的表吗? - Can I copy a table in one databsae from another database? 从一个表读取行并将其复制到Java中的另一个数据库表 - Reading row from one table and copy it to another table of database in Java 最佳实践-在集群环境中使用Quartz从一个数据库读取记录,验证并插入另一个数据库 - Best Practices - Reading records from one database, validate and insert into another database using Quartz in clustered environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM