26
MySQL DBA Training Session 14. Stored Procedures and Functions in MySQL RAM N SANGWAN WWW.RNSANGWAN.COM YOUTUBE CHANNEL : HTTP://YOUTUBE.COM/USER/THESKILLPEDIA TO LEARN OR TEACH JOIN WWW.THESKILLPEDIA.COM

Mysql dba training session 14 stored procedures and functions in mysql

Embed Size (px)

Citation preview

Page 1: Mysql dba training session 14 stored procedures and functions in mysql

MySQL DBA Training Session 14.Stored Procedures and Functions in MySQLRAM N SANGWAN

WWW.RNSANGWAN.COM

YOUTUBE CHANNEL : HTTP://YOUTUBE.COM/USER/THESKILLPEDIA

TO LEARN OR TEACH JOIN WWW.THESKILLPEDIA.COM

Page 2: Mysql dba training session 14 stored procedures and functions in mysql

Coming up in this Session

• Definition –MySQL Stored Routines

• Features of MySQL Stored Procedures

• Disadvantages

• Getting Started with MySQL Stored Routines

• Work Flow

• Checking Stored Procedure

• Stored Functions

• Examples

• Stored Routine Parameter Types

Page 3: Mysql dba training session 14 stored procedures and functions in mysql

Coming up in this Session

• Stored Procedure- Complete Syntax

• Stored Procedure/Function Syntax

• Control Flow Statements

• Control-Flow Example Code

• Case Statement

• Repeat Loop

• While Loop

• Declaring a handler

• Stored Routine Security

Page 4: Mysql dba training session 14 stored procedures and functions in mysql

Definition

• A stored procedure is a segment of declarative SQL statements stored inside the databasecatalog.

• MySQL does not support recursive stored procedure very well.

• Stored Procedures were added to MySQL version 5.0.

Page 5: Mysql dba training session 14 stored procedures and functions in mysql

Features

• MySQL stored procedures are compiled on demand.

• After compiling a stored procedure, MySQL puts it to a cache.

• If an application uses a stored procedure multiple times in a single connection, thecompiled version is used, otherwise the stored procedure works like a query.

• Stored procedures helps reduce the traffic between application and database server.

• Stored procedures are reusable and transparent to any applications.

• Stored procedures are secure.

Page 6: Mysql dba training session 14 stored procedures and functions in mysql

Disadvantages

• If you use a lot of stored procedures, the memory usage of every connection that is usingthose stored procedures will increase substantially.

• A constructs of stored procedures make it more difficult to develop stored procedures thathave complicated business logic.

• MySQL does not provide facilities for debugging stored procedures.

• It is not easy to develop and maintain stored procedures.

Page 7: Mysql dba training session 14 stored procedures and functions in mysql

Getting Started..

Suppose we want to keep track of the total salaries of employeesworking for each department

We need to write a procedure to

update the salaries in the deptsal

table

Page 8: Mysql dba training session 14 stored procedures and functions in mysql

Step -1

Change the delimiter (i.e., terminating character) of SQL statement from

semicolon (;) to something else (e.g., //) So that you can distinguish between

the semicolon of the SQL statements in the procedure and the terminating

character of the procedure definition

Page 9: Mysql dba training session 14 stored procedures and functions in mysql

Step 2

1. Define a procedure called updateSalary which takes as input a department

number.

2. The body of the procedure is an SQL command to update the totalsalary

column of the deptsal table.

3. Terminate the procedure definition using the delimiter you had defined in step

1 (//)

Page 10: Mysql dba training session 14 stored procedures and functions in mysql

Step 3

Change the delimiter back to semicolon (;)

Page 11: Mysql dba training session 14 stored procedures and functions in mysql

Done!!

Call the procedure to update the totalsalary for each

department

Page 12: Mysql dba training session 14 stored procedures and functions in mysql

Check it!

Show the updated total salary in the deptsal table

Page 13: Mysql dba training session 14 stored procedures and functions in mysql

Looking Back!

• Use show procedure status to display the list of stored procedures you have created

Use drop procedure to remove a stored procedure

Page 14: Mysql dba training session 14 stored procedures and functions in mysql

Stored Functions

function <function-name> (param_spec1, …, param_speck)

returns <return_type>

[not] deterministic

Begin

-- execution code

end;

where param_spec is:

[in | out | in out] <param_name> <param_type>

◦ You need ADMIN privilege to create functions on mysql-user server

Page 15: Mysql dba training session 14 stored procedures and functions in mysql

Example of Functions

Page 16: Mysql dba training session 14 stored procedures and functions in mysql

Example of Functions

Page 17: Mysql dba training session 14 stored procedures and functions in mysql

Stored Routine Parameter Types

• IN. This is the default. It is passed to the routine and can be changed insidethe routine, but remains unchanged outside.

• OUT. No value is supplied to the routine (it is assumed to be NULL), but itcan be modified inside the routine, and it is available outside the routine.

• INOUT. The characteristics of both IN and OUT parameters. A value can bepassed to the routine, modified there as well as passed back again.

Page 18: Mysql dba training session 14 stored procedures and functions in mysql

Control Flow Statements

IF search_condition THEN

statement_list

[ELSEIF search_condition THEN

statement_list] ….

[ELSE

statement_list]

END IF

Page 19: Mysql dba training session 14 stored procedures and functions in mysql

Control-Flow Example Code

DELIMITER //

CREATE FUNCTION SimpleCompare(i INT, j INT) RETURNS VARCHAR(20)

BEGIN

DECLARE s VARCHAR(20);

IF j > i THEN

set s = '>';

ELSEIF i = j THEN

set s = '=';

ELSE

set s = '<'

END IF;

set s = CONCAT(j, ' ', s, ' ', i);

RETURN s;

END //

DELIMITER ;

Page 20: Mysql dba training session 14 stored procedures and functions in mysql

Case Statement

CASE VarName

WHEN VarValue1 THEN Statement/s

WHEN VarValue2 THEN Statement/s

......

ELSE Statement/s

END CASE

Page 21: Mysql dba training session 14 stored procedures and functions in mysql

Repeat Loop

DELIMITER //

CREATE PROCEDURE SimpleRepeat(p1 INT)

BEGIN

SET @x = 0;

REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;

END

//

DELIMITER ;

Page 22: Mysql dba training session 14 stored procedures and functions in mysql

While Loop

CREATE PROCEDURE SimpleWhile()

BEGIN

DECLARE v1 INT DEFAULT 5;

WHILE v1 > 0 DO

...

SET v1 = v1 - 1;

END WHILE;

END

Page 23: Mysql dba training session 14 stored procedures and functions in mysql

Declaring a handler

To declare a handler, use the DECLARE HANDLER statement as follows:

DECLARE action HANDLER FOR condition_value statement;

If a condition whose value matches the condition_value, MySQL will execute the

statement and continue or exit the current code block based on the action.

The action accepts one of the following values:

• CONTINUE: the execution of the enclosing code block ( BEGIN … END)

continues.

• EXIT: the execution of the enclosing code block, where the handler is declared,

terminates.

• The condition_value specifies a particular condition or a class of conditions that

activates the handler.

Page 24: Mysql dba training session 14 stored procedures and functions in mysql

Declaring a handler Contd..

The condition_value accepts one of the following values:

• A MySQL error code.• A standard SQLSTATE value. Or it can be an SQLWARNING, NOTFOUND or

SQLEXCEPTION condition, which is shorthand for the class of SQLSTATE values.

The NOTFOUND condition is used for a cursor or SELECT INTO variable_list statement.

• A named condition associated with either a MySQL error code or SQLSTATE value.

• The statement could be a simple statement or a compound statement enclosing by

the BEGIN and END keywords.

Page 25: Mysql dba training session 14 stored procedures and functions in mysql

Stored Routine Security

• Access to an SR must be explicitly granted to users that did not create it and plan to use it.

• It is possible to give greater privileges to tables than those users that execute the SR.

• It is possible to create an environment more resistant to SQL injections.

Page 26: Mysql dba training session 14 stored procedures and functions in mysql

Thank You