1 PL/SQL Oracle’s Version of Triggers and PSM. 2 PL/SQL uOracle uses a variant of SQL/PSM which it calls PL/SQL. uPL/SQL not only allows you to create.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
Fall 2001Arthur Keller – CS 18011–1 Schedule Oct. 30 (T) Embedded SQL. u Read Section 8.1. u Assignment 5 due. Not accepted late. u Project Part 4 due.
Fall 2001Arthur Keller – CS 18010–1 Schedule Oct. 25 (TH) More Constraints, Triggers. u Read Sections Oct. 30 (T) Embedded SQL. u Read Section.
1 More SQL Defining a Database Schema Views. 2 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the.
Winter 2002Arthur Keller – CS 18010–1 Schedule Today: Feb. 5 (T) u Triggers, PL/SQL. u Read Sections 7.4, 8.2. Assignment 4 due. Feb. 7 (TH) u PL/SQL,
1 Constraints Foreign Keys Local and Global Constraints Triggers.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
1 Constraints Foreign Keys Local and Global Constraints Triggers.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.
Winter 2002Arthur Keller – CS 18011–1 Schedule Today: Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, Feb. 12 (T) Advising.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Triggers.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Real SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
Winter 2006Keller, Ullman, Cushing10–1 Modification to Views Via Triggers Oracle allows us to “intercept” a modification to a view through an instead-of.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
CS411 Database Systems Kazuhiro Minami 06: SQL. Constraints & Triggers Foreign Keys Local and Global Constraints Triggers.
Advanced SQL: Cursors & Stored Procedures
Advanced SQL Instructor: Mohamed Eltabakh 1 Part II.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
1 Real SQL Programming - 2 Persistent Stored Modules (PSM) PL/SQL Embedded SQL These slides are reused from Jeffrey Ullman’s class with the author’s permission.
Databases 1 9th lecture. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
Advanced SQL: Triggers & Assertions
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
1 Database Systems Defining Database Schema Views.
Introduction to PL/SQL. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
Database Management COP4540, SCS, FIU Database Trigger.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Constraints, Views, Indexes Foreign Keys Constraints Triggers Virtual and Materialized Views Speeding Accesses to Data.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives How to use the SQL programming language How to use SQL cursors How to create stored.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL (modified 11/11/2013)
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: Stored Procedure & Embedded SQL Ch. 9.3, 9.4.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
1 Lecture 11 PL/SQL Slides from
Introduction to Database Systems, CS420
CMSC-461 Database Management Systems
Foreign Keys Local and Global Constraints Triggers
Introduction to Database Systems, CS420
CPSC-310 Database Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
CMSC-461 Database Management Systems
Chapter 8 Advanced SQL Pearson Education © 2009.
CPSC-608 Database Systems
SQL – Constraints & Triggers
Modification to Views Via Triggers
Presentation transcript:

1 PL/SQL Oracle’s Version of Triggers and PSM

2 PL/SQL uOracle uses a variant of SQL/PSM which it calls PL/SQL. uPL/SQL not only allows you to create and store procedures or functions, but it can be run from the generic query interface, like any SQL statement. uTriggers are a part of PL/SQL.

3 Trigger Differences uCompared with SQL standard triggers, Oracle has the following differences: 1.Differences in order of elements. 2.Action is a PL/SQL statement. 3.New/old tuples referenced automatically. 4.Strong constraints on trigger actions designed to make certain you can’t fire off an infinite sequence of triggers.

4 Order of Oracle Trigger Elements 1.CREATE TRIGGER 2.Event, e.g., AFTER INSERT … 3.FOR EACH ROW, if desired. 4.Condition. 5.Action. 6.A dot and the word “run”. These cause the trigger to be installed in the database.

5 New/Old Tuples uInstead of a REFERENCING clause, Oracle assumes that new tuples are referred to as “new” and old tuples by “old.” uAlso, for statement-level triggers: “newtable” and “oldtable”. uIn actions, but not in conditions, you must prefix “new,” etc., by a colon.

6 Example: BeerTrig uRecall our example BeerTrig, which inserted a beer name into Beers whenever a tuple was inserted into Sells with a beer that was not mentioned in Beers. uHere’s the Oracle version of that same trigger.

7 BeerTrig in Oracle SQL CREATE OR REPLACE TRIGGER BeerTrig AFTER INSERT ON Sells FOR EACH ROW WHEN (new.beer NOT IN (SELECT name FROM Beers)) BEGIN INSERT INTO BEERS(name) VALUES(:new.beer); END;. run Note position Notice “new” is understood. Also, colon used only in the action. Needed to store trigger as an element of the database

8 Another Example uRecall PriceTrig, which stores in the relation Ripoffbars(bar) the name of any bar that raises the price of any beer by more than $1. uHere’s the Oracle version.

9 PriceTrig in Oracle CREATE OR REPLACE TRIGGER PriceTrig AFTER UPDATE OF price ON Sells FOR EACH ROW WHEN (new.price > old.price ) BEGIN INSERT INTO RipoffBars VALUES(:new.bar); END;. run

10 Oracle Limitation on Relations Affected uEach trigger is on some one relation R, mentioned in the event. uThe SQL standard puts no constraint on which relations, including R, can be modified in the action. uAs a result, infinite sequences of triggered events are possible.

11 Example: Infinite Triggering uLet R(x) be a unary relation that is a set of integers. uEasy to write a trigger with event INSERT ON R, that as action, inserts i +1 if i was the integer that awakened the trigger. uResults in a never-ending sequence of inserts.

12 Oracle Limitation uOracle is overly conservative about what relations can be changed when the event is on R. uR surely must not be subject to any modification in the action. uBut much trickier: any relation that is linked to R by a chain of foreign-key constraints may not be changed either.

13 Example: Foreign-Key Chains uSuppose R.a is a foreign key, referencing S.b. uAlso, T.c is a foreign key referencing S.b. uThen in a trigger on relation R, neither T nor S may be modified.

14 PL/SQL uIn addition to stored procedures, one can write a PL/SQL statement that looks like the body of a procedure, but is executed once, like any SQL statement typed to the generic interface. wOracle calls the generic interface “sqlplus.” wPL/SQL is really the “plus.”

15 Form of PL/SQL Statements DECLARE BEGIN END;. run uThe DECLARE section is optional.

16 Form of PL/SQL Procedure CREATE OR REPLACE PROCEDURE ( ) AS BEGIN END;. run Notice AS needed here Needed to store procedure in database

17 PL/SQL Declarations and Assignments uThe word DECLARE does not appear in front of each local declaration. wJust use the variable name and its type. uThere is no word SET in assignments, and := is used in place of =. wExample: x := y;

18 PL/SQL Procedure Parameters uThere are several differences in the forms of PL/SQL argument or local- variable declarations, compared with the SQL/PSM standard: 1.Order is name-mode-type, not mode- name-type. 2.INOUT is replaced by IN OUT in PL/SQL. 3.Several new types.

19 PL/SQL Types uIn addition to the SQL types, NUMBER can be used to mean INT or REAL, as appropriate. uYou can refer to the type of attribute x of relation R by R.x%TYPE. wUseful to avoid type mismatches. wAlso, R%ROWTYPE is a tuple whose components have the types of R’s attributes.

20 Example:JoeMenu uRecall the procedure JoeMenu(b,p) that adds beer b at price p to the beers sold by Joe (in relation Sells). uHere is the PL/SQL version.

21 Procedure JoeMenu in PL/SQL CREATE OR REPLACE 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 Notice these types have to be suitable for the intended uses of b and p.

22 PL/SQL Branching Statements uLike IF … in SQL/PSM, but: uUse ELSIF in place of ELSEIF. uViz.: IF … THEN … ELSIF … ELSIF … ELSE … END IF;

23 PL/SQL Loops uLOOP … END LOOP as in SQL/PSM. uInstead of LEAVE …, PL/SQL uses EXIT WHEN uAnd when the condition is that cursor c has found no tuple, we can write c%NOTFOUND as the condition.

24 PL/SQL Cursors uThe form of a PL/SQL cursor declaration is: CURSOR IS ; uTo fetch from cursor c, say: FETCH c INTO ;

25 Example: JoeGouge() in PL/SQL uRecall JoeGouge() sends a cursor through the Joe’s-Bar portion of Sells, and raises by $1 the price of each beer Joe’s Bar sells, if that price was initially under $3.

26 Example: JoeGouge() Declarations CREATE OR REPLACE PROCEDURE JoeGouge() AS theBeer Sells.beer%TYPE; thePrice Sells.price%TYPE; CURSOR c IS SELECT beer, price FROM Sells WHERE bar = ‘Joe’’s Bar’;

27 Example: JoeGouge Body BEGIN OPEN c; LOOP FETCH c INTO theBeer, thePrice; EXIT WHEN c%NOTFOUND; IF thePrice < 3.00 THEN UPDATE Sells SET price = thePrice ; WHERE bar = ‘Joe’’s Bar’ AND beer = theBeer; END IF; END LOOP; CLOSE c; END; How PL/SQL breaks a cursor loop Note this is a SET clause in an UPDATE, not an assignment. PL/SQL uses := for assignments.

28 Tuple-Valued Variables uPL/SQL allows a variable x to have a tuple type. ux R%ROWTYPE gives x the type of R’s tuples. uR could be either a relation or a cursor. ux.a gives the value of the component for attribute a in the tuple x.

29 Example: Tuple Type uHere is the declarations of JoeGouge(), using a variable bp whose type is beer-price pairs, as returned by cursor c. CREATE OR REPLACE PROCEDURE JoeGouge() AS CURSOR c IS SELECT beer, price FROM Sells WHERE bar = ‘Joe’’s Bar’; bp c%ROWTYPE;

30 JoeGouge Body Using bp BEGIN OPEN c; LOOP FETCH c INTO bp; EXIT WHEN c%NOTFOUND; IF bp.price < 3.00 THEN UPDATE Sells SET price = bp.price ; WHERE bar = ‘Joe’’s Bar’ AND beer =bp.beer; END IF; END LOOP; CLOSE c; END; Components of bp are obtained with a dot and the attribute name