Introduction To PL/SQL: Kristian Torp
Introduction To PL/SQL: Kristian Torp
Introduction To PL/SQL: Kristian Torp
Kristian Torp
Department of Computer Science
Aalborg University
www.cs.aau.dk/torp
torp@cs.aau.dk
December 2, 2011
daisy.aau.dk
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
1 / 56
Outline
Introduction
Stored Procedures
An Overview
Data Types
Parameter Parsing
Cursors
Packages
Case Study: Table API
Summary
Introduction to PL/SQL
December 2, 2011
2 / 56
Outline
Introduction
Stored Procedures
An Overview
Data Types
Parameter Parsing
Cursors
Packages
Case Study: Table API
Summary
Introduction to PL/SQL
December 2, 2011
3 / 56
Learning Outcomes
Learning Outcomes
Understand how code can be executed within a DBMS
Be able to design stored procedures in general
Be able to construct and execute stored procedures on Oracle
Knowledge about the pros and cons of stored procedures
Note That
Concepts are fairly DBMS independent
All code examples are Oracle specific
Introduction to PL/SQL
December 2, 2011
4 / 56
Prerequisites
SQL
Knowledge about the SQL select statement
Knowledge about SQL modification statements, e.g., insert and delete
Knowledge about transaction management, e.g., commit and rollback
Knowledge about tables, views, and integrity constraints
Introduction to PL/SQL
December 2, 2011
5 / 56
Motivation
Purpose
Move processing into the DBMS from client programs (database
applications)
Advantages
Code accessible to all applications
I
http://en.wikipedia.org/wiki/SQL_injection
Introduction to PL/SQL
December 2, 2011
6 / 56
Motivation
Purpose
Move processing into the DBMS from client programs (database
applications)
Advantages
Code accessible to all applications
I
http://en.wikipedia.org/wiki/SQL_injection
Missing Standard
Unfortunately, the major DBMS vendors each have their own SQL dialect
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
6 / 56
Overview
Functionality
SQL extended with control structures
I
Used for
I
I
I
I
Focus
The focus is here on stored procedures and packages!
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
7 / 56
Outline
Introduction
Stored Procedures
An Overview
Data Types
Parameter Parsing
Cursors
Packages
Case Study: Table API
Summary
Introduction to PL/SQL
December 2, 2011
8 / 56
Increases readability
Implementation hiding
I
Modular programs
I
Library
I
Note
This is not different from any other procedural programming language!
Introduction to PL/SQL
December 2, 2011
9 / 56
Outline
Introduction
Stored Procedures
An Overview
Data Types
Parameter Parsing
Cursors
Packages
Case Study: Table API
Summary
Introduction to PL/SQL
December 2, 2011
10 / 56
Note
It is a procedure, i.e., not a function
I
Introduction to PL/SQL
December 2, 2011
11 / 56
Note
It takes two parameters height and weight
It is a function, i.e., has a return statement
It is strongly typed language, i.e., parameters and the return value
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
12 / 56
Introduction to PL/SQL
December 2, 2011
13 / 56
Introduction to PL/SQL
December 2, 2011
13 / 56
Introduction to PL/SQL
December 2, 2011
13 / 56
Introduction to PL/SQL
December 2, 2011
13 / 56
Note
Output from server is not enabled by default in a session!
Return value of a function cannot be ignored!
Introduction to PL/SQL
December 2, 2011
13 / 56
Note
The declaration of the variable v status
The usage of the into keyword in the select statement
The usage of the parameter student id in the select statement
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
14 / 56
Introduction to PL/SQL
December 2, 2011
15 / 56
Example (Caller)
c r e a t e o r r e p l a c e procedure c a l l p i s
begin
p ( H e l l o ) ; p ( World ! ) ;
end ;
Introduction to PL/SQL
December 2, 2011
15 / 56
Example (Caller)
c r e a t e o r r e p l a c e procedure c a l l p i s
begin
p ( H e l l o ) ; p ( World ! ) ;
end ;
Note
Can call own and built-in stored procedures
Will use the procedure p instead of dbms output.put line
You are now officially a PL/SQL library builder!!!
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
15 / 56
Note
Is this stupid?
Recall three-valued logic the root of all evil!
We will use the procedure pb in the code that follows!
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
16 / 56
Note
What is printed 1 to 9 or 1 to 10?
PL/SQL also has a for statement, very different from C
PL/SQL does not have increment/decrement operators, e.g., i or ++j
PL/SQL does not have compound assignments, e.g., i +=7 or j =2
Introduction to PL/SQL
December 2, 2011
17 / 56
Surprises: In General
Surprises
The code is stored in the DBMS!
A strong-point of PL/SQL
Introduction to PL/SQL
December 2, 2011
18 / 56
Outline
Introduction
Stored Procedures
An Overview
Data Types
Parameter Parsing
Cursors
Packages
Case Study: Table API
Summary
Introduction to PL/SQL
December 2, 2011
19 / 56
An Example
Example (Various Data Types)
c r e a t e o r r e p l a c e procedure u s e b a s i c t y p e s i s
v s t r v a r c h a r 2 ( 3 0 ) : = A s t r i n g ;
v i n t i n t : = 2 65;
c p i c o n s t a n t f l o a t : = 3.14159265358979323846264338327950288419;
v f l o a t f l o a t := v i n t c p i ;
begin
p( v str ); p( v int ); p( v float );
end ;
Output
A string
36893488147419103232
115904311329233965478,149216911761758199
Note
Forget what you think of data types and size!
Very high precision on all number types in both SQL and PL/SQL
The size of strings must be defined
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
20 / 56
Floats
Strings
Boolean
Date/time
Type
Examples
smallint
int/integer
positive
number
dec/decimal
real
varchar2
nvarchar2
char
Boolean
date
timestamp
-100, 0, 100
-1000, 0, 1000
0, 1, 2, 3
10.3
123.456, 3.4
123456.7890
Hello, Theta-Join
Tger, Dmon
World, Noise
True, False
2007-09-09
2009-09-09 12:34:56
Note
Not all of these data types are available from within SQL!
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
21 / 56
r e p l a c e procedure u s i n g d e c i m a l i s
decimal ( 4 , 2 ) ;
:= 12.34;
output . p u t l i n e ( v dec ) ;
:= 12.344;
output . p u t l i n e ( v dec ) ;
:= 12.347;
output . p u t l i n e ( v dec ) ;
Questions
Will this compile, note that it is decimal(4,2)?
What will be printed (if it compiles)?
Are you surprised?
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
22 / 56
Type
Composite
Large Objects
Reference Types
Record
Varray
Table
BLOB
CLOB
BFILE
REF
REF CURSOR
Note
We will only use records in this lecture.
Introduction to PL/SQL
December 2, 2011
23 / 56
Note
The anchored type using the %type
Very convenient of maintenance reasons (avoid hard-wiring types)
I
Introduction to PL/SQL
December 2, 2011
24 / 56
Note
The anchored type using the rowtype
I
Creates a record
Introduction to PL/SQL
December 2, 2011
25 / 56
The sizes of the basic data type are very different from C and Java
Date and time are basic data types!
I
Introduction to PL/SQL
December 2, 2011
26 / 56
Outline
Introduction
Stored Procedures
An Overview
Data Types
Parameter Parsing
Cursors
Packages
Case Study: Table API
Summary
Introduction to PL/SQL
December 2, 2011
27 / 56
Overview
Example
create or
v tmp
begin
v tmp
v a l
end ;
r e p l a c e procedure p i n ( v a l i n i n t ) i s
int ;
:= val + 5;
: = v a l + 5 ; / i l l e g a l v a l i s read o n l y /
Example
c r e a t e o r r e p l a c e procedure p i n o u t ( v a l i n o u t i n t ) i s
begin
val := val + 5;
end ;
Example
c r e a t e o r r e p l a c e procedure c a l l p s i s
v in
i n t := 10;
v i n o u t i n t := 10;
begin
p in ( v in ) ; p( v in ) ;
p in out ( v in out ) ; p( v in out ) ;
end ;
Introduction to PL/SQL
December 2, 2011
28 / 56
Quiz
Example
c r e a t e o r r e p l a c e procedure p i n o u t ( v a l i n o u t i n t ) i s
begin
val := val + 5;
end ;
Example
c r e a t e o r r e p l a c e procedure c a l l p s i s
v in
i n t := 10;
v i n o u t i n t := 10;
begin
p in ( v in ) ; p( v in ) ;
p in out ( v in out ) ; p( v in out ) ;
end ;
Questions
What are the formal parameter(s)?
What are the actual parameter(s)?
Is it call-by-value or call-by-reference?
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
29 / 56
Out Parameters
Example
c r e a t e o r r e p l a c e procedure g e t x y c o o r (
c o o r i d i n i n t , x coor out i n t , y coor out i n t )
is
begin
x c o o r : = round ( c o o r i d / 4 . 2 ) ; s t u p i d c a l c u l a t i o n s
y c o o r : = round ( c o o r i d / 7 . 5 ) ;
end ;
Note
in
Introduction to PL/SQL
December 2, 2011
30 / 56
Parameter Mode
Mode
Mode
Description
in
out
in out
Note
in
Introduction to PL/SQL
December 2, 2011
31 / 56
Introduction to PL/SQL
December 2, 2011
32 / 56
Avoid This
Introduction to PL/SQL
December 2, 2011
33 / 56
Items to Notice
A default value can be provided for each parameter
Stored procedures cannot be overloaded on the parameter signature
Stored procedures can be called by position or by name
Works like in most programming languages, however different syntax!
Introduction to PL/SQL
December 2, 2011
34 / 56
Outline
Introduction
Stored Procedures
An Overview
Data Types
Parameter Parsing
Cursors
Packages
Case Study: Table API
Summary
Introduction to PL/SQL
December 2, 2011
35 / 56
Overview
Definition
A cursor is a mechanism that ensure a result set can be identified by a
declarative language such as SQL and processed by a procedural
language such as PL/SQL or C#
Introduction to PL/SQL
December 2, 2011
36 / 56
Overview
Definition
A cursor is a mechanism that ensure a result set can be identified by a
declarative language such as SQL and processed by a procedural
language such as PL/SQL or C#
Solution
Solves the well-known impedance mismatch problem!
Introduction to PL/SQL
December 2, 2011
36 / 56
Overview
Definition
A cursor is a mechanism that ensure a result set can be identified by a
declarative language such as SQL and processed by a procedural
language such as PL/SQL or C#
Solution
Solves the well-known impedance mismatch problem!
Generality
Knowledge about cursors in PL/SQL is directly transferable to many other
programming languages.
Introduction to PL/SQL
December 2, 2011
36 / 56
Note
The view tab is a table that contains all table names
The cursor is declared, opened, fetched, and closed
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
37 / 56
Cursor Attributes
Attributes
Attribute
notfound
found
rowcount
isopen
Type
Boolean
Boolean
Integer
Boolean
Description
True if a record is fetched unsuccessfully
True if a record is fetched successfully
The number of records fetched from the cursor
True if cursor is open
Note
There are additional attributes for bulk operations.
Introduction to PL/SQL
December 2, 2011
38 / 56
Question
What is printed?
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
39 / 56
);
);
);
);
Question
What is printed?
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
40 / 56
Outline
Introduction
Stored Procedures
An Overview
Data Types
Parameter Parsing
Cursors
Packages
Case Study: Table API
Summary
Introduction to PL/SQL
December 2, 2011
41 / 56
Introduction
Idea
A class like concept
Very good for building libraries
I
Introduction to PL/SQL
December 2, 2011
42 / 56
Outline
Introduction
Stored Procedures
An Overview
Data Types
Parameter Parsing
Cursors
Packages
Case Study: Table API
Summary
Introduction to PL/SQL
December 2, 2011
43 / 56
Introduction
Goal
To build a uniform way to address the data stored in table!
Methods
Name
Description
exist(<primary key>)
to string(<primary key>)
print(<primary key>)
Introduction to PL/SQL
December 2, 2011
44 / 56
Introduction
Goal
To build a uniform way to address the data stored in table!
Methods
Name
Description
exist(<primary key>)
to string(<primary key>)
print(<primary key>)
Note
Many more methods can be envisioned
Think object-relational mapping (ORM) tools
Introduction to PL/SQL
December 2, 2011
44 / 56
Note
The header lists all the public stored procedures
The naming convention table name course package name ccourse
The design is influenced by the Object class from Java and C#
Introduction to PL/SQL
December 2, 2011
45 / 56
Note
The header lists all the public stored procedures
The naming convention table name course package name ccourse
The design is influenced by the Object class from Java and C#
Quiz
Why is the method called exist and not exists?
Introduction to PL/SQL
December 2, 2011
45 / 56
Introduction to PL/SQL
December 2, 2011
46 / 56
Note
The method check valid cid is private
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
46 / 56
Note
Uses the private method check valid cid to check preconditions
Uses the private cursor cur exist
Returns true if a valid primary key is found
Introduction to PL/SQL
December 2, 2011
47 / 56
Note
Uses the private method check valid cid to check preconditions
Uses the private cursor cur exist
Kristian Torp (Aalborg University)
Introduction to PL/SQL
December 2, 2011
48 / 56
Example (Method)
procedure p r i n t ( c i d course . c i d%t y p e ) i s
begin
c h e c k v a l i d c i d ( c i d ) ; p r e c o n d i t i o n
dbms output . p u t l i n e ( t o s t r i n g ( c i d ) ) ;
end ;
Note
Uses the private method check valid cid to check preconditions
Introduction to PL/SQL
December 2, 2011
49 / 56
Introduction to PL/SQL
December 2, 2011
50 / 56
Note
Similar to executing a stored procedure
Access member by the dot notation
Introduction to PL/SQL
December 2, 2011
50 / 56
Note
Similar to executing a stored procedure
Access member by the dot notation
Example
SQL> execute ccourse . p r i n t ( n u l l ) ;
Note
Results in an error ORA-20001: Course ID is null
Introduction to PL/SQL
December 2, 2011
50 / 56
Summary: Packages
Main Points
Can have a public and a private part
I
Introduction to PL/SQL
December 2, 2011
51 / 56
Outline
Introduction
Stored Procedures
An Overview
Data Types
Parameter Parsing
Cursors
Packages
Case Study: Table API
Summary
Introduction to PL/SQL
December 2, 2011
52 / 56
Advantages
Advantages
A complete programming language
I
Introduction to PL/SQL
December 2, 2011
53 / 56
Disadvantages
Disadvantages
Proprietary programming language
There is a very large number (>1000) of reserved words
I
Introduction to PL/SQL
December 2, 2011
54 / 56
Additional Information
Web Sites
www.oracle.com/technology/tech/pl_sql/index.html
PL/SQLs home
en.wikibooks.org/wiki/Oracle_Programming/SQL_Cheatsheet
A short overview of PL/SQL
www.java2s.com/Tutorial/Oracle/CatalogOracle.htm Many
good examples, too many commercials
Introduction to PL/SQL
December 2, 2011
55 / 56