求:数据恢复的语句!
t_leaf:
id name time no
1 a ...... 2
2 b ....... 100
2 b ....... 100
2 b ....... 100
t_root:
id name time Total_no
2 b ....... 1000
期望,我在t_leaf中增加一条后,对应t_root中的Total_no会自动减少t_leaf中的no的数字,
同理在t_leaf中删除一条厚,对应t_root中的Total_no会自动增加t_leaf中的no的数字
举例:t_leaf中心赠加
id name time no
1 b ...... 2
那么这时候t_root中应该是
id name time Total_no
2 b ....... 998
举例:t_leaf中心删除一条记录
id name time no
1 b ...... 200
那么这时候t_root中应该是
id name time Total_no
2 b ....... 1198
我的想法是通过trigger来实现,不过sql语句不太熟练,请大家指点一下。
问题点数:100、回复次数:3Top
1 楼issgates(一直很安静)回复于 2006-07-03 17:16:19 得分 0
看看oracle触发器与存储过程便知,很简单的。Top
2 楼xiaoxiao1984(笨猫儿)回复于 2006-07-03 17:29:38 得分 100
--------插入,更新触发器
create or replace trigger trigger_leaf_insert_update
before insert or update on t_leaf
for each row
declare
cnt number;
begin
select count(1) into cnt from t_root where id = :new.id and name = :new.name;
if cnt = 0 then
insert into t_root(id, name, time, Total_no)
values(:new.id, :new.name, :new.time, :new.no);
else
update t_root set Total_no = Total_no - nvl(:new.no,0) + nvl(:old.no,0)
where id = :new.id and name = :new.name;
end if ;
end trigger_leaf_insert_update;
--删除触发器
create or replace trigger trigger_leaf_delete
before delete on t_leaf
for each row
begin
update t_root set Total_no = Total_no + :old.no
where id = :old.id and name = :old.name;
end trigger_leaf_delete;
Top
3 楼bigsir(bigsir)回复于 2006-07-04 10:05:14 得分 0
to xiaoxiao1984(笨猫一只^_^) :
非常感谢,我去试试,如果不行再给你发消息。Top




