Views, Triggers and Recursive Queries
Views and Recursive Queries Assume we have a relation Par(child, parent). Write a view that contains all pairs of siblings. Write a view that contains all pairs of direct cousins. Write a query that computes all (direct or indirect) cousins.
Views create table a(col1 integer, col2 integer); create view viewA as select col1 from a where col1>col2; insert into a values(1,2); insert into a values(13,7); insert into a values(16,19); select * from viewA; update viewA set col1 = col1 + 2 where col1<15 select * from a; select * from viewA; update a set col1 = col1 + 2 where col1<15 What will be returned on line 6? On line 8? On line 10?
Triggers create or replace function mystery() returns trigger as $$ begin new.a := (new.a + old.a)/2; new.b := (new.b + old.b)/2; return new; end $$ language plpgsql; create trigger mysterytrig before update on nums for each row execute procedure mystery(); update nums set a = a+2 where b>4; select * from nums; nums a b 1 5 2 6 3
Triggers create or replace function mystery() returns trigger as $$ begin new.a := (new.a + old.a)/2; new.b := (new.b + old.b)/2; return new; end $$ language plpgsql; create trigger mysterytrig after update on nums for each row execute procedure mystery(); update nums set a = a+2 where b>4; select * from nums; nums a b 1 5 2 6 3
Triggers create or replace function enigma() returns trigger as $$ begin insert into second values(new.A * new.A); update second set b=b+1 where b>12; return null; end $$ language plpgsql; create trigger enigmatrig after insert on first for each row execute procedure enigma(); insert into first values(5); insert into first values(2); select * from first; select * from second; first a 10 21 second b
Triggers create or replace function enigma() returns trigger as $$ begin insert into second values(new.A * new.A); update second set b=b+1 where b>12; return null; end $$ language plpgsql; create trigger enigmatrig before insert on first for each row execute procedure enigma(); insert into first values(5); insert into first values(2); select * from first; select * from second; first a 10 21 second b
Triggers create or replace function enigma() returns trigger as $$ begin insert into second values(new.A * new.A); update second set b=b+1 where b>12; return new; end $$ language plpgsql; create trigger enigmatrig before insert on first for each row execute procedure enigma(); insert into first values(5); insert into first values(2); select * from first; select * from second; first a 10 21 second b
Triggers create or replace function enigma() returns trigger as $$ begin insert into second values(new.A * new.A); update second set b=b+1 where b>12; return old; end $$ language plpgsql; create trigger enigmatrig before insert on first for each row execute procedure enigma(); insert into first values(5); insert into first values(2); select * from first; select * from second; first a 10 21 second b
Triggers create or replace function enigma() returns trigger as $$ begin insert into second values(old.A * new.A); update second set b=b+1; return old; end $$ language plpgsql; create trigger enigmatrig after update on first for each row execute procedure enigma(); update first set a = a+3; select * from first; select * from second; first a 1 2 second b
Triggers create or replace function what() returns trigger as $$ declare r nums%ROWTYPE; begin select max(a) into r from nums; IF r.a<new.a THEN insert into nums values(new.a-1); END IF; RETURN new; end $$ language plpgsql; create trigger whattrig before insert on nums for each row execute procedure what(); insert into nums values(4); select * from nums; insert into nums values(0); select * from nums; nums a 1
Triggers create or replace function whatr() returns trigger as $$ begin insert into S values(1); RETURN null; end $$ language plpgsql; create or replace function whats() returns trigger as $$ insert into T values(1); R a S a T a
Triggers (cont) create trigger whatstrig before insert on S for each statement execute procedure whats(); create trigger whatrtrig before insert on R execute procedure whatr(); insert into R values(0); select * from R; select * from S; select * from T; insert into S values(0); R a S a T a