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.

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.
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.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
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.
Winter 2002Arthur Keller – CS 18012–1 Schedule Today: Feb. 19 (T) u Object-Relational Systems. u Read Sections 4.5, Assignment 5 due. Feb. 21.
Fall 2001Arthur Keller – CS 18010–1 Schedule Oct. 25 (TH) More Constraints, Triggers. u Read Sections Oct. 30 (T) Embedded SQL. u Read Section.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #19 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
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,
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 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.
Oracle SQL*plus John Ortiz. Lecture 10SQL: Overview2 Overview  SQL: Structured Query Language, pronounced S. Q. L. or sequel.  A standard language for.
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.
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.
Announcements Read JDBC Project Step 5, due Monday.
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.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
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.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
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 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
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.
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 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.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
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
SQL Environment.
Introduction to Database Systems, CS420
CMSC-461 Database Management Systems
Chapter 5: Advanced SQL Database System concepts,6th Ed.
UNIT - V STORED PROCEDURE.
Database Application Development
CPSC-310 Database Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
Database Application Development
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
CMSC-461 Database Management Systems
Chapter 8 Advanced SQL Pearson Education © 2009.
CPSC-608 Database Systems
Modification to Views Via Triggers
Database Application Development
Presentation transcript:

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

2 SQL in Real Programs uWe have seen only how SQL is used at the generic query interface --- an environment where we sit at a terminal and ask queries of a database. uReality is almost always different: conventional programs interacting with SQL.

3 Options 1.Code in a specialized language is stored in the database itself (e.g., PSM, PL/SQL). 2.SQL statements are embedded in a host language (e.g., C). 3.Connection tools are used to allow a conventional language to access a database (e.g., CLI, JDBC, PHP/DB).

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

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

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

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

11 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 ;

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

13 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

14 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 its price at Joe’s Bar by $1.

15 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 (sqlplus), like any SQL statement.

16 SQLPlus uPL/SQL can be considered as a superset of SQL. uException: SQL data definition statements are not allowed in PL/SQL.

17 PL/SQL Anonymous Block DECLARE BEGIN END;. run uThe DECLARE section is optional.

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

19 Form of PL/SQL Function CREATE OR REPLACE FUNCTION [( )] RETURN AS BEGIN END;. run

20 Form of PL/SQL Packages CREATE OR REPLACE Package AS [BEGIN END;] END;. run

21 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;

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

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

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

25 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 will be suitable for the intended uses of b and p.

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

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

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

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

30 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’;

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

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

33 Example: Tuple Type uRepeat of JoeGouge() declarations with variable bp of type beer-price pairs. CREATE OR REPLACE PROCEDURE JoeGouge() AS CURSOR c IS SELECT beer, price FROM Sells WHERE bar = ’Joe’’s Bar’; bp c%ROWTYPE;

34 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

Built-In Packages uDbms_output package wAllows to display information to the session output device wDisable/enable (buffer size) wNew_line wPut/put_line/get_line 35

Project Deliverables – Phase 4 uPL/SQL code uOutput of each procedure (screenshots, file, etc.) 36

Phase 4 uFind the major of a given student list the courses, the term, the instructor, and the grade of the courses he/she has taken. uCalculates the GPA for each student and list the name, major, and contact information of those students whose GPA is larger than

Phase 4 uFind the top 5 largest enrolled courses (not sections) for each term. Print the course title, number of enrollment, the each instructor’s name and the number of credit hours taught by the instructor. uAllow a student to register for a new class if he has not taken it yet or has received lower than C in it. Check that there is no scheduling problem. 38

Project Deliverables – Phase 5 uCombine everything in a readable document: wName, title, course, semester wProject description wER Model wRelations Create table statements 39

Project Deliverables – Phase 5 uTriggers and Constraints wDescription what the triggers should do wCreate trigger code uData content wSelect * from statement for each table uPL/SQL code and output 40

Database Management Systems uNeed for DBMS uRelational data model uDB design wER Model wConstraints wNormalization wIndexing 41

DBMS -2 uDatabase Queries wDDL – Metadata uQuery processing wRelational Algebra wSQL uTransactions uSecurity 42

DBMS – 3. uUsing Oracle: wGeneral interface Table and constraint declarations/modifications Insert/delete/modify data Query database wPSM, PL/SQL Procedural extension of SQL 43

CSCE 520 Objectives uDescribe the major components of a database management system and state their functions and purpose. u2. Develop a data model for a database application using an appropriate modeling tool such as ER diagrams. u3. Use the concepts of data normalization to develop well-designed database applications. u4. Implement a database application using an appropriate relational DBMS. u5. Use SQL to access database information. u6. Describe major operational issues associated with database applications, including transaction management, security, and integrity. 44