Академический Документы
Профессиональный Документы
Культура Документы
Options
1. Code in a specialized language is
stored in the database itself (e.g.,
PSM, PL/SQL).
2. SQL statements are embedded in a
host language (e.g., C).
3. Connection tools are used to allow a
conventional language to access a
database (e.g., CLI, JDBC, PHP/DB).
3
Stored Procedures
PSM, or persistent stored modules,
allows us to store procedures as
database schema elements.
PSM = a mixture of conventional
statements (if, while, etc.) and SQL.
Lets us do things we cannot do in SQL
alone.
4
Parameters in PSM
Unlike the usual name-type pairs in
languages like C, PSM uses modename-type triples, where the mode can
be:
IN = procedure uses value, does not
change value.
OUT = procedure changes, does not use.
INOUT = both.
6
The Procedure
CREATE PROCEDURE JoeMenu (
IN b
CHAR(20),
Parameters are both
read-only, not changed
IN p
REAL
)
INSERT INTO Sells
The body --VALUES(Joes Bar, b, p); a single insertion
8
Invoking Procedures
Use SQL/PSM statement CALL, with the name
of the desired procedure and arguments.
Example:
CALL JoeMenu(Moosedrool, 5.00);
Functions used in SQL expressions wherever
a value of their return type is appropriate.
11
IF Statements
Simplest form:
IF <condition> THEN
<statements(s)>
END IF;
Add ELSE <statement(s)> if desired, as
IF . . . THEN . . . ELSE . . . END IF;
Add additional cases by ELSEIF
<statements(s)>: IF THEN ELSEIF
THEN ELSEIF THEN ELSE END IF;
12
Example: IF
Lets rate bars by how many customers
they have, based on Frequents(drinker,bar).
<100 customers: unpopular.
100-199 customers: average.
>= 200 customers: popular.
13
Example: IF (continued)
CREATE FUNCTION Rate (IN b CHAR(20) )
Number of
RETURNS CHAR(10)
customers of
bar b
DECLARE cust INTEGER;
BEGIN
SET cust = (SELECT COUNT(*) FROM Frequents
WHERE bar = b);
IF cust < 100 THEN RETURN unpopular
ELSEIF cust < 200 THEN RETURN average
ELSE RETURN popular
Nested
END IF;
IF statement
Return occurs here, not at
14
END;
one of the RETURN statements
Loops
Basic form:
<loop name>: LOOP <statements>
END LOOP;
Exit from a loop by:
LEAVE <loop name>
15
16
17
Queries
General SELECT-FROM-WHERE
queries are not permitted in PSM.
There are three ways to get the effect
of a query:
1. Queries producing one value can be the
expression in an assignment.
2. Single-row SELECT . . . INTO.
3. Cursors.
18
Example: Assignment/Query
Using local variable p and Sells(bar, beer,
price), we can get the price Joe charges for
Bud by:
SET p = (SELECT price FROM Sells
WHERE bar = Joes Bar AND
beer = Bud);
19
SELECT . . . INTO
Another way to get the value of a query
that returns one tuple is by placing INTO
<variable> after the SELECT clause.
Example:
SELECT price INTO p FROM Sells
WHERE bar = Joes Bar AND
beer = Bud;
20
Cursors
A cursor is essentially a tuple-variable
that ranges over all tuples in the result
of some query.
Declare a cursor c by:
DECLARE c CURSOR FOR <query>;
21
25
FETCH c INTO ;
IF NotFound THEN LEAVE
cursorLoop;
END IF;
END LOOP;
27
Example: Cursor
Lets write a procedure that examines
Sells(bar, beer, price), and raises by $1
the price of all beers at Joes Bar that
are under $3.
Yes, we could write this as a simple
UPDATE, but the details are instructive
anyway.
28
MySQL Cursors
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a CHAR(16);
DECLARE b, c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1; OPEN cur2;
read_loop: LOOP
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF done THEN LEAVE read_loop; END IF;
IF b < c THEN INSERT INTO test.t3 VALUES (a,b);
ELSE INSERT INTO test.t3 VALUES (a,c); END IF;
END LOOP;
CLOSE cur1;
CLOSE cur2;
31
END;