Advanced PL SQL Concepts
Advanced PL SQL Concepts
Chapter 9
A Guide to Oracle9i
Lesson A Objectives
Learn how to create and use indexes Become familiar with PL/SQL stored program units Learn how to create server-side stored program units in SQL*Plus Learn how to use Forms Builder to create stored program units
A Guide to Oracle9i
Database Indexes
Similar to an index in a book Table with list of sorted data values and corresponding physical location Used to speed searches Uses ROWID column to represent physical location Primary key indexed automatically Unlimited number allowed, but more indexes means more processing time for action queries (insert, update, delete)
A Guide to Oracle9i 3
Creating an Index
Create index after table data is loaded CREATE INDEX index_name ON tablename (index_fieldname); Convention for naming index: tablename_fieldname.
A Guide to Oracle9i
Composite Index
Contains multiple (up to 16) sorted columns Used for queries with multiple search conditions CREATE INDEX index_name ON tablename(index_fieldname1, index_fieldname2, );
A Guide to Oracle9i
A Guide to Oracle9i
Dropping an Index
If an index is no longer needed or does not improve performance, delete it DROP INDEX index_name;
A Guide to Oracle9i
A Guide to Oracle9i
A Guide to Oracle9i
10
A Guide to Oracle9i
11
A Guide to Oracle9i
12
A Guide to Oracle9i
13
A Guide to Oracle9i
14
A Guide to Oracle9i
15
A Guide to Oracle9i
16
A Guide to Oracle9i
17
A Guide to Oracle9i
18
A Guide to Oracle9i
19
A Guide to Oracle9i
20
A Guide to Oracle9i
21
Calling a Function
variable_name := function_name(parameter1, parameter2, ...);
A Guide to Oracle9i
22
A Guide to Oracle9i
24
Lesson B Objectives
Learn how to call stored procedures from other stored procedures and pass parameter values Create libraries Create packages Create database triggers
A Guide to Oracle9i
25
A Guide to Oracle9i
26
PL/SQL Libraries
Operating system file that contains code for multiple related procedures and functions Attach a PL/SQL library to a form or report
Triggers within the form or report reference librarys procedures and functions
Store a PL/SQL library in the file system of the client workstation .pll extension - stands for PL/SQL Library Compile the library into a library executable file - .plx extension - stands for PL/SQL Library Executable Library places the commands for multiple related program units in a single location that developers can access and use A Guide to Oracle9i 27
A Guide to Oracle9i
28
Packages
Another way to make PL/SQL program units available to multiple applications A code library that contains related program units and variables Stored in the database and executes on the database server Have more functionality than PL/SQL libraries:
Can create variables in packages Definitions for explicit cursors More convenient to use than PL/SQL libraries Available without explicitly attaching them to a form or report
A Guide to Oracle9i 29
Package Specification
Also called package header Declares package objects, including variables, cursors, procedures, and functions, Use to declare public variables:
Remain in memory after the programs that declare and reference them terminate Declared in the DECLARE section of a package Referenced same as private variables
A Guide to Oracle9i 30
Package Specification
A Guide to Oracle9i
31
Package Header
Package_name identifies the package
Must adhere to the Oracle Naming Standard
Declare the package objects in any order Package can consist of just variable declarations, or it can consist of just procedure or function declarations
A Guide to Oracle9i
32
A Guide to Oracle9i
33
Package Body
Contains the implementation of declared procedures and functions Specification comes before body Optional: sometimes a package contains only variable or cursor declarations, and no procedure or function declarations See Figure 9-35 for general syntax
A Guide to Oracle9i
34
Package Body
Package_name in the package body must be the same as package_name in the package specification Variables that you declare at the beginning of the package body are private to the package Each package program unit has its own declaration section and BEGIN and END statements Each program unit declared in the package body must have a matching program unit forward declaration in the package specification, with an identical parameter list
A Guide to Oracle9i 35
A Guide to Oracle9i
36
A Guide to Oracle9i
37
A Guide to Oracle9i
38
A Guide to Oracle9i
39
Database Triggers
Program units that execute in response to the database events of inserting, updating, or deleting a record Different from form triggers Useful for maintaining integrity constraints and audit information Cannot accept input parameters Executes only when its triggering event occurs
A Guide to Oracle9i
40
Trigger Properties
Trigger timing:
Defines whether a trigger fires before or after the SQL statement executes Can have the values BEFORE or AFTER
Trigger statement:
Defines the type of SQL statement that causes a trigger to fire Can be INSERT, UPDATE, or DELETE
A Guide to Oracle9i
41
Trigger Properties
Trigger level:
Defines whether a trigger fires once for each triggering statement or once for each row affected by the triggering statement Can have the values ROW or STATEMENT Statement-level triggers fire once, either before or after the SQL triggering statement executes. Row-level triggers fire once for each row affected by the triggering statement
Use :OLD.fieldname to reference previous value Use :NEW.fieldname to reference changed value
A Guide to Oracle9i 42
A Guide to Oracle9i
43
A Guide to Oracle9i
44
A Guide to Oracle9i
45
A Guide to Oracle9i
46
A Guide to Oracle9i
47
A Guide to Oracle9i
48
To disable/enable a trigger:
ALTER TRIGGER trigger_name [ENABLE | DISABLE];
A Guide to Oracle9i
49
A Guide to Oracle9i
50
Summary
Database indexes store an ordered list of field values with corresponding ROWID Indexes are used to speed query performance Stored program units are named PL/SQL blocks that are saved Procedures accept parameters and return 0,1, or many values Functions accept parameters and return exactly one value
A Guide to Oracle9i 51
Summary
PL/SQL Library is a client-side file containing procedures and functions PL/SQL Package is a collection of public variables, cursors, procedures and functions stored in the DBMS Database triggers are PL/SQL blocks that are run in response to table changes Database triggers are used to enforce integrity constraints and track changes Forms Builder may be used as an IDE to develop functions, procedures, libraries, packages and triggers
A Guide to Oracle9i 52