Db Triggers05ch


Published on

Test the Slide Share

Published in: Technology, Business
  • 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

Db Triggers05ch

  1. 1. Enhanced Guide to Oracle8i Chapter 5: Advanced PL/SQL Programming
  2. 2. Anonymous PL/SQL Programs <ul><li>Write code in text editor, execute it in SQL*Plus </li></ul><ul><li>Code can be stored as text in file system </li></ul><ul><li>Program cannot be called by other programs, or executed by other users </li></ul><ul><li>Cannot accept or pass parameter values </li></ul>
  3. 3. Named PL/SQL Programs <ul><li>Can be created: </li></ul><ul><ul><li>Using text editor & executed in SQL*Plus </li></ul></ul><ul><ul><li>Using Procedure Builder </li></ul></ul><ul><li>Can be stored: </li></ul><ul><ul><li>As compiled objects in database </li></ul></ul><ul><ul><li>As source code libraries in file system </li></ul></ul><ul><li>Can be called by other programs </li></ul><ul><li>Can be executed by other users </li></ul>
  4. 4. Named Program Locations <ul><li>Server-side </li></ul><ul><ul><li>Stored in database as database objects </li></ul></ul><ul><ul><li>Execute on the database server </li></ul></ul><ul><li>Client-side </li></ul><ul><ul><li>Stored in the client workstation file system </li></ul></ul><ul><ul><li>Execute on the client workstation </li></ul></ul>
  5. 5. Named Program Types <ul><li>Program Units (client or server-side) </li></ul><ul><ul><li>Procedures </li></ul></ul><ul><ul><li>Functions </li></ul></ul><ul><li>Libraries (client-side) </li></ul><ul><li>Packages (client or server-side) </li></ul><ul><li>Triggers (server-side) </li></ul>
  6. 6. Program Units <ul><li>Procedures </li></ul><ul><ul><li>Can receive and pass multiple parameter values </li></ul></ul><ul><ul><li>Can call other program units </li></ul></ul><ul><li>Functions </li></ul><ul><ul><li>Like procedures, except they return a single value </li></ul></ul>
  7. 7. <ul><li>Variables used to pass data values in/out of program units </li></ul><ul><li>Declared in the procedure header </li></ul><ul><li>Parameter values are passed when the procedure is called from the calling program </li></ul>Parameters
  8. 8. Parameter Modes <ul><li>IN </li></ul><ul><ul><li>Incoming values, read-only </li></ul></ul><ul><li>OUT </li></ul><ul><ul><li>Outgoing values, write-only </li></ul></ul><ul><li>IN OUT </li></ul><ul><ul><li>Can be both incoming and outgoing </li></ul></ul>
  9. 9. Creating a Procedure <ul><li>CREATE OR REPLACE PROCEDURE procedure_name </li></ul><ul><li>( parameter1 mode datatype, </li></ul><ul><li>parameter2 mode datatype , … </li></ul><ul><li>) IS | AS </li></ul><ul><li>local variable declarations </li></ul><ul><li>BEGIN </li></ul><ul><li>program statements </li></ul><ul><li>EXCEPTION </li></ul><ul><li>exception handlers </li></ul><ul><li>END; </li></ul>header body
  10. 10. Executing a Procedure <ul><li>EXECUTE procedure_name </li></ul><ul><li>( parameter1_value , parameter2_value , …); </li></ul>
  11. 11. <ul><li>Formal parameters: declared in procedure header </li></ul><ul><li>Actual parameters: values placed in parameter list when procedure is called </li></ul><ul><li>Values correspond based on order </li></ul>Parameter Types Procedure Header: PROCEDURE calc_gpa (s_id IN NUMBER, term_id IN NUMBER, gpa OUT NUMBER); Procedure Call: calc_gpa (current_s_id, 4, current_gpa); Formal Parameters Actual Parameters
  12. 12. Debugging Named Program Units in SQL*Plus <ul><li>Isolate program lines causing errors and then fix them </li></ul><ul><li>Use SHOW ERRORS command to view compile error details </li></ul><ul><li>Use DBMS_OUTPUT.PUT_LINE commands to view intermediate values and track program flow </li></ul>
  13. 13. Creating a Function <ul><li>CREATE OR REPLACE FUNCTION function_name </li></ul><ul><li>( parameter1 mode datatype, </li></ul><ul><li>parameter2 mode datatype , … </li></ul><ul><li>) </li></ul><ul><li>RETURN function_return_data_type </li></ul><ul><li>IS local variable declarations </li></ul><ul><li>BEGIN </li></ul><ul><li>program statements </li></ul><ul><li>RETURN return_value ; </li></ul><ul><li>EXCEPTION </li></ul><ul><li>exception handlers </li></ul><ul><li> RETURN EXCEPTION_NOTICE; </li></ul><ul><li>END; </li></ul>header body
  14. 14. Function Syntax Details <ul><li>RETURN command in header specifies data type of value the function will return </li></ul><ul><li>RETURN command in body specifies actual value returned by function </li></ul><ul><li>RETURN EXCEPTION_NOTICE instructs the function to display the except notice in the calling program </li></ul>
  15. 15. Calling a Function <ul><li>Can be called from either named or anonymous PL/SQL blocks </li></ul><ul><li>Can be called within SQL queries </li></ul><ul><li>return_value := </li></ul><ul><li>function_name ( parameter1_value, parameter2_value , …); </li></ul>
  16. 16. Stored Program Unit Object Privileges <ul><li>Stored program units exist as objects in your database schema </li></ul><ul><li>To allow other users to use your units, you must grant them EXECUTE privileges </li></ul><ul><li>GRANT EXECUTE ON unit_name TO username ; </li></ul>
  17. 17. Using Stored Program Units That Belong to Other Users <ul><li>You must have been granted the privilege to use it </li></ul><ul><li>You must preface the unit name with the owner’s username </li></ul><ul><li>return_value </li></ul><ul><li>:= LHOWARD.my_function </li></ul><ul><li>TO_DATE(’07/14/1958’, ‘MM/DD/YYYY’); </li></ul>
  18. 18. Using Procedure Builder to Create Named Program Units <ul><li>Procedures and functions created in Notepad/SQL*Plus are always server-side </li></ul><ul><ul><li>Stored in the database </li></ul></ul><ul><ul><li>Executed on the database server </li></ul></ul><ul><li>Procedure Builder can be used to create client-side libraries of procedures and functions </li></ul><ul><ul><li>Stored in the client file system </li></ul></ul><ul><ul><li>Executed on the client </li></ul></ul>
  19. 19. Procedure Builder Client-Side File Types <ul><li>.pls </li></ul><ul><ul><li>Single program unit </li></ul></ul><ul><ul><li>Uncompiled source code </li></ul></ul><ul><ul><li>Can only be opened/modified in Procedure Builder </li></ul></ul><ul><li>.pll </li></ul><ul><ul><li>Library of procedures or functions </li></ul></ul><ul><ul><li>Compiled code </li></ul></ul><ul><ul><li>Can be referenced in other Developer applications (Forms, Reports) </li></ul></ul>
  20. 20. Procedure Builder Interface Object Navigator Window PL/SQL Interpreter Window command prompt pane source code pane
  21. 21. Program Unit Editor Interface Button bar Procedure list Procedure template Status line Source code pane
  22. 22. Creating Client-Side Objects in Procedure Builder <ul><li>Client-side program unit source code </li></ul><ul><ul><li>Create program unit in Program Unit Editor, export text to .pls file </li></ul></ul><ul><li>Client-side library </li></ul><ul><ul><li>Click File, click Save As, and specify to save library .pll file in file system </li></ul></ul>
  23. 23. Executing a Procedure in Procedure Builder <ul><li>Load program unit as a top-level Program Unit object </li></ul><ul><li>Type the procedure name and parameter list values at the command prompt </li></ul>
  24. 24. <ul><li>Set a breakpoint on the program line where execution will pause </li></ul><ul><li>Single-step through the program lines and examine current variable values </li></ul><ul><ul><li>Global variables </li></ul></ul><ul><ul><li>Stack (local) variables </li></ul></ul><ul><li>View program execution path </li></ul>Using the PL/SQL Interpreter to Find Runtime Errors
  25. 25. Setting a Breakpoint <ul><li>Load program unit in PL/SQL Interpreter window </li></ul><ul><li>Double-click line to create breakpoint </li></ul>Breakpoint
  26. 26. Viewing Program Variable Values During Execution Variable values Execution arrow
  27. 27. Strategy For Using the PL/SQL Interpreter Debugger <ul><li>Run the program, and determine which line is causing the run-time error </li></ul><ul><li>Run the program again, and examine variable values just before the error occurs to determine its cause </li></ul>
  28. 28. Calling Procedures From Other Procedures <ul><li>Use procedure name followed by parameter list </li></ul><ul><ul><li>procedure_name </li></ul></ul><ul><ul><li>( parameter1_value , parameter2_value , …); </li></ul></ul>
  29. 29. Creating Server-Side Objects in Procedure Builder <ul><li>Stored program units </li></ul><ul><ul><li>Drag program unit from top-level node in Procedure Builder to Program Units node under Database node </li></ul></ul><ul><li>Libraries </li></ul><ul><ul><li>Click File, click Save As, and specify to save library in database </li></ul></ul><ul><ul><li>Regardless of storage location, PL/SQL libraries ALWAYS execute on client </li></ul></ul>
  30. 30. Program Unit Dependencies <ul><li>Object dependencies </li></ul><ul><ul><li>Program units are dependent on the database objects they reference (tables, views, sequences, …) </li></ul></ul><ul><li>Procedure dependencies </li></ul><ul><ul><li>Program units are dependent on other program units they call </li></ul></ul>
  31. 31. Direct and Indirect Dependencies <ul><li>Direct dependency </li></ul><ul><ul><li>Object or program is directly called or referenced </li></ul></ul><ul><li>Indirect dependency </li></ul><ul><ul><li>Object or program is called or referenced by a subprogram </li></ul></ul>
  32. 32. Direct and Indirect Dependencies Direct Dependency Indirect Dependency CREATE_ NEW_ ORDER CUST_ORDER CREATE_ NEW_ ORDER_LINE ORDER_LINE ORDER_ID_ SEQUENCE ORDER_ID_ SEQUENCE
  33. 33. Invalidation <ul><li>If an object or program on which a program has a dependency is changed, the program is invalidated , and must be recompiled </li></ul>
  34. 34. Packages <ul><li>Server-side code library </li></ul><ul><li>Can contain: </li></ul><ul><ul><li>Global variable declarations </li></ul></ul><ul><ul><li>Cursors </li></ul></ul><ul><ul><li>Procedures </li></ul></ul><ul><ul><li>Functions </li></ul></ul>
  35. 35. Differences Between Packages and Libraries <ul><li>Libraries have to be explicitly attached to applications, while packages are always available to be called by applications </li></ul><ul><li>Libraries always execute on client </li></ul><ul><li>Packages always execute on server </li></ul>
  36. 36. Package Components <ul><li>Specification </li></ul><ul><ul><li>Used to declare all public variables, cursors, procedures, functions </li></ul></ul><ul><li>Body </li></ul><ul><ul><li>Contains underlying code for procedures and functions </li></ul></ul><ul><li>Rationale: </li></ul><ul><ul><li>Specification is visible to users, gives details on how to use </li></ul></ul><ul><ul><li>Body is not visible, users don’t care about details </li></ul></ul>
  37. 37. Creating a Package Specification in SQL*Plus CREATE OR REPLACE PACKAGE package_name IS --public variables variable_name datatype ; --program units PROCEDURE procedure_name ( parameter_list ); FUNCTION function_name ( parameter_list ); END;
  38. 38. Creating a Package Specification in SQL*Plus
  39. 39. Creating a Package Body in SQL*Plus CREATE OR REPLACE PACKAGE BODY package_name IS private variable declarations program unit blocks END;
  40. 40. Creating a Package Body in SQL*Plus
  41. 41. Calling a Program Unit That Is In a Package <ul><li>Preface the program unit name with the package name </li></ul><ul><li>PACKAGE_NAME . program_unit_name ( parameter_list ); </li></ul><ul><li>Example: </li></ul><ul><li>DBMS_OUTPUT.PUT_LINE(‘Hello World’); </li></ul>
  42. 42. Overloading Program Units in Packages <ul><li>Overloading </li></ul><ul><ul><li>Multiple program units have the same name, but accept different input parameters </li></ul></ul><ul><li>Allows user to use the same command to perform an action but pass different parameter values </li></ul>
  43. 43. Package Specification With Overloaded Procedures
  44. 44. Saving Packages as Database Objects <ul><li>Expand the Database Objects node so your username appears, and expand your username so the Stored Program Units node appears </li></ul><ul><li>Drag the Package Specification and Package Body under the Stored Program Units node </li></ul>
  45. 45. Database Triggers <ul><li>Program units that are attached to a specific table </li></ul><ul><li>Execute in response to the following table operations: </li></ul><ul><ul><li>INSERT </li></ul></ul><ul><ul><li>UPDATE </li></ul></ul><ul><ul><li>DELETE </li></ul></ul>
  46. 46. Uses For Database Triggers <ul><li>Force related operations to always happen </li></ul><ul><ul><li>Sell an item, update QOH </li></ul></ul><ul><li>Create a table that serves as an audit trail </li></ul><ul><ul><li>Record who changes a student grade and when they change it </li></ul></ul>
  47. 47. Creating Database Triggers <ul><li>Code is similar to all PL/SQL program unit blocks </li></ul><ul><li>Database triggers cannot accept parameters </li></ul>
  48. 48. Defining Triggers <ul><li>To define a trigger, you must specify: </li></ul><ul><ul><li>Statement type that causes trigger to fire </li></ul></ul><ul><ul><ul><li>INSERT, UPDATE, DELETE </li></ul></ul></ul><ul><ul><li>Timing </li></ul></ul><ul><ul><ul><li>BEFORE or AFTER </li></ul></ul></ul><ul><ul><li>Level </li></ul></ul><ul><ul><ul><li>STATEMENT or ROW </li></ul></ul></ul>
  49. 49. Trigger Timing <ul><li>BEFORE: trigger fires before statement executes </li></ul><ul><ul><li>Example: for audit trail, records grade value before it is updated </li></ul></ul><ul><li>AFTER: trigger fires after statement executes </li></ul><ul><ul><li>Example: update QOH after item is sold </li></ul></ul>
  50. 50. Trigger Levels <ul><li>ROW: trigger fires once for each row that is affected </li></ul><ul><ul><li>Example: when adding multiple order lines, update multiple inventory QOH values </li></ul></ul><ul><li>STATEMENT: trigger fires once, regardless of how many rows are updated </li></ul><ul><ul><li>Example: for audit trail, you just want to record that someone updated a table, but you don’t care how many rows were updated </li></ul></ul>
  51. 51. Creating a Trigger in SQL*Plus <ul><li>CREATE OR REPLACE TRIGGER trigger_name </li></ul><ul><li>[BEFORE|AFTER] [INSERT|UPDATE|DELETE] ON </li></ul><ul><li>table_name </li></ul><ul><li>[FOR EACH ROW] [WHEN (condition)] </li></ul><ul><li>BEGIN </li></ul><ul><li>trigger body </li></ul><ul><li>END; </li></ul>
  52. 52. Creating a Statement-Level Trigger in SQL*Plus
  53. 53. Trigger Restrictions <ul><li>You can only create triggers on tables that you own </li></ul><ul><li>You must have the CREATE TRIGGER system privilege </li></ul><ul><li>You cannot execute a COMMIT command in a trigger </li></ul>
  54. 54. Row-Level Trigger Syntax <ul><li>WHEN (condition): </li></ul><ul><ul><li>Optional </li></ul></ul><ul><ul><li>Specifies to fire only when a row satisfies a certain search condition </li></ul></ul><ul><li>Referencing old and new values in the trigger body: </li></ul><ul><ul><li>:OLD. field_name </li></ul></ul><ul><ul><li>:NEW. field_name </li></ul></ul>
  55. 55. Creating a Row-Level Trigger in SQL*Plus
  56. 56. Creating a Trigger in Procedure Builder <ul><li>In the Object Navigator Pane, expand the Database Objects node, expand your username, and select the table to which the trigger will be attached </li></ul><ul><li>Create a new trigger </li></ul><ul><li>Specify the trigger properties </li></ul>
  57. 57. Specifying Trigger Properties in Procedure Builder Timing Statement type Trigger name Field WHEN condition Trigger body Level
  58. 58. INSTEAD-OF Triggers <ul><li>Fire when a user issues a DML command associated with a complex view </li></ul><ul><ul><li>Normally, users cannot insert, update, or delete records associated with complex views that are created by joining multiple tables </li></ul></ul>
  59. 59. Creating an INSTEAD-OF Trigger in SQL*Plus
  60. 60. Disabling and Dropping Triggers <ul><li>Syntax to drop a trigger: </li></ul><ul><li>DROP TRIGGER trigger_name ; </li></ul><ul><li>Syntax to enable or disable a trigger: </li></ul><ul><li>ALTER TRIGGER trigger_name [ENABLE | DISABLE]; </li></ul>
  61. 61. Oracle Built-In Packages <ul><li>Provide support for basic database functions </li></ul><ul><li>Owned by the SYS database schema </li></ul>
  62. 62. Oracle Built-In Package Types <ul><li>Transaction processing </li></ul><ul><li>Application development </li></ul><ul><li>Database and application administration </li></ul><ul><li>Internal support </li></ul>
  63. 63. Transaction Processing Packages <ul><li>Provide procedures to support transaction processing </li></ul><ul><ul><li>DBMS_ALERT: dynamically sends messages to other database sessions </li></ul></ul><ul><ul><li>DBMS_LOCK: creates user-defined locks on tables and records </li></ul></ul><ul><ul><li>DBMS_SQL: implements Dynamic SQL </li></ul></ul><ul><ul><li>DBMS_TRANSACTION: provides procedures for transaction management </li></ul></ul>
  64. 64. Application Development Packages <ul><li>Aid developers in creating and debugging PL/SQL applications </li></ul><ul><ul><li>DBMS_DESCRIBE: returns information about the parameters of any stored program unit </li></ul></ul><ul><ul><li>DBMS_JOB: schedules PL/SQL named programs to run at specific times </li></ul></ul><ul><ul><li>DBMS_OUTPUT: provides text output in PL/SQL programs in SQL*Plus </li></ul></ul><ul><ul><li>DBMS_PIPE: sends messages to other database sessions </li></ul></ul><ul><ul><li>DBMS_SESSION: dynamically changes the properties of a database session </li></ul></ul><ul><ul><li>UTL_FILE: enables PL/SQL output to be written to a binary file </li></ul></ul>
  65. 65. DBMS_JOB Package <ul><li>Enables PL/SQL named programs to be run at certain times </li></ul><ul><li>Creates a job queue </li></ul><ul><ul><li>List of programs to be run, and times they are to be run </li></ul></ul>
  66. 66. DBMS_PIPE Package <ul><li>Implements database pipes </li></ul><ul><ul><li>Used to transfer information among database sessions independently of transactions </li></ul></ul><ul><li>Sending a message </li></ul><ul><ul><li>Use the PACK_MESSAGE and SEND_MESSAGE procedures </li></ul></ul><ul><li>Receiving a message </li></ul><ul><ul><li>Use the RECEIVE_MESSAGE and UNPACK_MESSAGE procedures </li></ul></ul>
  67. 67. Database and Application Administration Packages <ul><li>Support database administration tasks </li></ul><ul><ul><li>DBMS_APPLICATION_INFO: registers information about programs being run by individual user sessions </li></ul></ul><ul><ul><li>DBMS_DDL: provides procedures for compiling program units and analyzing database objects </li></ul></ul><ul><ul><li>DBMS_SHARED_POOL: used to manage the shared pool </li></ul></ul><ul><ul><li>DBMS_SPACE: provides information for managing how data values are physical stored in the database </li></ul></ul><ul><ul><li>DBMS_UTILITY: provides procedures for compiling all program units and analyzing all objects in a database schema </li></ul></ul>
  68. 68. Internal Support Packages <ul><li>Provide underlying functionality of the Oracle database </li></ul><ul><ul><li>STANDARD: defines all built-in functions and procedures, database data types, and PL/SQL data types </li></ul></ul><ul><ul><li>DBMS_SNAPSHOT: used to manage database snapshots </li></ul></ul><ul><ul><li>DBMS_REFRESH: used to refresh snapshots </li></ul></ul><ul><ul><li>DBMS_STANDARD: contains common functions of the PL/SQL language </li></ul></ul>
  69. 69. Dynamic SQL <ul><li>Allows you to create SQL commands as text strings in PL/SQL programs, and validate the database objects at runtime </li></ul><ul><li>Allows user to specify program inputs </li></ul><ul><li>Supporting procedures are provided in the DBMS_SQL package </li></ul>
  70. 70. Creating Dynamic SQL Programs that Contain DML Commands <ul><li>Open the cursor that defines the memory area where processing is performed </li></ul><ul><li>Define the SQL command as a text string, using placeholders for dynamic values </li></ul><ul><li>Parse the SQL command </li></ul><ul><li>Bind input variables to placeholders </li></ul><ul><li>Execute the SQL command </li></ul><ul><li>Close the cursor </li></ul>
  71. 71. Creating Dynamic SQL Programs that Contain DDL Commands <ul><li>Open the cursor that defines the memory area where processing is performed </li></ul><ul><li>Define the SQL command as a text string </li></ul><ul><ul><li>You cannot define placeholders and dynamically bind values </li></ul></ul><ul><li>Parse the SQL command </li></ul><ul><li>Close the cursor </li></ul>
  72. 72. Creating Dynamic SQL Programs that Contain SELECT Commands <ul><li>Open the cursor </li></ul><ul><li>Define the SQL command as a text string using placeholders for dynamic values </li></ul><ul><li>Parse the SQL command </li></ul><ul><li>Bind input values to placeholders </li></ul><ul><li>Define output variables </li></ul><ul><li>Execute the query </li></ul><ul><li>Fetch the rows </li></ul><ul><li>Associate the fetched rows with the output variables </li></ul><ul><li>Close the cursor </li></ul>
  73. 73. Using Dynamic SQL to Create an Anonymous PL/SQL Block <ul><li>Open the cursor </li></ul><ul><li>Define the SQL command as a text string using placeholders for dynamic values </li></ul><ul><li>Parse the SQL command </li></ul><ul><li>Bind input and output values to placeholders </li></ul><ul><li>Execute the query </li></ul><ul><li>Retrieve values of output variables </li></ul><ul><li>Close the cursor </li></ul>