SlideShare a Scribd company logo
Chapter 9:  Advanced SQL and PL/SQL Topics Guide to Oracle 10 g
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
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
Database Indexes (continued) Index on specific table field ROWID value Sorted indexed field value Oracle 10 g  automatically creates index on table’s primary key Create indexes on columns that users often use in search conditions
Creating an Index Create after adding data Syntax: CREATE INDEX  index_name ON  tablename  ( index_fieldname );
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 , …);
Viewing Index Information Using the Data Dictionary Views Query data dictionary views  Retrieve information about database objects Retrieve index information
Querying the USER_INDEXES Data Dictionary View (Partial Output Shown)
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 ;
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
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
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
Types of Oracle 10 g  Stored Program Units
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
Stored Program Unit Procedures CREATE_PROCEDURE command Header Parameter declarations list Program unit body Exception section
Syntax to Create a Stored Program Unit Procedure
Creating the Parameter Declarations List Defines parameters  Declares associated data types Parameter mode IN OUT IN OUT
Creating a Stored Procedure in SQL*Plus
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
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 , ...);
Passing Parameters to a Procedure
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);
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
Commands to Create a Stored Program Unit Function
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
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
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
Creating, Testing, and Saving a Stored Program Unit Function in Forms Builder (continued) Create program unit function in Forms Builder Test program unit function Save program unit form as stored program unit in database
Lesson A Summary Database table index ROWID Stored program units Procedure Function Use Forms Builder PL/SQL Editor to develop stored program units
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
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
Calling Stored Program Units From Other Stored Program Units (continued) Must create called program unit before calling program unit Otherwise compile error
PL/SQL Libraries Operating system file  Contains code for multiple related procedures and functions Attach to:  Form  Report Another library Stored in client workstation
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
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
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
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
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
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
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
General Syntax for a Package Specification
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 , ...);
The Package Specification (continued) Declare function syntax: FUNCTION  function_name ( parameter1   parameter1_data_type , parameter2   parameter2_data_type , ...) RETURN  return_datatype ;
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
General Syntax for a Package Body
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
The Package Body (continued) Package exists in user schema Must grant permission to others GRANT EXECUTE ON  package_name  TO  username ;
Creating a Package in Forms Builder Create package specification in Forms Builder Create package body in Forms Builder Save package in database
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
Database Trigger Properties Trigger timing BEFORE AFTER Trigger statement INSERT UPDATE DELETE
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
Creating Database Triggers Database trigger header Trigger body
General Syntax to Create a Trigger in SQL*Plus
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
Database Trigger Dialog Box
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];
Viewing Information About Triggers USER_TRIGGERS data dictionary view Contains information about triggers
Lesson B Summary PL/SQL library Package Database trigger Activated when table modified

More Related Content

PPT
R12 d49656 gc10-apps dba 05
PDF
Oracle Concurrent Program Setup document
PPT
R12 d49656 gc10-apps dba 11
PPT
R12 d49656 gc10-apps dba 09
PPT
R12 d49656 gc10-apps dba 12
PDF
EBS R12 Concurrent program tracing
PPT
R12 d49656 gc10-apps dba 14
PPT
R12 d49656 gc10-apps dba 19
R12 d49656 gc10-apps dba 05
Oracle Concurrent Program Setup document
R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 09
R12 d49656 gc10-apps dba 12
EBS R12 Concurrent program tracing
R12 d49656 gc10-apps dba 14
R12 d49656 gc10-apps dba 19

What's hot (20)

PDF
Basic Oracle Usage v1
PPT
Oracle D2K reports
PPT
R12 d49656 gc10-apps dba 20
DOC
Oracle report from ppt
PDF
Oracle SQL Part 3
PPT
R12 d49656 gc10-apps dba 08
PDF
Altering Drop-Down Menus for Admissions IT Service Request Form
PPT
R12 d49656 gc10-apps dba 25
PDF
Oracle9i reports developer
PDF
Oracle Form material
DOCX
D2 k word_format
PDF
Oracle SQL Part 2
PPT
Maximizing SAP ABAP Performance
PDF
Oracle forms developer 10g vol1
PPT
Rational Publishing Engine and Rational System Architect
PPS
Auditing DB2 on z/VM and z/VSE
PDF
Add on packages
PDF
PPT
Abap
PPT
Reports 6i
Basic Oracle Usage v1
Oracle D2K reports
R12 d49656 gc10-apps dba 20
Oracle report from ppt
Oracle SQL Part 3
R12 d49656 gc10-apps dba 08
Altering Drop-Down Menus for Admissions IT Service Request Form
R12 d49656 gc10-apps dba 25
Oracle9i reports developer
Oracle Form material
D2 k word_format
Oracle SQL Part 2
Maximizing SAP ABAP Performance
Oracle forms developer 10g vol1
Rational Publishing Engine and Rational System Architect
Auditing DB2 on z/VM and z/VSE
Add on packages
Abap
Reports 6i
Ad

Similar to Chapter09 (20)

PPT
Db Triggers05ch
PPT
R12 d49656 gc10-apps dba 16
PPT
Test automation process _ QTP
PPT
Test automation process
PPT
R12 d49656 gc10-apps dba 26
PPT
Store programs
PPSX
Automation Framework 042009 V2
PPT
Intro to tsql
PPT
Intro to tsql unit 14
PPT
Ten Steps To Empowerment
PPT
R12 d49656 gc10-apps dba 03
PPT
R12 d49656 gc10-apps dba 07
PPS
Procedures/functions of rdbms
PDF
Oracle DBA interview_questions
PPT
Getting Started with Zend Framework
PDF
Ch07.pdf
PDF
.NET Portfolio
PPT
Db Triggers05ch
R12 d49656 gc10-apps dba 16
Test automation process _ QTP
Test automation process
R12 d49656 gc10-apps dba 26
Store programs
Automation Framework 042009 V2
Intro to tsql
Intro to tsql unit 14
Ten Steps To Empowerment
R12 d49656 gc10-apps dba 03
R12 d49656 gc10-apps dba 07
Procedures/functions of rdbms
Oracle DBA interview_questions
Getting Started with Zend Framework
Ch07.pdf
.NET Portfolio
Ad

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
RMMM.pdf make it easy to upload and study
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Basic Mud Logging Guide for educational purpose
Week 4 Term 3 Study Techniques revisited.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
RMMM.pdf make it easy to upload and study
human mycosis Human fungal infections are called human mycosis..pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Anesthesia in Laparoscopic Surgery in India
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Complications of Minimal Access Surgery at WLH
Microbial disease of the cardiovascular and lymphatic systems
Microbial diseases, their pathogenesis and prophylaxis
Insiders guide to clinical Medicine.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Supply Chain Operations Speaking Notes -ICLT Program
TR - Agricultural Crops Production NC III.pdf

Chapter09

  • 1. Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10 g
  • 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
  • 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
  • 4. Database Indexes (continued) Index on specific table field ROWID value Sorted indexed field value Oracle 10 g automatically creates index on table’s primary key Create indexes on columns that users often use in search conditions
  • 5. Creating an Index Create after adding data Syntax: CREATE INDEX index_name ON tablename ( index_fieldname );
  • 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 , …);
  • 7. Viewing Index Information Using the Data Dictionary Views Query data dictionary views Retrieve information about database objects Retrieve index information
  • 8. Querying the USER_INDEXES Data Dictionary View (Partial Output Shown)
  • 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 ;
  • 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
  • 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
  • 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
  • 13. Types of Oracle 10 g Stored Program Units
  • 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
  • 15. Stored Program Unit Procedures CREATE_PROCEDURE command Header Parameter declarations list Program unit body Exception section
  • 16. Syntax to Create a Stored Program Unit Procedure
  • 17. Creating the Parameter Declarations List Defines parameters Declares associated data types Parameter mode IN OUT IN OUT
  • 18. Creating a Stored Procedure in SQL*Plus
  • 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
  • 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 , ...);
  • 21. Passing Parameters to a Procedure
  • 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);
  • 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
  • 24. Commands to Create a Stored Program Unit Function
  • 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
  • 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
  • 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
  • 28. Creating, Testing, and Saving a Stored Program Unit Function in Forms Builder (continued) Create program unit function in Forms Builder Test program unit function Save program unit form as stored program unit in database
  • 29. Lesson A Summary Database table index ROWID Stored program units Procedure Function Use Forms Builder PL/SQL Editor to develop stored program units
  • 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
  • 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
  • 32. Calling Stored Program Units From Other Stored Program Units (continued) Must create called program unit before calling program unit Otherwise compile error
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 41. General Syntax for a Package Specification
  • 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 , ...);
  • 43. The Package Specification (continued) Declare function syntax: FUNCTION function_name ( parameter1 parameter1_data_type , parameter2 parameter2_data_type , ...) RETURN return_datatype ;
  • 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
  • 45. General Syntax for a Package Body
  • 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
  • 47. The Package Body (continued) Package exists in user schema Must grant permission to others GRANT EXECUTE ON package_name TO username ;
  • 48. Creating a Package in Forms Builder Create package specification in Forms Builder Create package body in Forms Builder Save package in database
  • 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
  • 50. Database Trigger Properties Trigger timing BEFORE AFTER Trigger statement INSERT UPDATE DELETE
  • 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
  • 52. Creating Database Triggers Database trigger header Trigger body
  • 53. General Syntax to Create a Trigger in SQL*Plus
  • 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
  • 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];
  • 57. Viewing Information About Triggers USER_TRIGGERS data dictionary view Contains information about triggers
  • 58. Lesson B Summary PL/SQL library Package Database trigger Activated when table modified