|
|
Trigger qui maintient une table en refletant les
modifications apportées a une autre
/*
|| Radu Caulea (aka prof) et Smahine H.)
|| Trigger qui denormalise une base. Soit une table volumineuse
|| qui est accedee regulierement de la maniere select sum(aa) where spec...
|| Créeons une autre table avec autant d'entrées que spec ...
|| Mettons un trigger qui la maintient. Obs: Pas de maj trop frequentes.
|| Merci Sophie pour la mauvaise conscience ;-). Si on truncate la premiere
|| table, la deuxieme n'est pas touchée.
*/
set echo on;
drop table taba;
drop table tabb;
create table taba
( aa varchar2(2),
bb number
);
create table tabb as select * from taba where 1=2;
drop index idxtb;
create unique index idxtb on tabb(aa asc)
/
create or replace trigger t_taba
before insert or update or delete
on taba
for each row
begin
if inserting then
begin
insert into tabb values(:new.aa,:new.bb);
exception
when dup_val_on_index then
update tabb set bb=bb+:new.bb
where aa=:new.aa;
end;
end if;
if updating then
begin
update tabb set bb=bb-:old.bb
where aa=:new.aa;
update tabb set bb=bb+:new.bb
where aa=:new.aa;
end;
end if;
if deleting then
begin
update tabb set bb=bb-:old.bb
where aa=:old.aa;
end;
end if;
end;
/
|