Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


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