1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.

Slides:



Advertisements
Similar presentations
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives u How to use the SQL programming language u How to use SQL cursors u How to create.
Advertisements

BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
UJeffrey D. Ullman uAnfang von: CS145 - Herbst Stanford University uOnline unter: Folien mit weißem Hintergrund wurden.
1 Combining SQL and Conventional Programming Languages Source: slides by Jeffrey Ullman.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
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.
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.
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.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
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 SQL/PSM Procedures Stored in the Database General-Purpose Programming.
1 Real SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Winter 2006Keller, Ullman, Cushing10–1 Modification to Views Via Triggers Oracle allows us to “intercept” a modification to a view through an instead-of.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Real SQL Programming Embedded SQL Java Database Connectivity Stored Procedures.
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.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
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.
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 Stored Procedure used in PosgreSQL.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
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.
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 Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
SQL in the real world 1. The Three-Tier Architecture of Database Applications browser network HTTP Web server Application server Database server database.
SQL in the real world 1. The SQL Client-Server Interactions SQL-clientSQL-server Environment SQL-agent Module Connection Session Current catalog and schema.
1 Data Modification with SQL CREATE TABLE, INSERT, DELETE, UPDATE Slides from
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Embedded SQL. u Read Sections 8.1, 8.5. Next u Transaction concepts, security u Read Sections 8.6 – 8.7.
1 Lecture 11 PL/SQL Slides from
Introduction to Database Systems, CS420
Database Design and Programming
Slides are reused by the approval of Jeffrey Ullman’s
CMSC-461 Database Management Systems
Foreign Keys Local and Global Constraints Triggers
CPSC-608 Database Systems
CPSC-310 Database Systems
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
Information Management
CPSC-608 Database Systems
Modification to Views Via Triggers
CSC215 Lecture Control Flow.
Stored Procedure Language
Instructor: Zhe He Department of Computer Science
Presentation transcript:

1 SQL/PSM Procedures Stored in the Database General-Purpose Programming

2 Stored Procedures uPSM, or “persistent, stored modules,” allows us to store procedures as database schema elements. uPSM = a mixture of conventional statements (if, while, etc.) and SQL. uLets us do things we cannot do in SQL alone.

3 Basic PSM Form CREATE PROCEDURE ( ) ; uFunction alternative: CREATE FUNCTION ( ) RETURNS

4 Parameters in PSM uUnlike the usual name-type pairs in languages like C, PSM uses mode- name-type triples, where the mode can be: wIN = procedure uses value, does not change value. wOUT = procedure changes, does not use. wINOUT = both.

5 Example: Stored Procedure uLet’s write a procedure that takes two arguments b and p, and adds a tuple to Sells(bar, beer, price) that has bar = ’Joe’’s Bar’, beer = b, and price = p. wUsed by Joe to add to his menu more easily.

6 The Procedure CREATE PROCEDURE JoeMenu ( INbCHAR(20), IN pREAL ) INSERT INTO Sells VALUES(’Joe’’s Bar’, b, p); Parameters are both read-only, not changed The body --- a single insertion

7 Invoking Procedures uUse SQL/PSM statement CALL, with the name of the desired procedure and arguments. uExample: CALL JoeMenu(’Moosedrool’, 5.00); uFunctions used in SQL expressions wherever a value of their return type is appropriate.

8 Types of PSM statements --- (1) uRETURN sets the return value of a function. wUnlike C, etc., RETURN does not terminate function execution. uDECLARE used to declare local variables. uBEGIN... END for groups of statements. wSeparate statements by semicolons.

9 Types of PSM Statements --- (2) uAssignment statements: SET = ;  Example: SET b = ’Bud’; uStatement labels: give a statement a label by prefixing a name and a colon.

10 IF Statements uSimplest form: IF THEN END IF; uAdd ELSE if desired, as IF... THEN... ELSE... END IF; uAdd additional cases by ELSEIF : IF … THEN … ELSEIF … ELSEIF … ELSE … END IF;

11 Example: IF uLet’s rate bars by how many customers they have, based on Frequents(drinker,bar). w<100 customers: ‘unpopular’. w customers: ‘average’. w>= 200 customers: ‘popular’. uFunction Rate(b) rates bar b.

12 Example: IF (continued) CREATE FUNCTION Rate (IN b CHAR(20) ) RETURNS CHAR(10) DECLARE cust INTEGER; BEGIN SET cust = (SELECT COUNT(*) FROM Frequents WHERE bar = b); IF cust < 100 THEN RETURN ’unpopular’ ELSEIF cust < 200 THEN RETURN ’average’ ELSE RETURN ’popular’ END IF; END; Number of customers of bar b Return occurs here, not at one of the RETURN statements Nested IF statement

13 Loops uBasic form: LOOP END LOOP; uExit from a loop by: LEAVE uThe is associated with a loop by prepending the name and a colon to the keyword LOOP.

14 Example: Exiting a Loop loop1: LOOP... LEAVE loop1;... END LOOP; If this statement is executed... Control winds up here

15 Other Loop Forms uWHILE DO END WHILE; uREPEAT UNTIL END REPEAT;

16 Queries uGeneral SELECT-FROM-WHERE queries are not permitted in PSM. uThere are three ways to get the effect of a query: 1.Queries producing one value can be the expression in an assignment. 2.Single-row SELECT... INTO. 3.Cursors.

17 Example: Assignment/Query uIf p is a local variable and Sells(bar, beer, price) the usual relation, we can get the price Joe charges for Bud by: SET p = (SELECT price FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’);

18 SELECT... INTO uAn equivalent way to get the value of a query that is guaranteed to return one tuple is by placing INTO after the SELECT clause. uExample: SELECT price INTO p FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’;

19 Cursors uA cursor is essentially a tuple-variable that ranges over all tuples in the result of some query. uDeclare a cursor c by: DECLARE c CURSOR FOR ;

20 Opening and Closing Cursors uTo use cursor c, we must issue the command: OPEN c; wThe query of c is evaluated, and c is set to point to the first tuple of the result. uWhen finished with c, issue command: CLOSE c;

21 Fetching Tuples From a Cursor uTo get the next tuple from cursor c, issue command: FETCH FROM c INTO x1, x2,…,xn ; uThe x ’s are a list of variables, one for each component of the tuples referred to by c. uc is moved automatically to the next tuple.

22 Breaking Cursor Loops --- (1) uThe usual way to use a cursor is to create a loop with a FETCH statement, and do something with each tuple fetched. uA tricky point is how we get out of the loop when the cursor has no more tuples to deliver.

23 Breaking Cursor Loops --- (2) uEach SQL operation returns a status, which is a 5-digit number. wFor example, = “Everything OK,” and = “Failed to find a tuple.” uIn PSM, we can get the value of the status in a variable called SQLSTATE.

24 Breaking Cursor Loops --- (3) uWe may declare a condition, which is a boolean variable that is true if and only if SQLSTATE has a particular value.  Example: We can declare condition NotFound to represent by: DECLARE NotFound CONDITION FOR SQLSTATE ’02000’;

25 Breaking Cursor Loops --- (4) uThe structure of a cursor loop is thus: cursorLoop: LOOP … FETCH c INTO … ; IF NotFound THEN LEAVE cursorLoop; END IF; … END LOOP;

26 Example: Cursor uLet’s write a procedure that examines Sells(bar, beer, price), and raises by $1 the price of all beers at Joe’s Bar that are under $3. wYes, we could write this as a simple UPDATE, but the details are instructive anyway.

27 The Needed Declarations CREATE PROCEDURE JoeGouge( ) DECLARE theBeer CHAR(20); DECLARE thePrice REAL; DECLARE NotFound CONDITION FOR SQLSTATE ’02000’; DECLARE c CURSOR FOR (SELECT beer, price FROM Sells WHERE bar = ’Joe’’s Bar’); Used to hold beer-price pairs when fetching through cursor c Returns Joe’s menu

28 The Procedure Body BEGIN OPEN c; menuLoop: LOOP FETCH c INTO theBeer, thePrice; IF NotFound THEN LEAVE menuLoop END IF; 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; Check if the recent FETCH failed to get a tuple If Joe charges less than $3 for the beer, raise it’s price at Joe’s Bar by $1.