oracle 觸發器兩個表的數據同步(同一個庫和不同庫的)

Oracle 觸發器 插入,更新,刪除,數據同步,在同一個數據庫的兩表同步 :

 建表語句:
create table User_Info (
   ID                   INTEGER                        not null,
   UserName            VARCHAR(30)                     not null,
   PassWord            VARCHAR(20)                     not null,
   CreateDate          Date                            not null,
   Status              INTEGER                         not null,
   constraint PK_User_Info primary key (ID)
);

create table User_Info_Temp (
   ID                   INTEGER                        not null,
   UserName            VARCHAR(30)                     not null,
   PassWord            VARCHAR(20)                     not null,
   CreateDate          Date                            not null,
   Status              INTEGER                         not null,
   constraint PK_User_Info_Temp primary key (ID)
);

觸發器寫法:

create or replace trigger UserToTemp after insert or update or delete
on user_info for each row
declare
    integrity_error exception;
    errno            integer;
    errmsg           char(200);
    dummy            integer;
    found            boolean;
   
begin
if inserting then
    insert into User_info_temp(ID,UserName,PassWord,CreateDate,Status) values(:NEW.ID,:NEW.UserName,:NEW.PassWord,:new.CreateDate,:NEW.Status);
elsif updating then
    update User_info_temp set ID=:NEW.ID,UserName=:NEW.UserName,PassWord=:NEW.PassWord,Status=:NEW.Status where id=:OLD.id;
elsif deleting then
    delete from User_info_temp where id=:OLD.id;
end if;
exception
    when integrity_error then
       raise_application_error(errno, errmsg);
end;

 


測試數據:
insert into user_info(ID,UserName,PassWord,CreateDate,Status)values(1,'hello','111',to_date('2015-08-11','yyyy-mm-dd'),1);

 

update user_info u set u.status=3,u.username='world' where u.id=1;

 

delete from user_info u where u.id=1;

 

 

 

+++++++++++++++++不同數據庫建的兩個表同步++++++++++++++++++++++++++++++++

 

 

 

在parking庫建User_Info表
在sfgl庫建User_Info_Temp表

 

----------------------------------------------------------
兩個數據庫的連接dblink:  在parking庫新建的
dblink_usertest  可以任意改,是個dblink連接名稱
sfgl/sfgl  數據庫的用戶名和密碼
orcl 數據庫連接標識符

 

 

 

create public database link dblink_usertest
  connect to sfgl identified by sfgl
  using 'orcl';
 

 

----------------------------------------------------------
觸發器: 新建在parking庫上的

 

create or replace trigger UserToTemp after insert or update or delete
on User_Info for each row
declare
    integrity_error exception;
    errno            integer;
    errmsg           char(200);
    dummy            integer;
    found            boolean;
   
begin
if inserting then
    insert into User_Info_Temp@dblink_usertest(ID,UserName,PassWord,CreateDate,Status) values(:NEW.ID,:NEW.UserName,:NEW.PassWord,:new.CreateDate,:NEW.Status);
elsif updating then
    update User_Info_Temp@dblink_usertest set ID=:NEW.ID,UserName=:NEW.UserName,PassWord=:NEW.PassWord,Status=:NEW.Status where id=:OLD.id;
elsif deleting then
    delete from User_Info_Temp@dblink_usertest where id=:OLD.id;
end if;
exception
    when integrity_error then
       raise_application_error(errno, errmsg);
end;
 
----------------------------------------------------------
select * from User_Info@dblink_usertest2;
select * from User_Info_Temp@dblink_usertest;
 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章