简体   繁体   中英

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.

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? What approach would be the best?

Let's start with DBlink to remote DB

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.

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. 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. It is best to dig in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM