Oracle® Database Advanced Application Developer's Guide 11g Release 2 (11.2) E25518-06 January 2013 Oracle Database Advanced Application Developer's Guide, 11g Release 2 (11.2) E25518-06 Copyright © 1996, 2013, Oracle and/or its affiliates. All rights reserved. Primary Author: Sheila Moore Contributing Authors: D. Adams, L. Ashdown, M. Cowan, T. Kyte, J. Melnick, R. Moran, E. Paapanen, J. Russell, R. Strohm, R. Ward Contributors: D. Alpern, G. Arora, C. Barclay, D. Bronnikov, T. Chang, L. Chen, B. Cheng, M. Davidson, R. Day, R. Decker, G. Doherty, D. Elson, A. Ganesh, M. Hartstein, Y. Hu, J. Huang, C. Iyer, N. Jain, R. Jenkins Jr., S. Kotsovolos, V. Krishnaswamy, S. Kumar, C. Lei, B. Llewellyn, D. Lorentz, V. Moore, K. Muthukkaruppan, V. Moore, J. Muller, R. Murthy, R. Pang, B. Sinha, S. Vemuri, W. Wang, D. Wong, A. Yalamanchi, Q. Yu This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing. If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, the following notice is applicable: U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users are "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, shall be subject to license terms and license restrictions applicable to the programs. No other rights are granted to the U.S. Government. This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group. This software or hardware and documentation may provide access to or information on content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services. iii Contents Preface xxvii Audience xxvii Documentation Accessibility xxvii Related Documents xxviii Conventions xxviii What's New in Application Development? xxix Oracle Database 11g Release 2 (11.2.0.2) Feature xxix Oracle Database 11g Release 2 Features xxix Oracle Database 11g Release 1 Features xxxii Part I SQL for Application Developers 1 SQL Processing for Application Developers Description of SQL Statement Processing 1-1 Grouping Operations into Transactions 1-4 Deciding How to Group Operations in Transactions 1-4 Improving Transaction Performance 1-4 Committing Transactions 1-5 Managing Commit Redo Action 1-5 Rolling Back Transactions 1-7 Defining Transaction Savepoints 1-7 Ensuring Repeatable Reads with Read-Only Transactions 1-8 Using Cursors 1-9 How Many Cursors Can a Session Have? 1-9 Using a Cursor to Reexecute a Statement 1-9 Scrollable Cursors 1-10 Closing a Cursor 1-10 Canceling a Cursor 1-10 Locking Tables Explicitly 1-11 Privileges Required to Acquire Table Locks 1-11 Choosing a Locking Strategy 1-12 When to Lock with ROW SHARE MODE and ROW EXCLUSIVE MODE 1-12 When to Lock with SHARE MODE 1-13 When to Lock with SHARE ROW EXCLUSIVE MODE 1-14 iv When to Lock with EXCLUSIVE MODE 1-14 Letting Oracle Database Control Table Locking 1-14 Explicitly Acquiring Row Locks 1-15 Examples of Concurrency Under Explicit Locking 1-15 Using Oracle Lock Management Services (User Locks) 1-22 When to Use User Locks 1-22 Viewing and Monitoring Locks 1-23 Using Serializable Transactions for Concurrency Control 1-23 Transaction Interaction and Isolation Level 1-24 Setting Isolation Levels 1-26 Serializable Transactions and Referential Integrity 1-27 READ COMMITTED and SERIALIZABLE Isolation Levels 1-28 Transaction Set Consistency Differences 1-29 Choosing Transaction Isolation Levels 1-29 Autonomous Transactions 1-30 Examples of Autonomous Transactions 1-32 Ordering a Product 1-33 Withdrawing Money from a Bank Account 1-33 Defining Autonomous Transactions 1-36 Resuming Execution After Storage Allocation Errors 1-37 What Operations Have Resumable Storage Allocation? 1-37 Handling Suspended Storage Allocation 1-37 Using an AFTER SUSPEND Trigger in the Application 1-38 Checking for Suspended Statements 1-39 2 Using SQL Data Types in Database Applications Overview of SQL Data Types 2-1 Representing Character Data 2-2 Specifying Column Lengths as Bytes or Characters 2-2 Choosing Between CHAR and VARCHAR2 Data Types 2-3 Representing Numeric Data 2-4 Floating-Point Number Components 2-5 Floating-Point Number Formats 2-5 Binary Floating-Point Formats 2-6 Special Values for Native Floating-Point Formats 2-7 Comparison Operators for Native Floating-Point Data Types 2-8 Arithmetic Operations with Native Floating-Point Data Types 2-8 Conversion Functions for Floating-Point Data Types 2-9 Client Interfaces for Native Floating-Point Data Types 2-10 OCI Native Floating-Point Data Types SQLT_BFLOAT and SQLT_BDOUBLE 2-10 Native Floating-Point Data Types Supported in ADTs 2-10 Pro*C/C++ Support for Native Floating-Point Data Types 2-10 Representing Date and Time Data 2-10 Displaying Current Date and Time 2-11 Displaying and Inserting Dates in Nondefault Formats 2-12 Displaying and Inserting Times in Nondefault Formats 2-13 Arithmetic Operations with Datetime Data Types 2-14 v Conversion Functions for Datetime Data Types 2-15 Importing, Exporting, and Comparing Datetime Types 2-15 Representing Specialized Data 2-16 Representing Geographic Data 2-16 Representing Multimedia Data 2-16 Representing Large Amounts of Data 2-16 Large Objects (LOBs) 2-17 LONG and LONG RAW Data Types 2-17 Representing Searchable Text 2-18 Representing XML Data 2-18 Representing Dynamically Typed Data 2-19 Representing ANSI, DB2, and SQL/DS Data 2-21 Representing Conditional Expressions as Data 2-21 Identifying Rows by Address 2-22 Querying the ROWID Pseudocolumn 2-23 ROWID Data Type 2-24 Restricted Internal ROWID Format 2-24 Extended Internal ROWID Format 2-24 External Binary Internal ROWID Format 2-25 UROWID Data Type 2-25 How Oracle Database Converts Data Types 2-25 Data Type Conversion During Assignments 2-25 Data Type Conversion During Expression Evaluation 2-27 Metadata for SQL Operators and Functions 2-27 ARGn Data Type 2-27 DISP_TYPE Data Type 2-28 Data Type Families 2-28 3 Using Regular Expressions in Database Applications Overview of Regular Expressions 3-1 Oracle SQL Support for Regular Expressions 3-2 Oracle SQL and POSIX Regular Expression Standard 3-4 Operators in Oracle SQL Regular Expressions 3-4 POSIX Operators in Oracle SQL Regular Expressions 3-4 Oracle SQL Multilingual Extensions to POSIX Standard 3-7 Oracle SQL PERL-Influenced Extensions to POSIX Standard 3-7 Using Regular Expressions in SQL Statements: Scenarios 3-9 Using a Constraint to Enforce a Phone Number Format 3-9 Using Back References to Reposition Characters 3-10 4 Using Indexes in Database Applications Guidelines for Managing Indexes 4-1 Managing Indexes 4-2 When to Use Domain Indexes 4-2 When to Use Function-Based Indexes 4-2 Advantages of Function-Based Indexes 4-3 vi Disadvantages of Function-Based Indexes 4-3 Examples of Function-Based Indexes 4-5 5 Maintaining Data Integrity in Database Applications Enforcing Business Rules with Constraints 5-2 Enforcing Business Rules with Both Constraints and Application Code 5-3 Creating Indexes for Use with Constraints 5-4 When to Use NOT NULL Constraints 5-4 When to Use Default Column Values 5-5 Choosing a Primary Key for a Table (PRIMARY KEY Constraint) 5-6 When to Use UNIQUE Constraints 5-7 Enforcing Referential Integrity with FOREIGN KEY Constraints 5-8 FOREIGN KEY Constraints and NULL Values 5-10 Defining Relationships Between Parent and Child Tables 5-10 Rules for Multiple FOREIGN KEY Constraints 5-11 Deferring Constraint Checks 5-11 Minimizing Space and Time Overhead for Indexes Associated with Constraints 5-13 Guidelines for Indexing Foreign Keys 5-13 Referential Integrity in a Distributed Database 5-13 When to Use CHECK Constraints 5-14 Restrictions on CHECK Constraints 5-14 Designing CHECK Constraints 5-15 Rules for Multiple CHECK Constraints 5-15 Choosing Between CHECK and NOT NULL Constraints 5-15 Examples of Defining Constraints 5-16 Privileges Needed to Define Constraints 5-17 Naming Constraints 5-17 Enabling and Disabling Constraints 5-17 Why Disable Constraints? 5-18 Creating Enabled Constraints (Default) 5-18 Creating Disabled Constraints 5-19 Enabling Existing Constraints 5-19 Disabling Existing Constraints 5-20 Guidelines for Enabling and Disabling Key Constraints 5-20 Fixing Constraint Exceptions 5-21 Modifying Constraints 5-21 Renaming Constraints 5-22 Dropping Constraints 5-23 Managing FOREIGN KEY Constraints 5-24 Data Types and Names for Foreign Key Columns 5-24 Limit on Columns in Composite Foreign Keys 5-24 Foreign Key References Primary Key by Default 5-24 Privileges Required to Create FOREIGN KEY Constraints 5-24 Choosing How Foreign Keys Enforce Referential Integrity 5-24 Viewing Information About Constraints 5-25 Part II PL/SQL for Application Developers vii 6 Coding PL/SQL Subprograms and Packages Overview of PL/SQL Units 6-1 Anonymous Blocks 6-2 Stored PL/SQL Units 6-4 Naming Subprograms 6-5 Subprogram Parameters 6-5 Creating Subprograms 6-8 Altering Subprograms 6-9 Dropping Subprograms and Packages 6-9 External Subprograms 6-9 PL/SQL Function Result Cache 6-9 PL/SQL Packages 6-10 PL/SQL Object Size Limits 6-13 Creating Packages 6-13 Naming Packages and Package Objects 6-14 Package Invalidations and Session State 6-14 Packages Supplied with Oracle Database 6-15 Overview of Bulk Binding 6-15 When to Use Bulk Binds 6-16 Triggers 6-18 Compiling PL/SQL Subprograms for Native Execution 6-18 Cursor Variables 6-18 Declaring and Opening Cursor Variables 6-19 Examples of Cursor Variables 6-19 Handling PL/SQL Compile-Time Errors 6-21 Handling Runtime PL/SQL Errors 6-22 Declaring Exceptions and Exception Handlers 6-23 Unhandled Exceptions 6-24 Handling Errors in Distributed Queries 6-25 Handling Errors in Remote Subprograms 6-25 Debugging Stored Subprograms 6-26 PL/Scope 6-26 PL/SQL Hierarchical Profiler 6-26 Oracle JDeveloper 6-27 DBMS_OUTPUT Package 6-27 Privileges for Debugging PL/SQL and Java Stored Subprograms 6-27 Writing Low-Level Debugging Code 6-28 DBMS_DEBUG_JDWP Package 6-28 DBMS_DEBUG Package 6-28 Invoking Stored Subprograms 6-28 Privileges Required to Invoke a Subprogram 6-29 Invoking a Subprogram Interactively from Oracle Tools 6-30 Invoking a Subprogram from Another Subprogram 6-31 Invoking a Subprogram from a 3GL Application 6-32 Invoking Remote Subprograms 6-32 Synonyms for Remote Subprograms 6-33 Committing Transactions 6-34 viii Invoking Stored PL/SQL Functions from SQL Statements 6-35 Why Invoke Stored PL/SQL Subprograms from SQL Statements? 6-35 Where PL/SQL Functions Can Appear in SQL Statements 6-36 When PL/SQL Functions Can Appear in SQL Expressions 6-36 Controlling Side Effects 6-37 Restrictions 6-38 Declaring a Function 6-38 Parallel Query and Parallel DML 6-40 PRAGMA RESTRICT_REFERENCES for Backward Compatibility 6-41 Returning Large Amounts of Data from a Function 6-44 Coding Your Own Aggregate Functions 6-44 7 Using PL/Scope Specifying Identifier Collection 7-1 PL/Scope Identifier Data for STANDARD and DBMS_STANDARD 7-2 How Much Space is PL/Scope Data Using? 7-4 Viewing PL/Scope Data 7-4 Static Data Dictionary Views 7-5 Unique Keys 7-5 Context 7-5 Signature 7-7 Demo Tool 7-7 SQL Developer 7-7 Identifier Types that PL/Scope Collects 7-7 Usages that PL/Scope Reports 7-9 Sample PL/Scope Session 7-10 8 Using the PL/SQL Hierarchical Profiler Overview of PL/SQL Hierarchical Profiler 8-1 Collecting Profile Data 8-2 Understanding Raw Profiler Output 8-3 Namespaces of Tracked Subprograms 8-6 Special Function Names 8-6 Analyzing Profile Data 8-6 Creating Hierarchical Profiler Tables 8-7 Understanding Hierarchical Profiler Tables 8-8 Hierarchical Profiler Database Table Columns 8-8 Distinguishing Between Overloaded Subprograms 8-10 Hierarchical Profiler Tables for Sample PL/SQL Procedure 8-10 Examples of Calls to DBMS_HPROF.analyze with Options 8-11 plshprof Utility 8-13 plshprof Options 8-13 HTML Report from a Single Raw Profiler Output File 8-14 First Page of Report 8-14 Function-Level Reports 8-15 Module-Level Reports 8-16 Namespace-Level Reports 8-16 ix Parents and Children Report for a Function 8-17 HTML Difference Report from Two Raw Profiler Output Files 8-18 Difference Report Conventions 8-19 First Page of Difference Report 8-19 Function-Level Difference Reports 8-20 Module-Level Difference Reports 8-21 Namespace-Level Difference Reports 8-22 Parents and Children Difference Report for a Function 8-22 9 Developing PL/SQL Web Applications Overview of PL/SQL Web Applications 9-1 Implementing PL/SQL Web Applications 9-2 PL/SQL Gateway 9-2 mod_plsql 9-2 Embedded PL/SQL Gateway 9-3 PL/SQL Web Toolkit 9-3 Using mod_plsql Gateway to Map Client Requests to a PL/SQL Web Application 9-4 Using Embedded PL/SQL Gateway 9-4 How Embedded PL/SQL Gateway Processes Client Requests 9-5 Installing Embedded PL/SQL Gateway 9-6 Configuring Embedded PL/SQL Gateway 9-6 Configuring Embedded PL/SQL Gateway: Overview 9-6 Configuring User Authentication for Embedded PL/SQL Gateway 9-8 Invoking PL/SQL Stored Subprograms Through Embedded PL/SQL Gateway 9-17 Securing Application Access with Embedded PL/SQL Gateway 9-17 Restrictions in Embedded PL/SQL Gateway 9-18 Using Embedded PL/SQL Gateway: Scenario 9-18 Generating HTML Output with PL/SQL 9-20 Passing Parameters to PL/SQL Web Applications 9-21 Passing List and Dropdown-List Parameters from an HTML Form 9-21 Passing Option and Check Box Parameters from an HTML Form 9-22 Passing Entry-Field Parameters from an HTML Form 9-22 Passing Hidden Parameters from an HTML Form 9-24 Uploading a File from an HTML Form 9-24 Submitting a Completed HTML Form 9-24 Handling Missing Input from an HTML Form 9-25 Maintaining State Information Between Web Pages 9-25 Performing Network Operations in PL/SQL Subprograms 9-25 Sending Email from PL/SQL 9-26 Getting a Host Name or Address from PL/SQL 9-27 Using TCP/IP Connections from PL/SQL 9-27 Retrieving HTTP URL Contents from PL/SQL 9-27 Using Tables, Image Maps, Cookies, and CGI Variables from PL/SQL 9-29 10 Developing PL/SQL Server Pages (PSP) What Are PL/SQL Server Pages and Why Use Them? 10-1 x Prerequisites for Developing and Deploying PL/SQL Server Pages 10-2 PL/SQL Server Pages and the HTP Package 10-3 PL/SQL Server Pages and Other Scripting Solutions 10-3 Developing PL/SQL Server Pages 10-4 Specifying Basic Server Page Characteristics 10-5 Specifying the Scripting Language 10-6 Returning Data to the Client Browser 10-6 Handling Script Errors 10-7 Accepting User Input 10-8 Naming the PL/SQL Stored Procedure 10-9 Including the Contents of Other Files 10-9 Declaring Global Variables in a PSP Script 10-10 Specifying Executable Statements in a PSP Script 10-10 Substituting Expression Values in a PSP Script 10-11 Using Quotation Marks and Escaping Strings in a PSP Script 10-12 Including Comments in a PSP Script 10-12 Loading PL/SQL Server Pages into the Database 10-13 Querying PL/SQL Server Page Source Code 10-14 Running PL/SQL Server Pages Through URLs 10-15 Examples of PL/SQL Server Pages 10-16 Setup for PL/SQL Server Pages Examples 10-16 Printing the Sample Table with a Loop 10-17 Allowing a User Selection 10-18 Using an HTML Form to Invoke a PL/SQL Server Page 10-19 Including JavaScript in a PSP File 10-20 Debugging PL/SQL Server Pages 10-21 Putting PL/SQL Server Pages into Production 10-22 11 Using Continuous Query Notification (CQN) Object Change Notification (OCN) 11-2 Query Result Change Notification (QRCN) 11-2 Guaranteed Mode 11-3 Best-Effort Mode 11-3 Events that Generate Notifications 11-4 Committed DML Transactions 11-5 Committed DDL Statements 11-5 Deregistration 11-6 Global Events 11-6 Notification Contents 11-7 Good Candidates for CQN 11-7 Creating CQN Registrations 11-10 PL/SQL CQN Registration Interface 11-10 CQN Registration Options 11-11 Notification Type Option 11-11 QRCN Mode (QRCN Notification Type Only) 11-11 ROWID Option 11-12 Operations Filter Option (OCN Notification Type Only) 11-12 [...]... Oracle Database 11g Release 2 documentation set: ■ Oracle Database PL/SQL Language Reference ■ Oracle Call Interface Programmer's Guide ■ Oracle Database Security Guide ■ Pro*C/C++ Programmer's Guide ■ Oracle Database SQL Language Reference ■ Oracle Database Administrator's Guide ■ Oracle Database Concepts ■ Oracle XML Developer's Kit Programmer's Guide ■ Oracle XML DB Developer's Guide ■ Oracle Database. .. Application Developer's Guide is intended for application developers who are either developing applications or converting applications to run in the Oracle Database environment This guide is also valuable to anyone who is interested in the development of database applications, such as systems analysts and project managers To use this document effectively, you need a working knowledge of: ■ Application programming... Database Advanced Application Developer's Guide explains topics that experienced application developers reference repeatedly Information in this guide applies to features that work the same on all supported platforms, and does not include system-specific information Preface Topics: ■ Audience ■ Documentation Accessibility ■ Related Documents ■ Conventions Audience Oracle Database Advanced Application. .. 11-27 11-27 11-28 11-30 Advanced Topics for Application Developers 12 Using Oracle Flashback Technology Overview of Oracle Flashback Technology Application Development Features Database Administration Features Configuring Your Database for Oracle Flashback Technology Configuring Your Database for Automatic Undo Management Configuring Your Database for Oracle... Feature ■ Oracle Database 11g Release 2 Features ■ Oracle Database 11g Release 1 Features Oracle Database 11g Release 2 (11.2.0.2) Feature Edition Attribute of Database Service Before Release 11.2.0.2, you could not specify your initial session edition when using a database service to connect to Oracle Database If you wanted to use Edition-Based Redefinition for hot rollover, where some database clients... Agent Control" on page A-8 See Also: Oracle Database Net Services Administrator's Guide for detailed information about IPv6 support in Oracle Database Oracle Database 11g Release 1 Features The application development features for Oracle Database 11g Release 1 are: ■ WAIT Option for Data Definition Language (DDL) Statements ■ Binary XML Support for Oracle XML Database ■ Metadata for SQL Operators and... as online application upgrades, without affecting the behavior of existing applications For more information, see Oracle Database Administrator's Guide PL/SQL Function Result Cache Before Oracle Database 11g Release 1, if you wanted your PL/SQL application to cache the results of a function, you had to design and code the cache and cache-management subprograms If multiple sessions ran your application, ... The Oracle Database 11g Release 1 feature "Fine-Grained Invalidation" on page xxxvii has been extended to triggers Edition-Based Redefinition Edition-based redefinition enables you to upgrade the database component of an application while it is in use, thereby minimizing or eliminating down time To upgrade an application while it is in use, you copy the database objects that comprise the application. .. Oracle XA with Precompilers Using Precompilers with the Default Database Using Precompilers with a Named Database Using Oracle XA with OCI Managing Transaction Control with Oracle XA Examples of Precompiler Applications Migrating Precompiler or OCI Applications to TPM Applications Managing Oracle XA Library Thread Safety Specifying... users of the application they continue to run the unchanged application When you are sure that your changes are correct, you make the upgraded application available to all users Using edition-based redefinition means using one or more of its component features The features you use, and the down time, depend on these factors: ■ What kind of database objects you redefine ■ How available the database objects . Oracle® Database Advanced Application Developer's Guide 11g Release 2 (11.2) E25518-06 January 2013 Oracle Database Advanced Application Developer's Guide, 11g Release. in Application Development? xxix Oracle Database 11g Release 2 (11.2.0.2) Feature xxix Oracle Database 11g Release 2 Features xxix Oracle Database 11g Release 1 Features xxxii Part I SQL for Application. 11-30 Part III Advanced Topics for Application Developers 12 Using Oracle Flashback Technology Overview of Oracle Flashback Technology 12-1 Application Development Features 12-2 Database Administration