DATABASE DESIGN - 1DL400

Similar documents
Databases 1. SQL/PSM and Oracle PL/SQL

Real SQL Programming Persistent Stored Modules (PSM)

CSCD43: Database Systems Technology. Lecture 4

Real SQL Programming 1

Outline. CS 235: Introduction to Databases. DB Application Programming. Interface Solutions. Basic PSM Form. Persistent Stored Modules

Chapter 9 SQL in a server environment

ITCS Implementation. Jing Yang 2010 Fall. Class 14: Introduction to SQL Programming Techniques (Ch13) Outline

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 13 Introduction to SQL Programming Techniques

Database Applications. SQL/PSM Embedded SQL JDBC

Non-interactive SQL. EECS Introduction to Database Management Systems

DATABASE DESIGN I - 1DL300

DATABASE DESIGN II - 1DL400

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

Lecture 08. Spring 2018 Borough of Manhattan Community College

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

Introduction to Computer Science and Business

SQL Stored Programs. You Can Not Do Everything in SQL SQL/PSM Cursors Recursion Triggers. Niklas Fors Stored Programs 1 / 21

DATABASE DESIGN II - 1DL400

Programming the Database

Database Application Development

Slides by: Ms. Shree Jaswal

Introduction to Database Technology Elmasri/Navathe ch 1-2 Padron-McCarthy/Risch ch 1 Sobhan Badiozamany Silvia Stefanova Department of Information Te

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

5. Single-row function

Oracle Database 12c: Program with PL/SQL Duration: 5 Days Method: Instructor-Led

SQL STORED ROUTINES. CS121: Relational Databases Fall 2017 Lecture 9

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Likesèdrinker, beerè. Sellsèbar, beer, priceè. Frequentsèdrinker, barè

1Z0-144 Q&As Oracle Database 11g: Program with PL/ SQL

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL Ed 2

DATABASTEKNIK - 1DL116

Persistent Stored Modules (Stored Procedures) : PSM

Conditionally control code flow (loops, control structures). Create stored procedures and functions.

DATABASE DESIGN - 1DL400

Contents I Introduction 1 Introduction to PL/SQL iii

DATABASE TECHNOLOGY - 1DL124

Database Technology. Topic 6: Triggers and Stored Procedures

DATABASDESIGN FÖR INGENJÖRER - 1DL124

Actual4Test. Actual4test - actual test exam dumps-pass for IT exams

DATABASE TECHNOLOGY - 1DL124

Oracle Database 11g: Program with PL/SQL Release 2

CSE 530A. Inheritance and Partitioning. Washington University Fall 2013

DATABASE TECHNOLOGY - 1MB025

Stored Procedure. Stored procedures or functions. CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Create Stored Routines

DATABASE DESIGN - 1DL400

Assign expressions to declared variables with :=. END IF; EXIT WHEN éconditioné END LOOP;

When a database trigger routine does not have to take place before the triggering event, which timing should you assign to the trigger?

Oracle Database 12c R2: Program with PL/SQL Ed 2 Duration: 5 Days

Question Bank PL/SQL Fundamentals-I

The SQL database language Parts of the SQL language

CMPT 354 Database Systems I

IZ0-144Oracle 11g PL/SQL Certification (OCA) training

Oracle Database: Introduction to SQL/PLSQL Accelerated

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

DATABASE DESIGN I - 1DL300

Introduction to Data Management. Lecture #18 (SQL, the Final Chapter )

Oracle PLSQL Training Syllabus

Assorted Topics Stored Procedures and Triggers Pg 1

ORACLE: PL/SQL Programming

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

OVERVIEW OF THE TYPES OF PL/SQL BLOCKS:

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Schedule. Feb. 12 (T) Advising Day. No class. Reminder: Midterm is Feb. 14 (TH) Today: Feb. 7 (TH) Feb. 21 (TH) Feb. 19 (T)

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 8 Advanced SQL

Vendor: Oracle. Exam Code: 1Z Exam Name: Oracle Database 11g: Program with PL/ SQL. Version: Demo

Introduction to SQL/PLSQL Accelerated Ed 2

Oracle - Oracle Database: Program with PL/SQL Ed 2

DATABASDESIGN FÖR INGENJÖRER - 1DL124

Contains slides made by Naci Akkøk, Pål Halvorsen, Arthur M. Keller, Vera Goebel

Summary Modifications. Database Construction (and Usage) Explicit attribute lists. Insertions with queries. Quiz. Default values

Oracle Developer Track Course Contents. Mr. Sandeep M Shinde. Oracle Application Techno-Functional Consultant

Databases: Why? Databases: What? Databases: How? DATABASE DESIGN I - 1DL300

DATABASE DESIGN I - 1DL300

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Database Applications

CSE 565 Computer Security Fall 2018

Chapter 9 SQL in a server environment

ORACLE TRAINING. ORACLE Training Course syllabus ORACLE SQL ORACLE PLSQL. Oracle SQL Training Syllabus

First lecture of this chapter is in slides (PPT file)

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Database Management Systems Triggers

Options. Real SQL Programming 1. Stored Procedures. Embedded SQL

SQL Stored Routines Draft SQL Stored Routines. Procedural Extensions of SQL and External Routines in Transactional Context

RDBMS - PL SQL - Topic 5 - MSBTE QUESTIONS AND ANSWERS

Procedural Language Structured Query Language (PL/SQL)

UNIT-IV (Relational Database Language, PL/SQL)

Lecture 7 Stored procedure

Database System Concepts

EDUVITZ TECHNOLOGIES

Oracle. SQL(Structured Query Language) Introduction of DBMS. Build In Function. Introduction of RDBMS. Grouping the Result of a Query

SQL+PL/SQL. Introduction to SQL

DATABASTEKNIK - 1DL116

An Introduction to Stored Procedures in MySQL 5 by Federico Leven6 Apr 2011

What s New in MariaDB Server 10.3

Instructor: Craig Duckett. Lecture 14: Tuesday, May 15 th, 2018 Stored Procedures (SQL Server) and MySQL

SQL User Defined Code. Kathleen Durant CS 3200

Transcription:

DATABASE DESIGN - 1DL400 Fall 2015 A course on modern database systems http://www.it.uu.se/research/group/udbl/kurser/dbii_ht15 Kjell Orsborn Uppsala Database Laboratory Department of Information Technology, Uppsala University, Uppsala, Sweden 26/11/15 1

Procedural SQL and SQL/PSM (Elmasri/Navathe ch. 5.2, 8.2 and 9.1, 9.6) (Padron-McCarthy/Risch ch 12 and 14) Kjell Orsborn Department of Information Technology Uppsala University, Uppsala, Sweden 26/11/15 2

PSM - stored routines, i.e. functions and procedures SQL persistent stored modules (SQL/PSM part of SQL standard), allows us to define functions and procedures stored in the database. That means that stored functions or procedures (user-defined routines) can be defined that are executed in the database server. Stored procedures may have side effects that update the database. They are not allowed in queries. Stored procedures have conventional programming language constructs like variable assignment, if-then-else, while, error handling, etc. making SQL a complete server programming language. By contrast stored functions cannot have side-effects that update the database state and can be used in SQL queries, e.g. to do common computations. Stored procedures usually update the database and contain application logic. Typically used for data centered code used by many applications to update database consistently 26/11/15 3

Why stored procedures and functions? Advantages: Save communication time Put data centered code close to (i.e. in) the database Make consistent database updates E.g. standard procedures to hire person, change salary, etc. Can make 2-tier solution rather than application server! Logic in database user interactions in client program Problems with stored procedures: Not well standardized: Mimer, MySQL and IBM (SQL PL) best to follow PSM standard MS SQL Server PSM (Transact-SQL) significantly different PL/SQL (Oracle language for stored procedures) PL/pgSQL (PostgreSQL language similar to SQL/PSM and PL/SQL) Some powerful features missing in primitive DBMSs E.g. table functions (returning tables as values and allowed in queries) not allowed in MySQL 26/11/15 4

Application servers Middle-tier Server Application Application Client Browser Application Server (e.g. Tomcat) JDBC Driver Database Server 3-4 tiers depending on JDBC tiers 26/11/15 5

Stored functions A stored function is invoked by its name and the parameters of a function are passed by value, e.g.: SIN(V.ANGLE), where V is a tuple variable and ANGLE is an atttribute A stored function always returns a single value. Functions can be used in queries: SELECT SQRT(P.AGE) FROM PERSON P SQL expressions for functions: CREATE FUNCTION, creates a function that exists on its own, DROP FUNCTION, drops a function that exists on its own GRANT EXECUTE, grants the privilege to invoke a function REVOKE EXECUTE, revokes the privilege to invoke a function 26/11/15 6

Stored procedures A stored procedure makes it possible to store procedural code for applications in the database. A procedure is normally invoked explicitly by executing the CALL statement and does not return a value. The parameters of a procedure can be used to provide input and may be used to pass values back to the calling environment. There is a special type of procedure, called a result set procedure, which returns rows of a result set to a cursor when it is invoked by executing the FETCH statement in that context. Stored procedures can perform certain error handling using exceptions SQL expressions for procedures: CREATE PROCEDURE, creates a procedure DROP PROCEDURE, drops a procedure GRANT EXECUTE, grants the privilege to invoke a procedure REVOKE EXECUTE, revokes the privilege to invoke a procedure CALL, invokes a procedure 26/11/15 7

PSM stored functions and procedures DETERMINISTIC or NOT DETERMINISTIC characteristic: If DETERMINISTIC is specified, the function is guaranteed to produce the same result every time it is invoked with the same set of input values and repeated invocations of it can, therefore, be optimized. Utilized by DBMS engine to optimize execution. If neither DETERMINISTIC nor NOT DETERMINISTIC is specified, then NOT DETERMINISTIC is implicit. Access options may be specified: CONTAINS SQL The function may not contain any data-manipulation-statements. All other procedural-sql-statements are permitted. The function may only invoke CONTAINS SQL functions and procedures. This option effectively prevents a routine from performing read or write operations on data in the database. READS SQL DATA All procedural-sql-statements are permitted except those performing updates (i.e. DELETE, INSERT and UPDATE). The function may only invoke CONTAINS SQL or READ SQL DATA functions and procedures. This option effectively prevents a routine from performing write operations on data in the database. MODIFIES SQL DATA All procedural-sql-statements are permitted and any function or procedure may be invoked from this type of function. This option allows a routine to perform read and write operations on data in the database. If neither CONTAINS SQL, READS SQL DATA nor MODIFIES SQL DATA is specified, then CONTAINS SQL is implicit. 26/11/15 8

Basic PSM form Stored procedure: CREATE PROCEDURE <name> ( <parameter list> ) <optional local declarations> <body>; Stored function: CREATE FUNCTION <name> ( <parameter list> ) RETURNS <type> <optional local declarations> <body>; Parameters in PSM: Unlike the usual name-type pairs in languages like C, PSM uses mode-name-type triples, where the mode can be: IN = procedure uses value, does not change value. OUT = procedure changes, does not use. INOUT = both. 26/11/15 9

Stored procedure example A simple procedure that takes three arguments asupplier, aproduct and aprice, and adds a tuple to: Supplies(supplier, product, price) The procedure: CREATE PROCEDURE ProductInsert( IN asupplier CHAR(20), IN aproduct CHAR(20), IN aprice REAL ) INSERT INTO Supplies VALUES(asupplier, aproduct, aprice); 26/11/15 10

Invoking procedures and functions Procedures are invoked by using the SQL/PSM statement CALL, with the name of the desired procedure and arguments. Example: CALL ProductInsert( NutInc, Bolt, 0.50); Stored procedures can be called either from Directly from the application, e.g. as a JDBC call From other stored procedures From triggers (later) NOTICE that stored procedures cannot be called from queries Guarantees that queries are non-procedural The query optimizer assumes non-procedural queries (no side effects in functions) Functions used in SQL expressions wherever a value of their return type is appropriate. 26/11/15 11

Basic PSM statements DECLARE <name> <type> used to declare local variables. BEGIN... END for groups of statements. Separate statements by semicolons. Assignment statements: SET <variable> = <expression>; For example: SET p = Bolt ; IF statements in its simplest form: IF <condition> THEN <statements(s)> END IF; Unlike stored procedures, stored functions contains a RETURN statements: RETURN <expression> returns the value of the function. 26/11/15 12

IF example Let s rate products by how many buyers there are of each product, represented as: Buyers(customer, product) <500 customers: unpopular. 500-1500 customers: average. >= 1500 customers: popular. Function ProductRater(p) rates product p. CREATE FUNCTION ProductRater(IN p CHAR(20) ) RETURNS CHAR(10) DECLARE customer INTEGER; BEGIN SET customer = (SELECT COUNT(*) FROM Buyers WHERE product = p); IF customer < 500 THEN RETURN unpopular ELSEIF customer < 1500 THEN RETURN average ELSE RETURN popular END IF; END; Number of customers of product p Nested IF statement Return occurs here, not at one of the RETURN statements 26/11/15 13

Loops Basic form: LOOP <statements> END LOOP; Exit from a loop by: LEAVE <loop name> The <loop name> is associated with a loop by prepending the name and a colon to the keyword LOOP. Example of exiting a loop: loop1: LOOP... LEAVE loop1;... END LOOP; 26/11/15 14

Loops cont Other types of loop forms While-Do: WHILE <condition> DO <statements> END WHILE; Repeat-until: REPEAT <statements> UNTIL <condition> END REPEAT; 26/11/15 15

Queries General SELECT-FROM-WHERE queries are not permitted in PSM. There are three ways to get the effect of a query: Queries producing one value can be the expression in an assignment. Single-row SELECT... INTO. Cursors. Assignment query example: If p is a local variable and Supplies(supplier, product, price) our old relation, we can get the price NutInc charges for Bolt by: SET p = (SELECT price FROM Supplies WHERE supplier = NutInc AND product = Bolt ); SELECT... INTO example: An equivalent way to get the value of a query that is guaranteed to return one tuple is by placing INTO <variable> after the SELECT clause. SELECT price INTO p FROM Supplies WHERE supplier = NutInc Bar AND product = Bolt ; 26/11/15 16

Cursors A cursor is a variable that represents the current position in the stream over the tuples returned by a query. The stream, itself is called a scan. Declare a cursor variable c by: DECLARE c CURSOR FOR <query>; Opening Cursors To use cursor c, we must issue the command: OPEN c; The query of c is evaluated, and c is set to point to the first tuple of the scan. Fetching tuples from a the current cursor position of a scan: To get the next tuple from cursor c, issue command: FETCH FROM c INTO x1, x2,...xn; The variables X1, X2 are bound to the corresponding positions in the current result tuple of the scan. The cursor c is moved automatically to the next tuple in the scan. Closing Cursors When finished with c, issue command: CLOSE c; 26/11/15 17

Iteration over scans The usual way to use a cursor over a scan is to create a loop with a FETCH statement, and do something with the variable bound by the FETCH. This allows iteration over arbitrary large data sets without using memory. Each PSM statement returns a status code, which is a 5-digit number. For example, FETCH has the status codes: 00000 = Next tuple read or 02000 = No more tuples in scan In PSM, we get the value of the status code in a variable called SQLSTATE. SQLSTATE is used for testing whether there is more data in the scan. 26/11/15 18

Exceptions We may also 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 02000 by: DECLARE NotFound CONDITION FOR SQLSTATE 02000 ; The structure of a cursor loop is thus: LOOP... FETCH c INTO...; IF NotFound THEN LEAVE; END IF;... END LOOP; 26/11/15 19

Cursor example Write a procedure that examines Supplies(supplier, product, price), and raises by $1 the price of all products from NutInc that are under $3. The procedure head: CREATE PROCEDURE StagePrices( ) DECLARE theproduct CHAR(20); DECLARE theprice REAL; DECLARE NotFound CONDITION FOR SQLSTATE 02000 ; DECLARE c CURSOR FOR (SELECT product, price FROM Supplies WHERE supplier = NutInc ); Used to hold product-price pairs when fetching through cursor c Returns product-price tuples for NutInc 26/11/15 20

The procedure body BEGIN OPEN c; END; supploop: LOOP FETCH c INTO theproduct, theprice; IF NotFound THEN LEAVE supploop END IF; IF theprice < 3.00 THEN END IF; END LOOP; CLOSE c; Check if the recent FETCH failed to get a tuple UPDATE Supplies SET price = theprice + 1.00 WHERE supplier = NutInc AND product = theproduct; If the price is less than $3 for the product, raise it s price at NutInc by $1. 26/11/15 21