SLIDE 1 Mo dication to Views Via T riggers Oracle allo ws us to \in tercept" a mo dication to a view through an instead-of trigger. Example Likes(drinker , beer ) Sells(bar , beer , price) Frequents(drinker , bar) CREATE VIEW Synergy AS SELECT Likes.drinker, Likes.beer, Sells.bar FROM Likes, Sells, Frequents WHERE Likes.drinker = Frequents.drinker AND Likes.beer = Sells.beer AND Sells.bar = Frequents.bar; 1
Mo dication to Views Via T riggers Oracle allo ws us to - - PDF document
Mo dication to Views Via T riggers Oracle allo ws us to - - PDF document
Mo dication to Views Via T riggers Oracle allo ws us to \in tercept" a mo dication to a view through an instead-of trigger. Example Likes(drinker , beer ) Sells(bar , beer , price) Frequents(drinker ,
SLIDE 2 CREATE TRIGGER ViewTrig INSTEAD OF INSERT ON Synergy FOR EACH ROW BEGIN INSERT INTO Likes VALUES( :new.drinker, :new.beer); INSERT INTO Sells(bar, beer) VALUES(:new.bar, :new.beer); INSERT INTO Frequents VALUES( :new.drinker, :new.bar); END; . run 2
SLIDE 3 SQL T riggers
- Read
- Some
- ut
- f
- r
- ther
- f
SLIDE 4 PL/SQL
- Oracle's
- f
✦
Use via sqlplus.- A
- Allo
- ps,
- f
- ne
- Rough
- DECLARE
- rtion
- ptional.
- Dot
- f
SLIDE 5 Simplest F
- rm:
- f
SLIDE 6 Pro cedures Stored database
- b
- dy
SLIDE 7
- Argumen
✦
Mo de: IN, OUT,- r
- nly
✦
T yp es: standard SQL + generic t yp es lik e NUMBER = an y in teger- r
✦
Since t yp es in pro cedures must matc h their t yp es in the DB sc hema, y- u
- f
SLIDE 8 Example A pro cedure to tak e a b eer and price and add it to Jo e's men u. Sells(bar , beer , price) CREATE PROCEDURE joeMenu( b IN Sells.beer%TYPE, p IN Sells.price%TYPE ) AS BEGIN INSERT INTO Sells VALUES('Joe''s Bar', b, p); END; . run;
- Note
- nly
SLIDE 9 In v
- king
- dy
- f
- Example:
- ol',
SLIDE 10 Assignmen t Assign expressions to declared v ariables with :=. Branc hes IF <condition> THEN <statemen t(s)> ELSE <statemen t(s)> END IF;
- But
- f
- ps
SLIDE 11 Queries in PL/SQL 1. Single-r
- w
- f
- f
- ne
- f
- p
SLIDE 12 Single-Ro w Select
- Select-from-where
- It
- r
- ne
- u
- n
- r).
SLIDE 13 Cursors Declare b y: CURSOR <name> IS select-from-where statemen t
- Cursor
- p.
✦
F etc h statemen t: FETCH <cursor name> INTO v ariable list;- Break
- p
- f
✦
T rue when there are no more tuples to get.- Op
SLIDE 14 Example A pro cedure that examines the men u for Jo e's Bar and raises b y $1.00 all prices that are less than $3.00. Sells(bar , beer , price)
- This
SLIDE 15 CREATE PROCEDURE joeGouge() AS theBeer Sells.beer%TYPE; thePrice Sells.price%TYPE; CURSOR c IS SELECT beer, price FROM Sells WHERE bar = 'Joe''s bar'; BEGIN OPEN c; LOOP FETCH c INTO theBeer, thePrice; EXIT WHEN c%NOTFOUND; IF thePrice < 3.00 THEN UPDATE Sells SET price = thePrice + 1.00 WHERE bar = 'Joe''s Bar' AND beer = theBeer; END IF; END LOOP; CLOSE c; END; . run 15
SLIDE 16 Ro w T yp es An ything (e.g., cursors, table names) that has a tuple t yp e can ha v e its t yp e captured with %ROWTYPE.
- W
- rary
- nen
- Handy
SLIDE 17 Example The same pro cedure with a tuple v ariable bp. CREATE PROCEDURE joeGouge() AS CURSOR c IS SELECT beer, price FROM Sells WHERE bar = 'Joe''s bar'; bp c%ROWTYPE; BEGIN OPEN c; LOOP FETCH c INTO bp; EXIT WHEN c%NOTFOUND; IF bp.price < 3.00 THEN UDPATE Sells SET price = bp.price + 1.00 WHERE bar = 'Joe''s Bar' AND beer = bp.beer; END IF; END LOOP; CLOSE c; END; . run 17