chapter09

58
Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10g

Upload: rajesh-mandava

Post on 22-May-2015

277 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Chapter09

Chapter 9: Advanced SQL and PL/SQL Topics

Guide to Oracle 10g

CE
Global to this PPT: Please note that most figure slides have figure caption as slide title, not heading - okay? (The individual slides are commented.)
Page 2: Chapter09

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

Page 3: Chapter09

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

Page 4: Chapter09

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

Page 5: Chapter09

Guide to Oracle 10g 5

Creating an Index

• Create after adding data• Syntax:

CREATE INDEX index_name

ON tablename (index_fieldname);

Page 6: Chapter09

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, …);

Page 7: Chapter09

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

Page 8: Chapter09

Guide to Oracle 10g 8

Querying the USER_INDEXES Data Dictionary View (Partial Output

Shown)

CE
Slide title is figure caption, not heading - okay?
Page 9: Chapter09

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;

Page 10: Chapter09

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

Page 11: Chapter09

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

Page 12: Chapter09

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

Page 13: Chapter09

Guide to Oracle 10g 13

Types of Oracle 10g Stored Program Units

CE
Slide title is figure caption, not heading - okay?
Page 14: Chapter09

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

Page 15: Chapter09

Guide to Oracle 10g 15

Stored Program Unit Procedures

• CREATE_PROCEDURE command– Header

– Parameter declarations list

– Program unit body

– Exception section

Page 16: Chapter09

Guide to Oracle 10g 16

Syntax to Create a Stored Program Unit Procedure

CE
Slide title is figure caption, not heading - okay?
Page 17: Chapter09

Guide to Oracle 10g 17

Creating the Parameter Declarations List

• Defines parameters • Declares associated data types• Parameter mode

– IN

– OUT

– IN OUT

Page 18: Chapter09

Guide to Oracle 10g 18

Creating a Stored Procedure in SQL*Plus

CE
Please note that the text in this figure may be hard to read.
Page 19: Chapter09

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

Page 20: Chapter09

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, ...);

Page 21: Chapter09

Guide to Oracle 10g 21

Passing Parameters to a Procedure

CE
Slide title is figure caption, not heading - okay?Please note that this seems odd since it comes between two slides with the same title/heading (slides 20 and 22).
Page 22: Chapter09

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);

Page 23: Chapter09

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

Page 24: Chapter09

Guide to Oracle 10g 24

Commands to Create a Stored Program Unit Function

CE
Slide title is figure caption, not heading - okay?
Page 25: Chapter09

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

Page 26: Chapter09

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

Page 27: Chapter09

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

Page 28: Chapter09

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

Page 29: Chapter09

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

Page 30: Chapter09

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

Page 31: Chapter09

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

Page 32: Chapter09

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

Page 33: Chapter09

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

Page 34: Chapter09

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

Page 35: Chapter09

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

Page 36: Chapter09

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

Page 37: Chapter09

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

Page 38: Chapter09

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

Page 39: Chapter09

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

Page 40: Chapter09

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

Page 41: Chapter09

Guide to Oracle 10g 41

General Syntax for a Package Specification

CE
Slide title is figure caption, not heading - okay?Please note that this seems odd since it comes between two slides with the same title/heading (slides 40 and 42).
Page 42: Chapter09

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, ...);

Page 43: Chapter09

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;

Page 44: Chapter09

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

Page 45: Chapter09

Guide to Oracle 10g 45

General Syntax for a Package Body

CE
Slide title is figure caption, not heading - okay?Please note that this seems odd since it comes between two slides with the same title/heading (slides 44 and 46).
Page 46: Chapter09

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

Page 47: Chapter09

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;

Page 48: Chapter09

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

Page 49: Chapter09

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

Page 50: Chapter09

Guide to Oracle 10g 50

Database Trigger Properties

• Trigger timing– BEFORE

– AFTER

• Trigger statement– INSERT

– UPDATE

– DELETE

Page 51: Chapter09

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

Page 52: Chapter09

Guide to Oracle 10g 52

Creating Database Triggers

• Database trigger header• Trigger body

Page 53: Chapter09

Guide to Oracle 10g 53

General Syntax to Create a Trigger in SQL*Plus

CE
Slide title is figure caption, not heading - okay?
Page 54: Chapter09

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

Page 55: Chapter09

Guide to Oracle 10g 55

Database Trigger Dialog Box

CE
Slide title is figure caption, not heading - okay?Please note that it may be hard to read the text in this figure.
Page 56: Chapter09

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];

Page 57: Chapter09

Guide to Oracle 10g 57

Viewing Information About Triggers

• USER_TRIGGERS data dictionary view– Contains information about triggers

Page 58: Chapter09

Guide to Oracle 10g 58

Lesson B Summary

• PL/SQL library• Package• Database trigger

– Activated when table modified