oracle 触发器两个表的数据同步(同一个库和不同库的)

您所在的位置:网站首页 oracle数据库触发器怎么写 oracle 触发器两个表的数据同步(同一个库和不同库的)

oracle 触发器两个表的数据同步(同一个库和不同库的)

2024-07-17 09:02:57| 来源: 网络整理| 查看: 265

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 deleteon user_info for each rowdeclare    integrity_error exception;    errno            integer;    errmsg           char(200);    dummy            integer;    found            boolean;    beginif 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 deleteon User_Info for each rowdeclare    integrity_error exception;    errno            integer;    errmsg           char(200);    dummy            integer;    found            boolean;    beginif 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; 

 

 

 



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭