Oracle Basics (PLSQL) ClassBook Lesson01
Oracle Basics (PLSQL) ClassBook Lesson01
Introduction to PL/SQL
Instructor Notes:
Page 01-1
Introduction to PL/SQL
Instructor Notes:
None
Page 01-2
Introduction to PL/SQL
Instructor Notes:
None
Introduction to PL/SQL:
PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by
adding constructs found in procedural languages, resulting in a structural
language that is more powerful than SQL.
With PL/SQL, you can use SQL statements to manipulate Oracle
data and flow-of-control statements to process the data.
Moreover, you can declare constants and variables, define
procedures and functions, and trap runtime errors.
Thus PL/SQL combines the data manipulating power of SQL with
the data processing power of procedural languages.
PL/SQL is an embedded language. It was not designed to be used as a
standalone language but instead to be invoked from within a host
environment.
You cannot create a PL/SQL executable that runs all by itself.
It can run from within the database through SQL*Plus interface or
from within an Oracle Developer Form (called client-side PL/SQL).
Page 01-3
Introduction to PL/SQL
Instructor Notes:
None
Features of PL/SQL
Tight Integration with SQL:
This integration saves both, your learning time as well as your
processing time.
PL/SQL supports SQL data types, reducing the need to
convert data passed between your application and
database.
PL/SQL lets you use all the SQL data manipulation, cursor
control, transaction control commands, as well as SQL
functions, operators, and pseudo columns.
Better Performance:
Several SQL statements can be bundled together into one PL/SQL
block, and sent to the server as a single unit.
This results in less network traffic and a faster application. Even
when the client and the server are both running on the same
machine, the performance is increased. This is because packaging
SQL statements results in a simpler program that makes fewer
calls to the database.
Portable:
PL/SQL is a standard and portable language.
A PL/SQL function or procedure written from within the Personal
Oracle database on your laptop will run without any modification on
your corporate network database. It is Write once, run everywhere
with the only restriction being everywhere there is an Oracle
Database.
Efficient:
Although there are a number of alternatives when it comes to
writing software to run against the Oracle Database, it is easier to
run highly efficient code in PL/SQL, to access the Oracle Database,
than in any other language.
Page 01-4
Introduction to PL/SQL
Instructor Notes:
None
Page 01-5
Introduction to PL/SQL
Instructor Notes:
None
Block Types:
The basic units (procedures and functions, also known as subprograms,
and anonymous blocks) that make up a PL/SQL program are logical
blocks, which can contain any number of nested sub-blocks.
Therefore one block can represent a small part of another block, which in
turn can be part of the whole unit of code.
Anonymous Blocks
Anonymous blocks are unnamed blocks. They are declared at the
point in an application where they are to be executed and are
passed to the PL/SQL engine for execution at runtime.
Named :
Subprograms
Subprograms are named PL/SQL blocks that can take
parameters and can be invoked. You can declare them
either as procedures or as functions.
Generally, you use a procedure to perform an action
and a function to compute a value.
Page 01-7
Introduction to PL/SQL
Instructor Notes:
None
Page 01-9
Introduction to PL/SQL
Instructor Notes:
None
Page 01-10
Introduction to PL/SQL
Instructor Notes:
PL/SQL Datatypes
Scalar type
(Number, varchar2,
Boolean, Binary_Integer,
etc.)
Reference type
(tablename.columnname
%type
Tablename%rowtype)
Composite type
PL/SQL record type
PL/SQL table type
Page 01-11
Introduction to PL/SQL
Instructor Notes:
None
Page 01-12
Introduction to PL/SQL
Instructor Notes:
None
Page 01-14
Introduction to PL/SQL
Instructor Notes:
None
begin
dbms_output.put_line('Area:
'||pie*power(radius,2));
dbms_output.put_line('Diameter: '||2*pie*radius);
end;
/
Page 01-15
Introduction to PL/SQL
Instructor Notes:
None
Reference types:
A reference type in PL/SQL is the same as a pointer in C. A reference
type variable can point to different storage locations over the life of the
program.
Using %TYPE
%TYPE is used to declare a variable with the same datatype as a column
of a specific table. This datatype is particularly used when declaring
variables that will hold database values.
Advantage:
You need not know the exact datatype of a column in the table in
the database.
If you change database definition of a column, it changes
accordingly in the PL/SQL block at run time.
Syntax:
Var_Name
V_Empno
table_name.col_name%TYPE;
emp.empno%TYPE;
Page 01-16
Introduction to PL/SQL
Instructor Notes:
None
Page 01-17
Introduction to PL/SQL
Instructor Notes:
None
Using %ROWTYPE
%ROWTYPE is used to declare a compound variable, whose type is same
as that of a row of a table.
Columns in a row and corresponding fields in record should have same
names and same datatypes. However, fields in a %ROWTYPE record do
not inherit constraints, such as the NOT NULL, CHECK constraints, or
default values.
Syntax:
V_Emprec emp%rowtype
Var_Name
V_Emprec
table_name%ROWTYPE;
emp%ROWTYPE;
Introduction to PL/SQL
Instructor Notes:
None
Page 01-20
Introduction to PL/SQL
Instructor Notes:
None
Record Datatype:
A record is a collection of individual fields that represents a row in the table.
They are unique and each has its own name and datatype. The record as a
whole does not have value. By using records you can group the data into
one structure and then manipulate this structure into one entity or logical
unit. This helps to reduce coding and keeps the code easier to maintain
and understand.
Page 01-21
Introduction to PL/SQL
Instructor Notes:
None
Page 01-22
Introduction to PL/SQL
Instructor Notes:
None
Page 01-23
Introduction to PL/SQL
Instructor Notes:
None
Page 01-24
Introduction to PL/SQL
Instructor Notes:
None
Page 01-25
Introduction to PL/SQL
Instructor Notes:
None
Page 01-26
Introduction to PL/SQL
Instructor Notes:
None
Page 01-27
Introduction to PL/SQL
Instructor Notes:
None
Page 01-28
Introduction to PL/SQL
Instructor Notes:
None
Page 01-29
Introduction to PL/SQL
Instructor Notes:
None
Page 01-30
Introduction to PL/SQL
Instructor Notes:
None
Page 01-31
Introduction to PL/SQL
Instructor Notes:
None
SELECT Statement:
Note:
The SELECT clause is used if the selected row must be modified through a
DELETE or UPDATE command.
Page 01-32
Introduction to PL/SQL
Instructor Notes:
None
Page 01-33
Introduction to PL/SQL
Instructor Notes:
None
Programming Constructs:
The selection structure tests a condition, then executes one sequence of
statements instead of another, depending on whether the condition is
TRUE or FALSE.
A condition is any variable or expression that returns a Boolean
value (TRUE or FALSE).
The iteration structure executes a sequence of statements repeatedly as
long as a condition holds true.
The sequence structure simply executes a sequence of statements in the
order in which they occur.
Page 01-35
Introduction to PL/SQL
Instructor Notes:
None
contd.
Page 01-36
Introduction to PL/SQL
Instructor Notes:
None
Page 01-37
Introduction to PL/SQL
Instructor Notes:
None
Page 01-38
Introduction to PL/SQL
Instructor Notes:
None
DECLARE
D VARCHAR2(3): = TO_CHAR(SYSDATE, DY)
BEGIN
IF D= SAT THEN
DBMS_OUTPUT.PUT_LINE(ENJOY YOUR
WEEKEND);
ELSIF D= SUN THEN
DBMS_OUTPUT.PUT_LINE(ENJOY YOUR
WEEKEND);
ELSE
DBMS_OUTPUT.PUT_LINE(HAVE A NICE
DAY);
END IF;
END;
Page 01-39
Introduction to PL/SQL
Instructor Notes:
None
Page 01-41
Introduction to PL/SQL
Instructor Notes:
None
Page 01-42
Introduction to PL/SQL
Instructor Notes:
None
Page 01-43
Introduction to PL/SQL
Instructor Notes:
None
Note:
EXIT WHEN is used for conditional exit out of the loop.
Page 01-44
Introduction to PL/SQL
Instructor Notes:
None
Note:
LOOP.. END LOOP can be used in conjunction with FOR and WHILE for better
control on looping.
Page 01-45
Introduction to PL/SQL
Instructor Notes:
None
Page 01-46
Introduction to PL/SQL
Instructor Notes:
None
DECLARE
ctr number := 1;
BEGIN
WHILE ctr <= 10
LOOP
dbms_output.put_line(ctr);
ctr := ctr+1;
END LOOP;
END;
/
Page 01-48
Introduction to PL/SQL
Instructor Notes:
None
Page 01-49
Introduction to PL/SQL
Instructor Notes:
None
Page 01-50
Introduction to PL/SQL
Instructor Notes:
None
Page 01-51
Introduction to PL/SQL
Instructor Notes:
None
Page 01-52
Introduction to PL/SQL
Instructor Notes:
Page 01-53