Download presentation
Presentation is loading. Please wait.
1
Database Application Development
Chapter 6 PSM (Stored Procedures) The slides for this text are organized into chapters. This lecture covers Chapter 6. I Foundations •Chapter 1: Introduction •Chapter 2: ER Model and Conceptual Design •Chapter 3: The Relational Model and SQL DDL •Chapter 4: Relational Algebra and Relational Calculus •Chapter 5: SQL II Applications •Chapter 6: Database Application Development •Chapter 7: Database-backed Internet Applications III Systems •Chapter 8: Overview of Storage and Indexing •Chapter 9: Data Storage •Chapter 10: Tree Indexes •Chapter 11: Hash Indexes IV Systems •Chapter 12: Overview of Query Evaluation •Chapter 13: External Sorting •Chapter 14: Evaluation of Relational Operators: First part (joins) and second part (other operators) •Chapter 15: A Typical Relational Optimizer V Systems •Chapter 16: Overview of Transaction Management •Chapter 17: Concurrency Control •Chapter 18: Recovery VI Applications •Chapter 19: Schema Refinement, Functional Dependencies, Normalization •Chapter 20: Physical Database Design, Database Tuning •Chapter 21: Security and Authorization VII Advanced Topics •Chapter 22: Parallel and Distributed Database Systems •Chapter 23: Data Warehousing and Decision Support •Chapter 24: Object-Database Systems •Chapter 25: Deductive Databases •Chapter 26: Data Mining •Chapter 27: Information Retrieval and XML Data Management •Chapter 28: Spatial Databases
2
Stored Procedures What is a stored procedure: Advantages:
SQL allows you to define procedures and functions and store in the DB server Program executed through single SQL statement Executed in process space by the database server Advantages: Can encapsulate application logic while staying “close” to the data (usually implies efficiency) Reuse of application logic by different users Avoid tuple-at-a-time return of records through cursors (as would be done by JDBC)
3
SQL/PSM Declare a stored procedure:
CREATE PROCEDURE name(p1, p2, …, pn) local variable declarations procedure code for body; Declare a function: CREATE FUNCTION name (p1, …, pn) RETURNS sqlDataType local variable declarations function code;
4
Stored Procedures: Examples
CREATE PROCEDURE ShowNumReservations SELECT S.sid, S.sname, COUNT(*) FROM Sailors S, Reserves R WHERE S.sid = R.sid GROUP BY S.sid, S.sname;
5
Stored Procedures: Examples
Stored procedures can have parameters: <name> <mode> <type> <mode> is one of {IN, OUT, INOUT} eg: IN val1 int CREATE PROCEDURE IncreaseRating( IN sailor_sid INTEGER, IN increase INTEGER) UPDATE Sailors SET rating = rating + increase WHERE sid = sailor_sid;
6
Stored Procedures: Examples
Stored procedure do not have to be in SQL: CREATE PROCEDURE TopSailors( IN num INTEGER) LANGUAGE JAVA EXTERNAL NAME “file:///c:/storedProcs/rank.jar”
7
Example: Procedure in PSM
CREATE PROCEDURE testProcedure BEGIN INSERT INTO Student VALUES (5, ‘Joe’); END; Oracle’s version of PSM : CREATE PROCEDURE testProcedure IS . run; NOTE: mySQL later versions have initial support.
8
Also You can drop procedure by DROP PROCEDURE <procedureName>
In PL/SQL, you can replace procedure by CREATE OR REPLACE PROCEDURE <procedureName> …
9
Calling Stored Procedures
Calling Procedures call <procedureName> [(<paramList>)]; Embedded SQL: EXEC SQL BEGIN DECLARE SECTION Int sid; Int rating; EXEC SQL END DECLARE SECTION // now increase rating of this sailor EXEC CALL IncreaseRating(:sid,:rating);
10
Stored Procedures : Persistent Stored Modules (PSM)
11
SQL/PSM Most DBMSs allow users to write stored procedures in a simple general-purpose language (close to SQL) SQL/PSM standard is a representative of vendor-specific languages. Oracle supports a slightly different version of PSM called PL/SQL
12
Local Declarations CREATE PROCEDURE testProcedure (num IN int, name IN varchar) IS BEGIN INSERT INTO Student VALUES (num, name); END;
13
Local Declarations CREATE PROCEDURE testProcedure (num IN int, name IN varchar) IS num1 int; local variable BEGIN num1 := 10; INSERT INTO Student VALUES (num1, name); END;
14
Other PSM features Assignment statements: PL/SQL
<varName> := <expression>
15
Control Structures: IF THEN ELSE
IF <condition> THEN <statementList> ELSIF <condition> THEN ELSIF … ELSE <statementList> END IF;
16
Loops LOOP <statementList> END LOOP; To exit from a loop use
17
Loops: Example CREATE PROCEDURE testProcedure (num IN int, name IN varchar) IS num1 int; BEGIN num1 := 10; LOOP INSERT INTO Student VALUES (num1, name); num1 := num1 + 1; IF (num1 > 15) THEN EXIT; END IF; END LOOP; END;
18
FOR Loops FOR i in [REVERSE] <lowerBound> .. <upperBound> LOOP <statementList> END LOOP Example: FOR i in LOOP INSERT INTO Student (sNumber) values (10 + i); END LOOP;
19
WHILE LOOPS WHILE <condition> LOOP <statementList>
END LOOP;
20
Functions CREATE FUNCTION <functionName> [(<paramList>)] RETURNS type AS <localDeclarations> BEGIN <functionBody>; END; You can call a function as part of an SQL expression Drop a function: drop function <functionName>
21
Functions: Example CREATE FUNCTION testFunction RETURN int AS
num1 int; BEGIN SELECT MAX (sNumber) INTO num1 FROM Student; RETURN num1; END; SELECT * from Student where sNumber = testFunction ();
22
SQL/PSM Example CREATE FUNCTION rate Sailor (IN sailorId INTEGER) RETURNS INTEGER DECLARE rating INTEGER; DECLARE numRes INTEGER; BEGIN SET numRes = (SELECT COUNT(*) FROM Reserves R WHERE R.sid = sailorId); IF (numRes > 10) THEN rating =1; ELSE rating = 0; END IF; RETURN rating; END
23
Other Info Re Oracle : Oracle stores procedures and functions in catalog as relational tables: Check user_procedures Check user_functions You may run queries against them such as: describe user_procedures; select object_name from user_procedures;
24
Summary : Stored Procedures
Stored procedures execute application logic directly at the server SQL/PSM standard for writing stored procedural logic: Used stand-alone on explicit call Used functions in WHERE clause of SQL statement Used in body of triggers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.