求:数据恢复的语句!

bigsir 2006-07-03 03:52:41
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语句不太熟练,请大家指点一下。
...全文
231 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigsir 2006-07-04
  • 打赏
  • 举报
回复
to xiaoxiao1984(笨猫一只^_^) :

非常感谢,我去试试,如果不行再给你发消息。
xiaoxiao1984 2006-07-03
  • 打赏
  • 举报
回复
--------插入,更新触发器
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;
issgates 2006-07-03
  • 打赏
  • 举报
回复
看看oracle触发器与存储过程便知,很简单的。

17,089

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧