Documente Academic
Documente Profesional
Documente Cultură
Database Fundamentals
Stored Procedures
MySQL
Overview
z What Are Stored Procedures?
z Benefits
z Stored Procedures in MySQL
z Parameters
z Stored Functions
z SQL Commands
1
What Are Stored Procedures?
z Stored procedure: Set of SQL
statements that are:
1. Stored on the server to accomplish a task
2. Pre-compiled
z Almost any valid SQL can go inside a
stored procedure, with a few exceptions.
z Invoked by making a call to the
procedure with the correct arguments.
Benefits
z Performance: Stored procedures are
faster than application code because
they are pre-compiled SQL code.
Usually…
z Separation of concerns: Stored
procedures allow separate database
access logic to be from application logic.
z Maintainability: Having SQL on server
provides central location for making
changes.
4
2
Benefits
(cont.)
z Security: Can grant users access to
procedures rather than directly to tables.
z Portability: Will run in all environments.
No need to recreate the logic
z Reduction in network traffic.
Complex repetitive tasks frequently make
computations and then apply more
computations to result
Having all results on DB is more efficient
3
Stored Procedures in MySQL
products Example
z Lets create a procedure to list all
product names in a category:
GadgetCadge DB
create procedure products()
select product_name
from product
where category_id like "%Media";
z To call the procedure:
call products();
7
4
Parameters
z Real benefit of procedures is sending
data into or out of of procedure.
z Three types of parameter:
IN: (default) Parameter passed to the
procedure, can change inside the
procedure, but remains unchanged outside
OUT: No value is supplied to the procedure
(assumed to be NULL), but returned value
can be modified inside the procedure, and
is available outside the procedure
INOUT: The characteristics of both IN and
OUT parameters. A value can be passed to
the procedure, modified there as well as
passed out again 9
Parameters
(cont.)
z What if we want to use a value in
our procedure?
z Lets look at retrieving product
names for products with price
greater than $100:
create procedure
price_greater_than(IN p float)
select product_name
from product
where unit_cost > p; 10
5
Parameters
(cont.)
call price_greater_than(100);
11
Parameters
(cont.)
z What if we wanted to calculate and
return the number of “Media” items:
create procedure
count_category (IN cat varchar(20))
select count(*)
from product
where category_id like cat;
12
6
Parameters
(cont.)
call count_category("Media");
13
Parameters
Update Example
z Lets look at how we put items in a
particular category on sale.
z Since this will require multiple lines of
code, we need to define new delimiter to
be used inside the procedure.
z The original product relation:
14
7
delimiter |
create procedure
put_on_sale(cid varchar(15),
sale decimal(6,2))
begin
update product
set unit_cost = (unit_cost –
(unit_cost * sale))
where category_id like cid;
select * from product;
end;
|
delimiter;
15
Parameters
Update Example (cont.)
z The function call:
call put_on_sale("Household",0.10);
16
8
Stored Functions
z A stored function is like a stored
procedure except that it always returns a
result.
z Can be called inside an SQL statement
just like ordinary SQL functions.
z A function parameter is the equivalent of
the IN procedure parameter.
Functions use return keyword to
determine what is passed back
17
Stored Functions
(cont.)
z Stored functions also have slightly
more limitations in what SQL
statements they can run than stored
procedures.
z Cannot use SQL statements that
return a result set.
No select queries returning result
sets from a table
You can get around this by using
select into
18
9
Stored Functions
order_total Example
z Lets look at an example where we
total the cost of the items in an
order.
19
Stored Functions
order_total Example (cont.)
delimiter |
create function total_order(
o_id varchar(15))
returns decimal(7,2)
begin
declare total decimal(7,2) default 0;
select sum(quantity*unit_cost)
into total
from order_item
where order_id = o_id;
return total;
end;
|
delimiter ;
20
10
Functions
order_total Example
z The function is called:
select total_order("i111");
21
SQL Commands
22
11
SQL Commands
(cont.)
23
SQL Commands
(cont.)
24
12
SQL Commands
(cont.)
25
SQL Commands
(cont.)
26
13
Questions?
27
14