PLSQL
PLSQL
Deepa Rajesh
2004 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice
Introduction
Content
Introduction to PL/SQL Database Objects (Tables, Views, Data Types, indexes) Anonymous Blocks
'HP Restricted'
Basic Concepts
The following definitions explain concepts central to SQL*Plus: command : An instruction you give SQL*Plus or Oracle. block : A group of SQL and PL/SQL commands related to one another through procedural logic.
table
query
query results : The data retrieved by a query. report : Query results formatted by you through SQL*Plus commands.
February 17, 2014 'HP Restricted' 3
PL/SQL Introdution
PL/SQL is Oracles procedural language extension to the SQL language. It has its origins in ADA, a high-level programming language developed for the US Department of Defense. ADA was named after a daughter of Lord Byron, Augusta ADA, Countess of Lovelace, a 19th century mathematician, whom many regard as the worlds first computer programmer because of her work with Charles Babbages Analytical Engine. PL/SQL is an extremely powerful structured language, which is not only tightly integrated with SQL but also incorporates many of the advanced features of other procedural languages. It provides the foundation for distributing and processing transactions across the network for both client/server and Web applications. Despite its structured approach, PL/SQL it is also a language that , is very easy to abuse and misuse
February 17, 2014 'HP Restricted' 4
What is a table ?
'HP Restricted'
column constraints table constraints storage parameters default value for a column cache and no-cache
'HP Restricted'
Types of Tables
Stage Tables
Operational Tables
Functional Tables Disposition Tables
Archive tables
Audit Tables
'HP Restricted'
Views
A logical table based on one or more tables or views. A view contains no data itself. (Base Tables) To provide an additional level of table security, by restricting access to a predetermined set of rows and/or columns of a base table. To hide data complexity. For example, a view may be used to act as one table when actually several tables are used to construct the result. To present data from another perspective. For example, views provide a means of renaming columns without actually changing the base tables definition.
'HP Restricted'
'HP Restricted'
select empno,ename,dept.dname from emp, dept where emp.deptno = dept.deptno; create view empdept as select empno, ename, dept.dname from emp, dept where emp.deptno = dept.deptno ; select * from empdept ;
'HP Restricted'
10
PL/SQL
what is PL/SQL? A programming language which use SQL statements to maninpuate Oracle data and flow-control statements to process data Architecture Advantages of PL/SQL Block Structure Variable and Constants (not case sensitive) Cursors Attributes Control structures Modularity Error Handling
'HP Restricted'
11
Structure
PL/SQL is an algorithmic, block-structured language. A block is the basic unit from which all PL/SQL programs are built. A block can be named (functions and procedures) or anonymous. An anonymous block can exist only within a named block, a SQL script, or a trigger. A block consists of between one and four sections
Declaration Section : Declares variables, cursors etc. to be used in the block. BEGIN : Executable code Exception Section (optional): Exception (error) handling END;
Example: DECLARE <variable name> <data type><(length precision)>; BEGIN <valid statement>; BEGIN <valid statement>; EXCEPTION <exception handler>; END; EXCEPTION <exception handler>; END; /
'HP Restricted'
12
'HP Restricted'
13
PL/SQL Architecture
'HP Restricted'
14
Advantages of PL/SQL
'HP Restricted'
15
Data types
'HP Restricted'
16
Comments
Comment restrictions
Nested comments are not allowed
'HP Restricted'
17
birthday DATE;
'HP Restricted'
18
Attributes
%TYPE
balance number(7,2) ; minimum_balance balance%TYPE := 10.00; maximum_balace balance%TYPE ; local_empname scott.emp.empname%TYPE;
%ROWTYPE
cursor cur1 select * from emp ; emp_cur cur1%ROWTYPE; dept_cur scott.dept%ROWTYPE;
'HP Restricted'
19
The scope of an identifier is that region of a program unit from which you can reference the identifier. An identifier is visible only in the regions from which you can reference the identifier using an unqualified name.
'HP Restricted'
20
'HP Restricted'
21
Built in Functions
'HP Restricted'
22
Control Structures
'HP Restricted'
23
Conditional Control
'HP Restricted'
24
Iterative control
'HP Restricted'
25
WHIL ctr < max LOOP .. EXIT WHEN CUR%NOTFOUND END LOOP;
<<OUTER>>
FOR I IN 1..5 loop .. For j in 1..10 LOOP fetch c1 into emp_rec; EXIT outer WHEN c1%NOTFOUND . END LOOP; END LOOP ;
'HP Restricted'
26
Sequential Control
Syntax
Goto <<label>> ; Null (do nothing)
IF rating > 90 THEN GOTO calc_raise; -- branch to label END IF; ... <<calc_raise>> IF job_title = 'SALESMAN' THEN -- control resumes here amount := commission * 0.25; ELSE amount := salary * 0.10; END IF;
Restrictions
cannot branch into IF cannot branch into else cannot branch into LOOP cannot branch into sub-block cannot branch out of a procedure cannot branch out of execption handler to a block
'HP Restricted'
27
Cursors
cursor is used to manipulate or process queries that result in more than one row. Cursors can be declared in the declarative part of any PL/SQL block, subprogram, or package.
DECLARE CURSOR c1 IS SELECT empno, ename, job FROM emp WHERE deptno = 20;
'HP Restricted'
28
'HP Restricted'
29
BEGIN OPEN c1; -- here factor equals 2 LOOP FETCH c1 INTO my_sal; EXIT WHEN c1%NOTFOUND; factor := factor + 1; -- does not affect FETCH END LOOP; CLOSE c1; END;
'HP Restricted'
30