View
277
Download
1
Category
Tags:
Preview:
Citation preview
Chapter 9: Advanced SQL and PL/SQL Topics
Guide to Oracle 10g
Guide to Oracle 10g 2
Lesson A Objectives
After completing this lesson, you should be able to:• Create and use indexes• Work with PL/SQL stored program units• Create server-side stored program units in
SQL*Plus• Use Forms Builder to create stored program units
Guide to Oracle 10g 3
Database Indexes
• Database table index – Distinct database table
– Contains data values with corresponding columns that specify physical locations of records that contain data values
• ROWID– Internal location of record in database
– Encoded using internal data format
Guide to Oracle 10g 4
Database Indexes (continued)
• Index on specific table field– ROWID value
– Sorted indexed field value
• Oracle 10g automatically creates index on table’s primary key– Create indexes on columns that users often use in
search conditions
Guide to Oracle 10g 5
Creating an Index
• Create after adding data• Syntax:
CREATE INDEX index_name
ON tablename (index_fieldname);
Guide to Oracle 10g 6
Creating Composite Indexes
• Composite index– Multiple sorted columns
– For queries that contain multiple search conditions primary search field
• Secondary search field• Syntax:
CREATE INDEX index_name
ON tablename(index_fieldname1, index_fieldname2, …);
Guide to Oracle 10g 7
Viewing Index Information Using the Data Dictionary Views
• Query data dictionary views – Retrieve information about database objects
• Retrieve index information
Guide to Oracle 10g 8
Querying the USER_INDEXES Data Dictionary View (Partial Output
Shown)
Guide to Oracle 10g 9
Dropping an Index
• Drop index when:– Applications no longer use queries aided by index
– Index does not improve query performance • Enough to justify overhead created on insert,
update, and delete operations
• Syntax:– DROP INDEX index_name;
Guide to Oracle 10g 10
Determining When to Create an Index
• Create index when:– Table contains large number of records
– Field contains wide range of values
– Field contains large number of null values
– Queries frequently use field in search condition or join condition
– Most queries retrieve less than 2% to 4% of table rows
Guide to Oracle 10g 11
Determining When to Create an Index (continued)
• Do not create index when:– Table does not contain large number of records
– Applications do not use proposed index field in query search condition
– Most queries retrieve more than 2% to 4% of table records
– Applications frequently insert or modify table data
• Decision based on judgment and experience
Guide to Oracle 10g 12
Overview of PL/SQL Stored Program Units
• Program unit – Self-contained group of program statements that
can be used within larger program
• Anonymous PL/SQL programs• Stored PL/SQL program units• Server-side program units• Client-side program units
Guide to Oracle 10g 13
Types of Oracle 10g Stored Program Units
Guide to Oracle 10g 14
Creating Stored Program Units
• Procedure– Receive multiple input parameters
– Return multiple output values or return no output values
– Perform action such as inserting, updating, or deleting database records
• Function– Receive multiple input parameters
– Always returns single output value
Guide to Oracle 10g 15
Stored Program Unit Procedures
• CREATE_PROCEDURE command– Header
– Parameter declarations list
– Program unit body
– Exception section
Guide to Oracle 10g 16
Syntax to Create a Stored Program Unit Procedure
Guide to Oracle 10g 17
Creating the Parameter Declarations List
• Defines parameters • Declares associated data types• Parameter mode
– IN
– OUT
– IN OUT
Guide to Oracle 10g 18
Creating a Stored Procedure in SQL*Plus
Guide to Oracle 10g 19
Debugging Stored Program Units in SQL*Plus
• Similar to debugging any program• Identify program line causing error• SQL*Plus interpreter displays error warning
message– Does not automatically display compile error
messages and line locations
– Writes all compile errors to system table • Access using USER_ERRORS data dictionary view
• Execute SHOW ERRORS command
Guide to Oracle 10g 20
Calling a Stored Procedure
• Execute directly from SQL*Plus command line• Create separate PL/SQL program that contains
– Command to call stored procedure– Passes parameter values to procedure
• Calling stored procedure from SQL*Plus command line:EXECUTE procedure_name
(parameter1_value, parameter2_value, ...);
Guide to Oracle 10g 21
Passing Parameters to a Procedure
Guide to Oracle 10g 22
Calling a Stored Procedure (continued)
• Variables passed for each parameter– Must be in same order as parameters appear in
parameter declarations list
• Calling stored procedure from separate PL/SQL program– Similar to calling stored procedure from SQL*Plus
command line
– Omit EXECUTE command– update_enrollment_grade(MA100, 12, B);
Guide to Oracle 10g 23
Creating a Stored Program Unit Function
• Use CREATE OR REPLACE FUNCTION command
• function_return_value_datatype – Defines data type that function returns
• return_value_variable – Declares variable that represents function return
value
• RETURN command
Guide to Oracle 10g 24
Commands to Create a Stored Program Unit Function
Guide to Oracle 10g 25
Calling a Function
• Syntax:variable_name := function_name(parameter1, parameter2, ...);
• Variables passed for parameter values– Must be in same order as parameters appear in
function declaration
Guide to Oracle 10g 26
Using Forms Builder to Create Stored Procedures and Functions
• Create and test program unit within form• Save as stored program unit in database schema• Advantage of using Forms Builder
– Provides enhanced development and debugging environment
– PL/SQL Editor
Guide to Oracle 10g 27
Creating, Testing, and Saving a Stored Program Unit Procedure in
Forms Builder• Create stored procedure in test form• Create form trigger to test program unit procedure• Save program unit as stored procedure in database• Database Objects node
– Contains child nodes that represent every database user
Guide to Oracle 10g 28
Creating, Testing, and Saving a Stored Program Unit Function inForms Builder (continued)
• Create program unit function in Forms Builder• Test program unit function• Save program unit form as stored program unit in
database
Guide to Oracle 10g 29
Lesson A Summary
• Database table index• ROWID• Stored program units
– Procedure
– Function
• Use Forms Builder PL/SQL Editor to develop stored program units
Guide to Oracle 10g 30
Lesson B Objectives
After completing this lesson, you should be able to:• Call stored procedures from other stored
procedures and pass parameter values• Create libraries• Create packages• Create database triggers
Guide to Oracle 10g 31
Calling Stored Program Units From Other Stored Program Units
• Decompose applications into logical units of work– Write individual program units for each logical unit
– Code is in single location
• Call stored program unit from another stored program unit– Execute PL/SQL command that specifies
• Name of called program unit
• Parameter value list
Guide to Oracle 10g 32
Calling Stored Program Units From Other Stored Program Units
(continued)• Must create called program unit before calling
program unit– Otherwise compile error
Guide to Oracle 10g 33
PL/SQL Libraries
• Operating system file • Contains code for multiple related procedures and
functions• Attach to:
– Form
– Report
– Another library
• Stored in client workstation
Guide to Oracle 10g 34
PL/SQL Libraries (continued)
• Extensions:– .pll
– .plx
• Advantage of using library over multiple stored program units:– Library places commands for multiple related
program units in single location
– Always executes on client workstation
Guide to Oracle 10g 35
Creating a PL/SQL Library
• Use Forms Builder to:– Create libraries
– Add form program units and stored program units to library
• Save library .pll file in file system of workstation or shared network drive
• Click Compile Module button– Generate .plx file
Guide to Oracle 10g 36
Attaching a Library to a Form
• Select form’s Attached Libraries node– Click Create button
– Specify PL/SQL library .pll filename
• Message appears to store folder path information for library– Include path information for library
• Library .pll file must always be available at specified folder path and filename
– Otherwise store in default form folder
Guide to Oracle 10g 37
Modifying Library Program Units
• When program unit within library requires maintenance:– Open library in Forms Builder
– Modify program unit as necessary
– Compile program unit within library
– Recompile library to create new .plx file
– Forms that contain library as attached library object then use modified code
Guide to Oracle 10g 38
Packages
• Code library containing related program units and variables
• Stored in database • Executes on database server• Grant other users privilege to use package
– Any PL/SQL program can reference package procedures and functions
• More functionality than PL/SQL libraries• More convenient to use than PL/SQL libraries
Guide to Oracle 10g 39
The Package Specification
• Also called package header• Declares public package objects, including:
– Variables – Cursors– Procedures– Functions
• Made public– Program units outside package can reference
package’s objects
Guide to Oracle 10g 40
The Package Specification (continued)
• Public variables– Visible to many different PL/SQL programs
– Values remain in memory even after programs that declare and reference them terminate
– Declare public variable in DECLARE section of package
• Same syntax used to declare private variable
Guide to Oracle 10g 41
General Syntax for a Package Specification
Guide to Oracle 10g 42
The Package Specification (continued)
• Declare variables and cursors in packages– Same syntax used in other PL/SQL programs
• Declare procedure syntax:PROCEDURE procedure_name
(parameter1 parameter1_data_type,
parameter2 parameter2_data_type, ...);
Guide to Oracle 10g 43
The Package Specification (continued)
• Declare function syntax:FUNCTION function_name
(parameter1 parameter1_data_type,
parameter2 parameter2_data_type, ...)
RETURN return_datatype;
Guide to Oracle 10g 44
The Package Body
• Contains commands to create program units that package specification declares
• Must create specification before body• Optional• Variables declared at beginning of package body
– Private to package
• Each program unit in package specification must be defined
Guide to Oracle 10g 45
General Syntax for a Package Body
Guide to Oracle 10g 46
The Package Body (continued)
• Create package using SQL*Plus• Create package specification in SQL*Plus• Create package body in SQL*Plus• Reference package objects syntax:
– package_name.item_name
Guide to Oracle 10g 47
The Package Body (continued)
• Package exists in user schema– Must grant permission to others– GRANT EXECUTE ON package_name TO username;
Guide to Oracle 10g 48
Creating a Package in Forms Builder
• Create package specification in Forms Builder• Create package body in Forms Builder• Save package in database
Guide to Oracle 10g 49
Database Triggers
• Program units – Execute in response to database events of inserting,
updating, or deleting record
• Different from form triggers• Useful for maintaining integrity constraints• Similar to other program units
– But cannot accept parameters
Guide to Oracle 10g 50
Database Trigger Properties
• Trigger timing– BEFORE
– AFTER
• Trigger statement– INSERT
– UPDATE
– DELETE
Guide to Oracle 10g 51
Database Trigger Properties (continued)
• Trigger level– Statement-level
– Row-level
• Reference value of field in current record – Both before and after triggering statement executes
•:OLD.fieldname•:NEW.fieldname
Guide to Oracle 10g 52
Creating Database Triggers
• Database trigger header• Trigger body
Guide to Oracle 10g 53
General Syntax to Create a Trigger in SQL*Plus
Guide to Oracle 10g 54
Creating Database Triggers to Leave an Audit Trail for the Northwoods University ENROLLMENT Table
• Track when users insert, update, and delete table records
• Create trigger that leaves audit trail– Create one or more tables to store audit trail values
• Create database trigger in SQL*Plus• Create database trigger in Forms Builder
Guide to Oracle 10g 55
Database Trigger Dialog Box
Guide to Oracle 10g 56
Disabling and Dropping Triggers
• Drop trigger when not needed:– DROP TRIGGER trigger_name;
• Disable trigger– Trigger still exists in user’s database schema
– No longer fires when triggering event occurs
– Syntax:•ALTER TRIGGER trigger_name [ENABLE | DISABLE];
Guide to Oracle 10g 57
Viewing Information About Triggers
• USER_TRIGGERS data dictionary view– Contains information about triggers
Guide to Oracle 10g 58
Lesson B Summary
• PL/SQL library• Package• Database trigger
– Activated when table modified
Recommended