如题:我在同一个数据库下想要实现:表A,表B,A和B是数据表名。名字不同,但是他们里面的字段完全相同,
现在我想实现A中的数据和B中的数据同步!也就是A中的数据由变动时,B的数据跟着变动!
如果可以实现的话!请大家指点一下 !谢谢了
现在我想实现A中的数据和B中的数据同步!也就是A中的数据由变动时,B的数据跟着变动!
如果可以实现的话!请大家指点一下 !谢谢了
解决方案 »
- 请教:10g 的本地捕获和下游捕获到底哪个的延迟时间较长啊?
- 求大神指点,介绍下oracle索引到底是干什么用的?什么原理?基础点就好~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 看了半天的贴子,还是没解决--ORACLE10g 实例问题
- 新手提问: 如何获取到表得首次更新时间 和 最后一次更新时间?
- [高难度]这样的触发器应该怎么写?
- 运行SQLLDR工具是不是必须安装ORACLE?
- oracle 11i的技术突破和版本命名?
- 请教大家,日志是怎么做的?
- 谁能给我提供oracle 817 spatial guide的印刷版?
- 请问ORACL中有什么命令可以把表结构和数据导出一个文件,就像MYSQL那样的导成一个.SQL有吗?
- 关于oracle行列转换
- 弱弱的问一个问题
Oracle merge into 的用法详解实例作用:merge into 解决用B表跟新A表数据,如果A表中没有,则把B表的数据插入A表;语法:MERGE INTO [your table-name] [rename your table here]USING ( [write your query here] )[rename your query-sql and using just like a table]ON ([conditional expression here] AND [...]...)WHEN MATHED THEN [here you can execute some update sql or something else ]WHEN NOT MATHED THEN [execute something else here ! ]-------实例-------
merge into a
using (select id,name from b ) c
on(a.id=c.id )
when matched then update set a.name=c.name
when not matched then insert (a.id,a.name) values (c.id,c.name);
作用:利用表 b 跟新表a ,条件是a.id=b.id,如果a表中没有该条件的数据就插入。如果你的数据量很大,此sql效率非常高。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tianlesoftware/archive/2009/10/23/4714921.aspx
create or replace trigger tg_a
before insert or update or delete on a
for each row
begin
if inserting then insert into b values (:new.col1,:new.col2...);end if;
if updating then update b set (col1,col2,col3..)=(:new.col1,:new.col2,:new.col3...)where col1(主键)=:old.col1; end if;
if deleting then delete from b where col1=:old.col1; end if;
end;
---------------------------------------------
那b表有什么用?建个view不就行了。
触发器
create or replace trigger tg_a
before insert or update or delete on a
for each row
begin
if inserting then insert into b values (:new.col1,:new.col2...);end if;
if updating then update b set (col1,col2,col3..)=(:new.col1,:new.col2,:new.col3...)where col1(主键)=:old.col1; end if;
if deleting then delete from b where col1=:old.col1; end if;
end;每天同步:
merge into a
using (select id,name from b ) c
on(a.id=c.id )
when matched then update set a.name=c.name
when not matched then insert (a.id,a.name) values (c.id,c.name); 触发器用多了,不好。
建立表B的时候就关联好表A,让表B永远自动跟着表A变化!
---------------------------------------------
如果A,B表一样,就不要浪费表空间了。建个view就行了。
你想实现类似级联的效果。在oracle里面实现不了,除了写触发器外
create table ta(id int primary key,name varchar2(10));
create table tb(id int primary key,name varchar2(10));create or replace trigger trig_ta
before insert or update or delete
on ta
for each row
begin
if inserting then
insert into tb(id,name) select :new.id,:new.name from dual;
end if;
if updating then
update tb set name= :new.name
where id =:old.id;
end if;
if deleting then
delete from tb where id =:old.id;
end if;end;insert into ta select 1,'aa' from dual;
insert into ta select 2,'bb' from dual;select * from tb;update ta set name ='cc';
select * from tb;delete from ta where id =1 ;select * from tb;
create table ta(id int primary key,name varchar2(10));
create table tb(id int primary key,name varchar2(10));merge into tb a
using (select id,name from ta) b
on (a.id=b.id)
when matched then update set a.name=b.name
when not matched then insert (a.id,a.name) values(b.id,b.name)