Stored PL/SQL Dr Janusz R. Getta School of Computing and - - PowerPoint PPT Presentation

stored pl sql
SMART_READER_LITE
LIVE PREVIEW

Stored PL/SQL Dr Janusz R. Getta School of Computing and - - PowerPoint PPT Presentation

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 CSCI235 Database Systems Stored PL/SQL Dr Janusz R. Getta School of Computing and Information Technology - University of Wollongong 1 of 15


slide-1
SLIDE 1

CSCI235 Database Systems

Stored PL/SQL

Dr Janusz R. Getta

School of Computing and Information Technology - University of Wollongong

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 1 of 15 22/8/20, 9:54 pm

slide-2
SLIDE 2

Stored PL/SQL

Outline

Stored PL/SQL ? What is it ? Applications CREATE OR REPLACE PROCEDURE statement CREATE OR REPLACE FUNCTION statement GRANT statement revisited

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 2/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 2 of 15 22/8/20, 9:54 pm

slide-3
SLIDE 3

Stored PL/SQL ? What is it

Stored PL/SQL means PL/SQL procedures and PL/SQL functions pre- compiled and stored in a data dictionary ready to be processed Stored procedures and functions can be referenced or called any number of times by multiple applications processing the relational tables Stored procedures and functions can accept parameters when processed (called) Stored procedures can be processed (called) with EXECUTE statement Stored functions can be processed (called) in SQL statement wherever a function can be used, e.g. as row functions in SELECT statement Stored procedures and stored functions can be used to extend the functionality of data retrieval and data manipulation statements of SQL (extensibility) and to eliminate duplication of code in the database applications (re-useability)

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 3/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 3 of 15 22/8/20, 9:54 pm

slide-4
SLIDE 4

Stored PL/SQL ? What is it

Stored procedures and functions are created with CREATE OR REPLACE PROCEDURE and CREATE OR REPLACE FUNCTION SQL statements

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 4/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 4 of 15 22/8/20, 9:54 pm

slide-5
SLIDE 5

Stored PL/SQL

Outline

Stored PL/SQL ? What is it ? Applications CREATE OR REPLACE PROCEDURE statement CREATE OR REPLACE FUNCTION statement GRANT statement revisited

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 5/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 5 of 15 22/8/20, 9:54 pm

slide-6
SLIDE 6

Applications - reusability

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 6/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 6 of 15 22/8/20, 9:54 pm

slide-7
SLIDE 7

Applications - extensibility

Find the names of all departments together with a list of courses offered by each department, display the results in the following form: Implement a function LCOURSES( dept_name ) that returns a list of courses offered by a department whose name is a value of a parameter dept_name Use a function LCOURSES as a row function in SELECT statement A function LCOURSES is called for every row retrieved from a relational table DEPARTMENT like any standard row function, e.g. UPPER function

DEPARTMENT NAME | LIST OF COURSES OFFERED

  • Math

| Calculus Topology Logic Algebra Comp Sci | Python Java Databases Biol | Phys | Relativity Mechanics Astro | Astrology

SQL

SELECT dname, LCOURSES( dname ) FROM DEPARTMENT;

SQL TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 7/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 7 of 15 22/8/20, 9:54 pm

slide-8
SLIDE 8

Stored PL/SQL

Outline

Stored PL/SQL ? What is it ? Applications CREATE OR REPLACE PROCEDURE statement CREATE OR REPLACE FUNCTION statement GRANT statement revisited

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 8/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 8 of 15 22/8/20, 9:54 pm

slide-9
SLIDE 9

CREATE OR REPLACE PROCEDURE statement

CREATE OR REPLACE PROCEDURE statement compiles and stores PL/SQL procedure in a data dictionary The following stored procedure INSERT_COURSE converts the values of string parameters to upper case and inserts a row into a relational table COURSE EXECUTE statement is used to process a procedure INSERT_COURSE

CREATE OR REPLACE PROCEDURE INSERT_COURSE( cnumber IN NUMBER, ctitle IN VARCHAR, ccredits IN NUMBER, coffer IN VARCHAR) IS

Stored procedure

BEGIN INSERT INTO COURSE VALUES( cnumber, UPPER(ctitle), ccredits, UPPER(coffer) ); COMMIT; END INSERT_COURSE;

Stored procedure

EXECUTE INSERT_COURSE(666, 'Java for kids', 6, 'Comp Sci');

EXECUTE TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 9/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 9 of 15 22/8/20, 9:54 pm

slide-10
SLIDE 10

Stored PL/SQL

Outline

Stored PL/SQL ? What is it ? Applications CREATE OR REPLACE PROCEDURE statement CREATE OR REPLACE FUNCTION statement GRANT statement revisited

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 10/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 10 of 15 22/8/20, 9:54 pm

slide-11
SLIDE 11

CREATE OR REPLACE FUNCTION statement

CREATE OR REPLACE FUNCTION statement compiles and stores PL/SQL function in a data dictionary The following stored function LCOURSES lists the names of departments together with the titles of courses offered by each department

CREATE OR REPLACE FUNCTION LCOURSES( dept_name VARCHAR ) RETURN VARCHAR IS

Stored function

course_list VARCHAR(300); BEGIN course_list = '';

Stored function

FOR course_cur_rec IN (SELECT title FROM COURSE WHERE offered_by = dept_name);

Stored function

LOOP course_list := course_list || course_cur_rec.title || ' '; END LOOP;

Stored function

RETURN course_list; END LCOURSES;

Stored function TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 11/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 11 of 15 22/8/20, 9:54 pm

slide-12
SLIDE 12

CREATE OR REPLACE FUNCTION statement

A stored function LCOURSES is called as a row function in SELECT statement

SELECT dname, LCOURSES( dname ) FROM COURSE;

SQL TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 12/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 12 of 15 22/8/20, 9:54 pm

slide-13
SLIDE 13

Stored PL/SQL

Outline

Stored PL/SQL ? What is it ? Applications CREATE OR REPLACE PROCEDURE statement CREATE OR REPLACE FUNCTION statement GRANT statement revisited

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 13/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 13 of 15 22/8/20, 9:54 pm

slide-14
SLIDE 14

GRANT statement revisited

In addition to read and write access rights it is possible to grant EXECUTE rights on stored procedures and functions For example, a user scott grants execution rights on INSERT_COURSE to a user janusz Now, a user janusz executes a stored procedure INSERT_COURSE

GRANT EXECUTE ON INSERT_COURSE TO janusz;

SQL

EXECUTE scott.INSERT_COURSE(958, 'Multimedia Databases', 6, 'Comp Sci');

SQL TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 14/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 14 of 15 22/8/20, 9:54 pm

slide-15
SLIDE 15

References

Database PL/SQL Language Reference Database SQL Language Reference, CREATE PROCEDURE Database SQL Language Reference, CREATE FUNCTION Database SQL Language Reference, GRANT

  • T. Connoly, C. Begg, Database Systems, A Practical Approach to Design,

Implementation, and Management, Chapter 8 Advanced SQL, Pearson Education Ltd, 2015

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 15/15

Stored PL/SQL file:///Users/jrg/235-SPRING-2020/SLIDES/WEEK04/08storedplsql/08storedplsql.html#1 15 of 15 22/8/20, 9:54 pm