t_leaf:
id name time no
1 a ...... 2
2 b ....... 100
2 b ....... 100
2 b ....... 100t_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语句不太熟练,请大家指点一下。
id name time no
1 a ...... 2
2 b ....... 100
2 b ....... 100
2 b ....... 100t_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语句不太熟练,请大家指点一下。
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;