Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Advanced Topics.

Similar presentations


Presentation on theme: "Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Advanced Topics."— Presentation transcript:

1 Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Advanced Topics

2 Topics for Today Stored Functions (SQL-Invoked Routines) Stored Procedures (SQL-Invoked Routines) Final Review This Thursday: MySQL Connectors Database-Enabled Applications More Final Review

3 Stored Functions Simple way to write your own functions Functions must return a single value Stored functions are defined in database schema Not global, must be associated with a database Can use SQL Drawbacks Can’t define your own aggregates For custom aggregates, use C/C++ to make UDFs (user-defined functions).

4 Stored Functions Single Statement Syntax CREATE FUNCTION name(varname type, varname type, varname type,...) RETURNS data_type RETURN single_query | single_command; Compound Statement Syntax CREATE FUNCTION name(varname type, varname type, varname type,...) RETURNS data_type BEGIN compound_statements END;

5 Stored Functions Drop Function Syntax DROP FUNCTION [IF EXISTS] name;

6 Stored Functions Using compound table syntax, you can declare variables initialize variables use conditional statements use loop statements use subqueries Statements must be terminated by a semicolon Statements must occur between BEGIN and END

7 Stored Functions To declare a variable: DECLARE name data_type [DEFAULT expression]; To initialize a variable: SET name = expression | subquery; To return a value: RETURN expression | subquery;

8 Stored Functions To define an IF statement: IF expression THEN statement_list ELSEIF expression THEN statement_list ELSE statement_list ENDIF;

9 Stored Functions To define a LOOP statement: [begin_label] LOOP statement_list END LOOP [end_label] LOOP is similar to for(;;) {} in C++ and Java ITERATE is similar to continue in C++ and Java LEAVE is similar to break in C++ and Java

10 Stored Functions Example #1: Write a function to solve the quadratic equation. Functions parameters should be a, b, and c for the constants and s = {1, 2} for the first or second solution. CREATE FUNCTION QUADRATIC_V1(a REAL, b REAL, c REAL, r INT) RETURNS REAL RETURN IF(r = 1, (-b + SQRT(b*b - 4*a*c))/(2*a), (-b - SQRT(b*b - 4*a*c))/(2*a));

11 Stored Functions Example #2: Rewrite the previous example using the compound-statement syntax. CREATE FUNCTION QUADRATIC_V2(a REAL, b REAL, c REAL, r INT) RETURNS REAL BEGIN DECLARE denom, discr REAL; SET discr = SQRT(b*b - 4*a*c); SET denom = 2*a; RETURN IF(r = 1, (-b + discr)/(denom), (-b - discr)/(denom)); END

12 Stored Functions Example #3: Write a stored function that computes the number of occurrences of a certain character within a string. CREATE FUNCTION OCCURRENCE(s VARCHAR(256), c CHAR(1)) RETURNS INT BEGIN DECLARE retval INT DEFAULT 0; -- A BUNCH OF STUFF GOES HERE RETURN retval; END

13 Stored Functions Solution: CREATE FUNCTION OCCURRENCE(s VARCHAR(256), c CHAR(1)) RETURNS INT BEGIN DECLARE retval INT DEFAULT 0; DECLARE i INT DEFAULT 1; loop_label: LOOP IF i > CHAR_LENGTH(s) THEN LEAVE loop_label; END IF; IF SUBSTRING(s, i, 1) = c THEN SET retval = retval + 1; END IF; SET i = i + 1; END LOOP; RETURN retval; END

14 Stored Procedures A stored procedure is a stored function that does not return a value Stored procedures are defined in database schema Not global, must be associated with a database Can use SQL Provides an extra layer of security If only stored procedures are allowed (no selection queries or commands), what you can do to the database is limited by the procedures the database supports

15 Stored Procedures Syntax is the same as stored functions, with two exceptions: No RETURNS and RETURN statements You cannot use a stored procedure in a selection query, you must use the CALL keyword CALL Syntax CALL stored_procedure_name(param1, param2,...);

16 Stored Procedures Example: Write a stored procedure for the toy store database that inserts a new genre into the database. CREATE PROCEDURE ADD_GENRE(g VARCHAR(32)) BEGIN INSERT INTO genre VALUES(0, g); END


Download ppt "Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Advanced Topics."

Similar presentations


Ads by Google