Oracle® Database Application Developer's Guide - Fundamentals 10g Release 1 (10.1) Part No. B10795-01 December 2003 Oracle Database Application Developer's Guide - Fundamentals, 10g Release 1 (10.1) Part No. B10795-01 Copyright © 1996, 2003 Oracle Corporation. All rights reserved. Primary Authors: Drew Adams, Eric Paapanen Contributing Authors: M. Cowan, R. Moran, J. Russell, R. Strohm Contributors: D. Alpern, G. Arora, C. Barclay, D. Bronnikov, T. Chang, M. Davidson, G. Doherty, D. Elson, A. Ganesh, M. Hartstein, J. Huang, N. Jain, R. Jenkins Jr., S. Kotsovolos, S. Kumar, C. Lei, D. Lorentz, R. Murthy, R. Pang, B. Sinha, S. Vemuri, W. Wang, D. Wong, A. Yalamanchi, Q. Yu Graphic Designer: V. Moore The Programs (which include both the software and documentation) contain proprietary information of Oracle Corporation; they are provided under a license agreement containing restrictions on use and disclosure and are also protected by copyright, patent and other intellectual and industrial property laws. Reverse engineering, disassembly or decompilation of the Programs, except to the extent required to obtain interoperability with other independently created software or as specified by law, is prohibited. The information contained in this document is subject to change without notice. If you find any problems in the documentation, please report them to us in writing. Oracle Corporation does not warrant that this document is error-free. Except as may be expressly permitted in your license agreement for these Programs, no part of these Programs may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without the express written permission of Oracle Corporation. If the Programs are delivered to the U.S. Government or anyone licensing or using the programs on behalf of the U.S. Government, the following notice is applicable: Restricted Rights Notice Programs delivered subject to the DOD FAR Supplement are "commercial computer software" and use, duplication, and disclosure of the Programs, including documentation, shall be subject to the licensing restrictions set forth in the applicable Oracle license agreement. Otherwise, Programs delivered subject to the Federal Acquisition Regulations are "restricted computer software" and use, duplication, and disclosure of the Programs shall be subject to the restrictions in FAR 52.227-19, Commercial Computer Software - Restricted Rights (June, 1987). Oracle Corporation, 500 Oracle Parkway, Redwood City, CA 94065. The Programs are not intended for use in any nuclear, aviation, mass transit, medical, or other inherently dangerous applications. It shall be the licensee's responsibility to take all appropriate fail-safe, backup, redundancy, and other measures to ensure the safe use of such applications if the Programs are used for such purposes, and Oracle Corporation disclaims liability for any damages caused by such use of the Programs. Oracle is a registered trademark, and ConText, Oracle Store, Oracle8i, Oracle9i, PL/SQL, Pro*COBOL, Pro*C, Pro*C/C++, SQL*Net, and SQL*Plus are trademarks or registered trademarks of Oracle Corporation. Other names may be trademarks of their respective owners. iii Contents Send Us Your Comments xxv Preface xxvii Audience xxvii Organization xxix Related Documentation xxxi Conventions xxxiii Documentation Accessibility xxxv What's New in Application Development? xxxvii New Application Development Features in Oracle Database 10g Release 1 xxxvii New Application Development Features in Oracle9i Release 2 xxxix New Application Development Features in Oracle9i Release 1 xlii Part I Introduction to Application Development Features of Oracle Database 1 Programmatic Environments Overview of Developing an Oracle Database Application 1-2 Overview of PL/SQL 1-3 A Simple PL/SQL Example 1-4 Advantages of PL/SQL 1-5 Full Support for SQL 1-5 Tight Integration with Oracle Database 1-5 Better Performance 1-5 iv Higher Productivity 1-6 Scalability 1-6 Maintainability 1-6 PL/SQL Support for Object-Oriented Programming 1-6 Object Types 1-6 Collections 1-7 Portability 1-7 Security 1-7 Built-In Packages for Application Development 1-7 Built-In Packages for Server Management 1-8 Built-In Packages for Distributed Database Access 1-8 Overview of Java Support Built Into the Database 1-8 Overview of Oracle JVM 1-8 Overview of Oracle Extensions to JDBC 1-9 JDBC Thin Driver 1-10 JDBC OCI Driver 1-10 JDBC Server-Side Internal Driver 1-11 Oracle Database Extensions to JDBC Standards 1-11 Sample JDBC 2.0 Program 1-12 Sample Pre-2.0 JDBC Program 1-12 JDBC in SQLJ Applications 1-13 Overview of Oracle SQLJ 1-13 Benefits of SQLJ 1-15 Comparing SQLJ with JDBC 1-15 SQLJ Stored Procedures in the Server 1-16 Overview of Oracle JPublisher 1-17 Overview of Java Stored Procedures 1-17 Overview of Database Web Services 1-17 Database as a Web Service Provider 1-18 Database as a Web Service Consumer 1-18 Overview of Writing Procedures and Functions in Java 1-19 Overview of Writing Database Triggers in Java 1-19 Why Use Java for Stored Procedures and Triggers? 1-19 Overview of Pro*C/C++ 1-20 How You Implement a Pro*C/C++ Application 1-20 v Highlights of Pro*C/C++ Features 1-21 Overview of Pro*COBOL 1-23 How You Implement a Pro*COBOL Application 1-23 Highlights of Pro*COBOL Features 1-24 Overview of OCI and OCCI 1-25 Advantages of OCI 1-26 Parts of the OCI 1-27 Procedural and Non-Procedural Elements 1-27 Building an OCI Application 1-28 Overview of Oracle Data Provider for .NET (ODP.NET) 1-29 Using ODP.NET in a Simple Application 1-29 Overview of Oracle Objects for OLE (OO4O) 1-30 OO4O Automation Server 1-31 OO4O Object Model 1-32 OraSession 1-33 OraServer 1-33 OraDatabase 1-34 OraDynaset 1-34 OraField 1-35 OraMetaData and OraMDAttribute 1-35 OraParameters and OraParameter 1-35 OraParamArray 1-36 OraSQLStmt 1-36 OraAQ 1-36 OraAQMsg 1-37 OraAQAgent 1-37 Support for Oracle LOB and Object Datatypes 1-37 OraBLOB and OraCLOB 1-38 OraBFILE 1-38 Oracle Data Control 1-39 Oracle Objects for OLE C++ Class Library 1-39 Additional Sources of Information 1-39 Choosing a Programming Environment 1-40 Choosing Whether to Use OCI or a Precompiler 1-40 Using Built-In Packages and Libraries 1-41 vi Java Compared to PL/SQL 1-41 PL/SQL Is Optimized for Database Access 1-42 PL/SQL Is Integrated with the Database 1-42 Both Java and PL/SQL Have Object-Oriented Features 1-42 Java Is Used for Open Distributed Applications 1-42 Part II Designing the Database 2 Selecting a Datatype Summary of Oracle Built-In Datatypes 2-2 Representing Character Data 2-8 Column Lengths for Single-Byte and Multibyte Character Sets 2-9 Implicit Conversion Between CHAR/VARCHAR2 and NCHAR/NVARCHAR2 2-10 Comparison Semantics 2-10 Representing Numeric Data with Number and Floating-Point Datatypes 2-11 Floating-Point Number System Concepts 2-12 About Floating-Point Formats 2-12 Representing Special Values with Native Floating-Point Formats 2-14 Behavior of Special Values for Native Floating-Point Datatypes 2-15 Rounding of Native Floating-Point Datatypes 2-15 Comparison Operators for Native Floating-Point Datatypes 2-16 Arithmetic Operators for Native Floating-Point Datatypes 2-16 Conversion Functions for Native Floating-Point Datatypes 2-16 Exceptions for Native Floating-Point Datatypes 2-17 Client Interfaces for Native Floating-Point Datatypes 2-18 SQL Native Floating-Point Datatypes 2-18 OCI Native Floating-Point Datatypes SQLT_BFLOAT and SQLT_BDOUBLE 2-18 Native Floating-Point Datatypes Supported in Oracle OBJECT Types 2-18 Pro*C/C++ Support for Native Floating-Point Datatypes 2-19 Storing Data Using the NUMBER Datatype 2-19 Representing Date and Time Data 2-20 Date Format 2-21 Checking If Two DATE Values Refer to the Same Day 2-21 Displaying the Current Date and Time 2-21 Setting SYSDATE to a Constant Value 2-21 vii Printing a Date with BC/AD Notation 2-21 Time Format 2-22 Performing Date Arithmetic 2-22 Converting Between Datetime Types 2-23 Handling Time Zones 2-23 Importing and Exporting Datetime Types 2-24 Establishing Year 2000 Compliance 2-24 Oracle Server Year 2000 Compliance 2-25 Centuries and the Year 2000 2-25 Examples of The RR Date Format 2-26 Examples of The CC Date Format 2-27 Storing Dates in Character Datatypes 2-27 Viewing Date Settings 2-28 Altering Date Settings 2-29 Troubleshooting Y2K Problems in Applications 2-29 Representing Conditional Expressions as Data 2-32 Representing Geographic Coordinate Data 2-33 Representing Image, Audio, and Video Data 2-33 Representing Searchable Text Data 2-34 Representing Large Amounts of Data 2-34 Using RAW and LONG RAW Datatypes 2-35 Addressing Rows Directly with the ROWID Datatype 2-36 Extended ROWID Format 2-36 Different Forms of the ROWID 2-37 ROWID Pseudocolumn 2-37 Internal ROWID 2-37 External Character ROWID 2-37 External Binary ROWID 2-38 ROWID Migration and Compatibility Issues 2-38 Accessing Oracle Database Version 7 from an Oracle9i Client 2-39 Accessing an Oracle9i Database from a Client of Oracle Database Version 7 2-39 Import and Export 2-39 ANSI/ISO, DB2, and SQL/DS Datatypes 2-39 How Oracle Database Converts Datatypes 2-40 Datatype Conversion During Assignments 2-41 viii Datatype Conversion During Expression Evaluation 2-43 Representing Dynamically Typed Data 2-44 Representing XML Data 2-47 3 Maintaining Data Integrity Through Constraints Overview of Integrity Constraints 3-2 When to Enforce Business Rules with Integrity Constraints 3-2 Example of an Integrity Constraint for a Business Rule 3-2 When to Enforce Business Rules in Applications 3-3 Creating Indexes for Use with Constraints 3-3 When to Use NOT NULL Integrity Constraints 3-3 When to Use Default Column Values 3-4 Setting Default Column Values 3-5 Choosing a Table's Primary Key 3-5 When to Use UNIQUE Key Integrity Constraints 3-6 Constraints On Views: for Performance, Not Data Integrity 3-7 Enforcing Referential Integrity with Constraints 3-8 About Nulls and Foreign Keys 3-10 Defining Relationships Between Parent and Child Tables 3-10 No Constraints on the Foreign Key 3-10 NOT NULL Constraint on the Foreign Key 3-10 UNIQUE Constraint on the Foreign Key 3-11 UNIQUE and NOT NULL Constraints on the Foreign Key 3-11 Rules for Multiple FOREIGN KEY Constraints 3-11 Deferring Constraint Checks 3-12 Guidelines for Deferring Constraint Checks 3-12 Select Appropriate Data 3-12 Ensure Constraints Are Created Deferrable 3-12 Set All Constraints Deferred 3-13 Check the Commit (Optional) 3-13 Managing Constraints That Have Associated Indexes 3-14 Minimizing Space and Time Overhead for Indexes Associated with Constraints 3-14 Guidelines for Indexing Foreign Keys 3-14 About Referential Integrity in a Distributed Database 3-15 When to Use CHECK Integrity Constraints 3-15 ix Restrictions on CHECK Constraints 3-16 Designing CHECK Constraints 3-16 Rules for Multiple CHECK Constraints 3-17 Choosing Between CHECK and NOT NULL Integrity Constraints 3-17 Examples of Defining Integrity Constraints 3-17 Example: Defining Integrity Constraints with the CREATE TABLE Command 3-18 Example: Defining Constraints with the ALTER TABLE Command 3-18 Privileges Required to Create Constraints 3-19 Naming Integrity Constraints 3-19 Enabling and Disabling Integrity Constraints 3-19 Why Disable Constraints? 3-20 About Exceptions to Integrity Constraints 3-20 Enabling Constraints 3-20 Creating Disabled Constraints 3-21 Enabling and Disabling Existing Integrity Constraints 3-21 Enabling Existing Constraints 3-21 Disabling Existing Constraints 3-22 Tip: Using the Data Dictionary to Find Constraints 3-22 Guidelines for Enabling and Disabling Key Integrity Constraints 3-23 Fixing Constraint Exceptions 3-23 Altering Integrity Constraints 3-23 Renaming Integrity Constraints 3-24 Dropping Integrity Constraints 3-25 Managing FOREIGN KEY Integrity Constraints 3-26 Datatypes and Names for Foreign Key Columns 3-26 Limit on Columns in Composite Foreign Keys 3-26 Foreign Key References Primary Key by Default 3-26 Privileges Required to Create FOREIGN KEY Integrity Constraints 3-27 Choosing How Foreign Keys Enforce Referential Integrity 3-27 Viewing Definitions of Integrity Constraints 3-28 Examples of Defining Integrity Constraints 3-28 Example 1: Listing All of Your Accessible Constraints 3-29 Example 2: Distinguishing NOT NULL Constraints from CHECK Constraints 3-30 Example 3: Listing Column Names that Constitute an Integrity Constraint 3-30 x 4 Selecting an Index Strategy Guidelines for Application-Specific Indexes 4-2 Create Indexes After Inserting Table Data 4-2 Switch Your Temporary Tablespace to Avoid Space Problems Creating Indexes 4-3 Index the Correct Tables and Columns 4-3 Limit the Number of Indexes for Each Table 4-4 Choose the Order of Columns in Composite Indexes 4-4 Gather Statistics to Make Index Usage More Accurate 4-5 Drop Indexes That Are No Longer Required 4-6 Privileges Required to Create an Index 4-6 Creating Indexes: Basic Examples 4-6 When to Use Domain Indexes 4-7 When to Use Function-Based Indexes 4-8 Advantages of Function-Based Indexes 4-9 Examples of Function-Based Indexes 4-10 Example: Function-Based Index for Case-Insensitive Searches 4-10 Example: Precomputing Arithmetic Expressions with a Function-Based Index 4-10 Example: Function-Based Index for Language-Dependent Sorting 4-11 Restrictions for Function-Based Indexes 4-11 5 How Oracle Database Processes SQL Statements Overview of SQL Statement Execution 5-2 Identifying Extensions to SQL92 (FIPS Flagging) 5-2 Grouping Operations into Transactions 5-4 Improving Transaction Performance 5-4 Committing Transactions 5-5 Rolling Back Transactions 5-5 Defining Transaction Savepoints 5-6 An Example of COMMIT, SAVEPOINT, and ROLLBACK 5-6 Privileges Required for Transaction Management 5-7 Ensuring Repeatable Reads with Read-Only Transactions 5-7 Using Cursors within Applications 5-8 Declaring and Opening Cursors 5-9 Using a Cursor to Execute Statements Again 5-9 Closing Cursors 5-10 [...]... Resuming Operations After an Error Condition Writing an Application to Handle Suspended Storage Allocation 5-1 0 5-1 0 5-1 1 5-1 2 5-1 2 5-1 4 5-1 5 5-1 5 5-1 5 5-1 6 5-1 7 5-1 8 5-1 8 5-1 9 5-1 9 5-2 1 5-2 3 5-2 3 5-2 3 5-2 4 5-2 5 5-2 5 5-2 6 5-2 7 5-2 8 5-2 8 5-3 2 5-3 2 5-3 3 5-3 6 5-3 7 5-3 8 5-3 8 5-3 8 5-3 9 xi Example of Resumable Storage Allocation 5-3 9 6 Coding Dynamic SQL Statements What Is Dynamic SQL? ... Inter-Language Parameter Mode Mappings Executing External Procedures with the CALL Statement Preconditions for External Procedures Privileges of External Procedures xvi 8-5 8-1 0 8-1 2 8-1 2 8-1 2 8-1 2 8-1 2 8-1 2 8-1 3 8-1 3 8-1 3 8-1 3 8-1 4 8-1 4 8-1 5 8-1 6 8-1 6 8-1 8 8-1 9 8-2 1 8-2 3 8-2 4 8-2 5 8-2 5 8-2 7 8-2 7 8-2 8 8-2 9 8-2 9 8-3 2 8-3 3 8-3 3 8-3 3 8-3 4 8-3 5 Managing Permissions... Trigger Applications Auditing with Triggers: Example Integrity Constraints and Triggers: Examples Referential Integrity Using Triggers Foreign Key Trigger for Child Table xviii 9-7 9-8 9-9 9-1 0 9-1 1 9-1 2 9-1 3 9-1 4 9-1 5 9-1 5 9-1 6 9-1 7 9-1 8 9-1 8 9-1 9 9-1 9 9-2 0 9-2 0 9-2 1 9-2 5 9-2 6 9-2 6 9-2 7 9-2 7 9-2 8 9-2 8 9-2 8 9-2 8 9-2 9 9-2 9 9-3 1 9-3 2 9-3 7 9-3 8 9-3 9 UPDATE... Synonyms for Procedures and Packages Calling Stored Functions from SQL Expressions xiv 7-2 1 7-2 1 7-2 2 7-2 3 7-2 5 7-2 5 7-2 6 7-2 6 7-2 8 7-2 9 7-2 9 7-3 0 7-3 1 7-3 1 7-3 1 7-3 2 7-3 3 7-3 5 7-3 6 7-3 8 7-3 8 7-3 8 7-4 0 7-4 3 7-4 3 7-4 4 7-4 5 7-4 5 7-4 5 7-4 6 7-4 7 7-4 7 7-4 8 7-4 9 7-5 0 Using PL/SQL Functions Syntax for SQL Calling a PL/SQL Function Naming Conventions ... Precompiler Applications Migrating Precompiler or OCI Applications to TPM Applications XA Library Thread Safety Specifying Threading in the Open String Restrictions on Threading in XA xxii 1 5-9 1 5-1 0 1 5-1 2 1 5-1 3 1 5-1 5 1 5-1 5 1 5-1 6 1 6-2 1 6-4 1 6-5 1 6-5 1 6-5 1 6-6 1 6-6 1 6-7 1 6-8 1 6-8 1 6-9 1 6-9 1 6-1 0 1 6-1 1 1 6-1 2 1 6-1 6 1 6-1 6 1 6-1 6 1 6-1 7 1 6-1 8 1 6-1 9 1 6-2 0 1 6-2 1 1 6-2 3 1 6-2 3... Hiding PL/SQL Code with the PL/SQL Wrapper Compiling PL/SQL Procedures for Native Execution 7-2 7-2 7-4 7-5 7-5 7-6 7-7 7-7 7-8 7-9 7-9 7-1 0 7-1 1 7-1 1 7-1 2 7-1 2 7-1 2 7-1 3 7-1 4 7-1 4 7-1 5 7-1 5 7-1 6 7-1 6 7-1 6 7-1 7 7-1 7 7-1 8 7-1 8 7-1 9 7-1 9 7-2 0 7-2 0 7-2 1 xiii Remote Dependencies Timestamps Disadvantages of the Timestamp Model Signatures... Solutions Writing the Code and Content for the PL/SQL Server Page The Format of the PSP File xx 1 3-2 1 3-3 1 3-4 1 3-4 1 3-4 1 3-5 1 3-5 1 3-6 1 3-8 1 3-9 1 3-9 1 3-1 0 1 3-1 0 1 3-1 2 1 3-1 3 1 3-1 3 1 3-1 3 1 3-1 4 1 3-1 5 1 3-1 5 1 3-1 6 1 3-1 6 1 3-1 6 1 3-1 9 1 3-1 9 1 3-2 0 1 3-2 0 1 3-2 0 1 3-2 1 1 3-2 1 Syntax of PL/SQL Server Page Elements Page Directive Procedure Directive Parameter... Performing DML with RETURNING Clause Using Dynamic SQL: Example Using Dynamic SQL in Languages Other Than PL/SQL xii 6-2 6-3 6-3 6-4 6-4 6-5 6-6 6-7 6-7 6-8 6-9 6-9 6-1 0 6-1 1 6-1 1 6-1 2 6-1 4 6-1 4 6-1 5 6-1 5 6-1 6 6-1 6 6-1 6 6-1 6 6-1 6 6-1 7 6-1 7 6-1 9 6-1 9 6-2 0 7 Using Procedures and Packages Overview of PL/SQL Program Units Anonymous Blocks Stored Program Units... Putting an Application using PL/SQL Server Pages into Production Enabling PL/SQL Web Applications for XML 14 1 3-2 7 1 3-2 7 1 3-2 7 1 3-2 8 1 3-2 8 1 3-2 8 1 3-2 8 1 3-2 9 1 3-2 9 1 3-3 0 1 3-3 0 1 3-3 1 1 3-3 1 1 3-3 2 1 3-3 2 1 3-3 3 1 3-3 5 1 3-3 8 1 3-3 9 1 3-4 1 Porting Non -Oracle Applications to Oracle Database 10g Performing Natural Joins and Inner Joins 1 4-2 Migrating a Schema and Data from Another Database. .. Boundaries Returning Large Amounts of Data from a Function Coding Your Own Aggregate Functions 8 7-5 0 7-5 1 7-5 1 7-5 2 7-5 2 7-5 3 7-5 3 7-5 4 7-5 4 7-5 5 7-5 5 7-5 6 7-5 7 7-5 9 7-6 1 7-6 2 7-6 3 7-6 3 7-6 3 7-6 4 7-6 4 7-6 5 7-6 5 7-6 5 7-6 6 7-6 8 7-6 9 7-7 1 Calling External Procedures Overview of Multi-Language Programs What Is an External Procedure? Overview of The Call Specification . Oracle Database Application Developer's Guide - Fundamentals 10g Release 1 (10.1) Part No. B1079 5-0 1 December 2003 Oracle Database Application. Object-Oriented Programming 1-6 Object Types 1-6 Collections 1-7 Portability 1-7 Security 1-7 Built-In Packages for Application Development 1-7 Built-In Packages for