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