Presentation is loading. Please wait.

Presentation is loading. Please wait.

Views, Triggers and Recursive Queries

Similar presentations


Presentation on theme: "Views, Triggers and Recursive Queries"— Presentation transcript:

1 Views, Triggers and Recursive Queries

2 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.

3 Solution (Part 1) CREATE VIEW Siblings AS SELECT P1.child as sib1, P2.child as sib2 FROM Par P1, Par P2 WHERE P1.parent = P2.parent and P1.child<>P2.child

4 Solution (Part 2) CREATE VIEW DCousins AS SELECT P1.child as cous1, P2.child as cous2 FROM Par P1, Par P2, Siblings S WHERE P1.parent = sib1 and P2.parent = sib2

5 Solution (Part 3) WITH Recursive Cousins(C1, C2) AS (SELECT * FROM DCousins) UNION (SELECT P1.child, P2.child FROM Par P1, Par P2, Cousins C WHERE P1.parent = c1 and P2.parent = c2) SELECT * FROM Cousins

6 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 10.select * from a; select * from viewA; What will be returned on line 6? On line 8? On line 10?

7 Solution Line 6: 13 Line 8: Line 10:
Select * from a: (1,2), (15,7), (16,19) Select * from viewA: 15 Line 10: Select * from a: (3,2), (15,7), (16,19) Select * from viewA: 15, 3

8 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; Solution: (2,5), (3,6), (6,3) nums a b 1 5 2 6 3

9 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; Solution: (3,5), (4,6), (6,3) nums a b 1 5 2 6 3

10 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; Solution: First:10, 21, 5, 2 Second: 27, 4 first a 10 21 second b

11 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; Solution: First:10, 21 Second: 27, 4 first a 10 21 second b

12 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; Solution: First:10, 21, 5, 2 Second: 27, 4 first a 10 21 second b

13 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; Solution: First:10, 21 Second: 27, 4 first a 10 21 second b

14 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; Solution: The answer is order dependent. First will have 4,5. We will either get: Second with 6, 11 or Second with 5, 12 first a 1 2 second b

15 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; Solution: The first select will return 1,1,2,3,4 The second select will return 1,1,2,3,4,0 nums a 1

16 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

17 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); Solution: After first insert R: 0, S: 1, T: 1 After second insert R: 0 , S: 0, 1, T: 1, 1 R a S a T a


Download ppt "Views, Triggers and Recursive Queries"

Similar presentations


Ads by Google