Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
www.theoracletrainer.com
raghu@theoracletrainer.com
Contents
Structure of PL/SQL PL/SQL programming fundamentals Error Handling Cursor manipulation PL/SQL types Procedures and Functions Database triggers Introduction to Packages
www.theoracletrainer.com
raghu@theoracletrainer.com
PL/SQL
Procedural Language provided by ORACLE Is a technology and not a product PL/SQL is a block-structured language Used to create and store Oracle objects such as Stored Procedures, Functions and Database Triggers These objects can be called by client applications
www.theoracletrainer.com
raghu@theoracletrainer.com
PL/SQL
PL/SQL Blocks of code can be embedded in host languages Using PL/SQL, you can define and execute PL/SQL procedures, functions and packages PL/SQL engine executes PL/SQL blocks of code.It can reside in two environments The Oracle server Oracle tools
www.theoracletrainer.com
raghu@theoracletrainer.com
PL/SQL Engine
Procedural PL/SQL block Procedural Statement Statement Executor SQL SQL Statement Executor
www.theoracletrainer.com
Structure of PL/SQL
Declarations
Optional
Optional
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
Identifiers
Definition An identifier is a name of a PL/SQL object. Properties Max 30 characters Must start with an alphabet Can include $, _, and # Cannot contain spaces,slashes and hyphens Should not be a reserved word PL/SQL is not case sensitive except within string and character literals www.theoracletrainer.com raghu@theoracletrainer.com
Comments
PL/SQL supports two comment styles: singleline and multi-line. Single-line comments begin with a double hyphen (--) anywhere on a line and extend to the end of the line Ex: -- DELETE FROM emp WHERE comm IS NULL; Multi-line comments begin with a slash-asterisk (/*), end with an asterisk-slash (*/), and can span multiple lines. Ex: /* The following line computes the area of a circle using pi, which is the ratio between the circumference and diameter. */ www.theoracletrainer.com raghu@theoracletrainer.com
Variables
Temporary storage of data Manipulation of stored values Reusability Ease of maintenance
www.theoracletrainer.com
raghu@theoracletrainer.com
Types of Variables
PL/SQL variables Scalar Scalar data types hold a single value. The main data types are those that correspond to column types in Oracle server tables: Pl/SQL also supports BOOLEAN variables. Composite Composite data types such as records, allow group of fields to be defined and manipulated in PL/SQL blocks. LOB (large objects) LOB data types hold values , called locators, that specify the location of large objects (such as graphic images) Non-PL/SQL variables: Bind and host variables
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
Database objects
Table Basic unit of storage; composed of rows and columns View Logically represents subsets of data from one or more tables Sequence Generates primary key values Index Improves the performance of some quires Synonym Alternative name for an object
www.theoracletrainer.com
raghu@theoracletrainer.com
Creating a View
Syntax CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias(,alias].)] as subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY [CONSTRAINT constraint]];
www.theoracletrainer.com
raghu@theoracletrainer.com
Contd..
OR REPLACE Re-creates the view if it already exists FORCE Creates the view regardless of weather or not the base table exists NOFORCE Creates the view only if base table exists (this is the default) View Is the name of the view Alias Specifies names for the expressions selected by the views query Subquery Is a complete SELECT statment WITH CHECK OPTION Specifies that only rows accessible to the view can be inserted or updated Constraint is the name assigned to the CHECK OPTION constraint WITH READ ONLY Ensures that no DML operations can be performed on this view
www.theoracletrainer.com
raghu@theoracletrainer.com
CREATE SEQUENCE seq_name START WITH n INCREMENT BY n MAXVALUE n MINVALUE n CYCLE/NOCYCLE CACHE 20;
CREATE INDEX CREATE INDEX index_name ON table_name(column_name) COMPOSITE INDEXS CREATE INDEX index_name ON table_name(col1,col2,col3);
www.theoracletrainer.com
raghu@theoracletrainer.com
Anchored Declarations
%TYPE Enables you to declare the variables datatype being the same as the specified columns data type or a variables datatype %ROWTYPE Can declare a composite variable that is equivalent to a row in the specified table Anchored data types provide synchronization with database columns
www.theoracletrainer.com
raghu@theoracletrainer.com
Example
Using %TYPE DECLARE emp_name
emp.ename%TYPE;
www.theoracletrainer.com
raghu@theoracletrainer.com
IF construct GOTO statement LOOPs simple or infinite loop FOR loop WHILE loop
www.theoracletrainer.com
raghu@theoracletrainer.com
IF Construct
IF <condition> THEN .... END IF;
IF <condition> THEN .... [ELSIF<condition > THEN] ...... [ELSE] ... END IF;
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
GOTO Statement
To unconditionally transfer control. GOTO <labelname> <labelname> to be provided in double angle brackets << >>
www.theoracletrainer.com
raghu@theoracletrainer.com
Note:
At least one statement must follow a label You cannot transfer into an IF construct You cannot go to the middle of a LOOP You cannot transfer control from an executable section to an exception and vice versa You cannot use GOTO to enter a block
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
FOR Loop
1.
www.theoracletrainer.com
raghu@theoracletrainer.com
3.
www.theoracletrainer.com
raghu@theoracletrainer.com
WHILE Loop
www.theoracletrainer.com
raghu@theoracletrainer.com
CURSORS
Oracle uses work areas to execute SQL statements and store processing information A cursor is a PL/SQL construct using which the work area can be named and the stored information can be accessed There are two kinds of cursors: implicit and explicit
www.theoracletrainer.com
raghu@theoracletrainer.com
Types of Cursors
Implicit Cursors Automatically created and destroyed by Oracle Created for every DML Explicit Cursors To be declared by the user and used using the open, fetch and close statements
www.theoracletrainer.com
raghu@theoracletrainer.com
Explicit Cursors-Usage
DECLARE CURSOR cursor_name IS <SELECT statement> OPEN OPEN cursor_name; FETCH FETCH cursor_name INTO cursor_rec CLOSE CLOSE cursor_name;
www.theoracletrainer.com
raghu@theoracletrainer.com
Cursor Declarations
DECLARE empname emp.name%TYPE; empsal emp.salary%TYPE; CURSOR emp_cur IS SELECT name,salary FROM WHERE dept=10;
emp
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
RECORDS
RECORDS TABLE BASED CURSOR BASED PROGRAMMER DEFINED SYNTAX TYPE <typename> IS RECORD (<field_name> <datatype>,.....); <recordname> <record_type>;
www.theoracletrainer.com
raghu@theoracletrainer.com
ADVANTAGES OF RECORDS
Data Abstraction Aggregate operations Leaner and cleaner code
www.theoracletrainer.com
raghu@theoracletrainer.com
PL/SQL Tables
One Column, One Primary key Column any scalar data type; Primary key is BINARY_INTEGER Can refer using the primary key value
www.theoracletrainer.com
raghu@theoracletrainer.com
PL/SQL Tables
Data structure that contains multiple data items that are the same data type Each table item has a key and a value Column any scalar data type; Primary key is BINARY_INTEGER Used to create a lookup table that is stored in memory to improve processing speed Comparable to a single dimension Array
www.theoracletrainer.com
raghu@theoracletrainer.com
PL/SQL tables
Cannot assign values directly ename_tab ENameTabType = (Nam1, Name2) --Not allowed Cannot reference in INTO clause SELECT ename INTO ename_tab FROM emp; --Not allowed
www.theoracletrainer.com
raghu@theoracletrainer.com
Exception Handling
PL/SQL traps errors using exception handlers. Types of exceptions Named System exceptions Unnamed System exceptions Programmer defined exceptions
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com
PRAGMA EXCEPTION_INIT
Eg: an integrity violation condition like referential integrity violation returns a negative sqlcode but this is not named explicity by PL/SQL Associates an ORACLE error code with a user defined Exception
www.theoracletrainer.com
raghu@theoracletrainer.com
DECLARE exception_A BEGIN RAISE exception_A EXCEPTION exception_A error handler END;
additional program statements EXCEPTION error handling statements END;
If an exception is raised and handled in an inner block, program execution resumes in the outer block
Exception is raised and handled in inner block Program execution resumes here Exceptions raised in inner blocks can be handled by exception handlers in outer raghu@theoracletrainer.com blocks
www.theoracletrainer.com
www.theoracletrainer.com
raghu@theoracletrainer.com