oracle两个表数据同步备份
这段时间做sqoop2数据导出,需要先将数据导入到中间表,然后将中间表的数据同步到目的表,中间表和目的表字段完全一样,只是表的名称不一致。
方式一:触发器
触发器:是一个与表相关联的、存储的PL/SQL程序。每当一个特定的数据操作语句(Insert,update,delete)在指定的表上发出时,Oracle自动地执行触发器中定义的语句序列。 相当于监视器,对表中数据进行增,删,改时候自动工作,如果合法,才让操作;
1、触发器作用
数据确认
实施复杂的安全性检查
做审计,跟踪表上所做的数据操作等
数据的备份和同步
2、触发器语法
CREATE [or REPLACE] TRIGGER 触发器名 {BEFORE | AFTER} {DELETE | INSERT | UPDATE [OF 列名]} ON 表名 [FOR EACH ROW [WHEN(条件) ] ] declare …… begin PLSQL 块 End [触发器名]; create or replace trigger TGR_NAME after insert or update on power_forecast for each row begin if inserting then insert into t_powergrid_damage_forecast (pid,companyid,dept_code,forest_type,refid,line_fiveh_forecast,line_twoh_forecast,line_one h_forecast,line_thirtyh_forecast,line_tenh_forecast,station_fiveh_forecast,station_twoh_forecast,station_oneh_forecast,station_thirtyh_forecast,platformarea_forecast,user_forecast,spare1,spare2,spare3,create_date,create_userid) values (:NEW.pid,:NEW.companyid,:NEW.dept_code,:NEW.forest_type,:NEW.refid,:NEW.line_fiveh_forecast,:NEW.line_twoh_forecast,:NEW.line_oneh_forecast,:NEW.line_thirtyh_forecast,:NEW.line_tenh_forecast,:NEW.station_fiveh_forecast,:NEW.station_twoh_forecast,:NEW.station_oneh_forecast,:NEW.station_thirtyh_forecast,:NEW.platformarea_forecast,:NEW.user_forecast,:NEW.spare1,:NEW.spare2,:NEW.spare3,:NEW.create_date,:NEW.create_userid); end if; end;
power_forecast : 中间表
t_powergrid_damage_forecast : 目的表
3、删除触发器
drop trigger TGR_NAME
方式二、存储过程
存储过程:一组为了完成特定功能事先编译好的pl/sql程序块,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。
1、语法
create [or replace] procedure 过程名[(参数名 in/out 数据类型)] AS | IS //声明变量 begin PLSQL子程序体; End [过程名]; create or replace procedure addData AS fore_date date; begin SELECT MAX(create_date) INTO fore_date FROM t_powergrid_damage_forecast; INSERT INTO t_powergrid_damage_forecast SELECT * FROM power_forecast t Where to_char(t.create_date, 'yyyy-mm-dd hh24:mi:ss') > to_char(fore_date, 'yyyy-mm-dd hh24:mi:ss'); COMMIT; End;
---提交存储过程
begin addData; end;
----提交存储过程
call addData();
删除存储过程
drop procedure addData
更多java、大数据学习面试资料,请扫码关注我的公众号:
专注于大数据和java开发,学习交流可以关注我的公众号:javaydsj