IBM i Database Database programming 7.1 IBM i Database Database programming 7.1 Note Before using this information and the product it supports, read the information in “Notices,” on page 295. This edition applies to version 7, release 1, modification 0 of IBM i (product number 5770-SS1) and to all subsequent releases and modifications until otherwise indicated in new editions. This version does not run on all reduced instruction set computer (RISC) models nor does it run on CISC models. © Copyright International Business Machines Corporation 1998, 2010. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents Database programming 1 What’s new for IBM i 7.1 1 PDF file for Database programming 1 Database file concepts 2 DB2fori 2 Interfaces to DB2 for i 2 Traditional system interface 2 SQL 2 System i Navigator 3 IBM Query for i 3 Database files 3 How database files are described 4 Externally and program-described data . . . 4 Dictionary-described data 5 Record format description 5 Access path description 6 Naming conventions for a database file . . . 6 Database data protection and monitoring 7 Database file sizes 7 Example: Database file sizes 11 Setting up database files 12 Creating and describing database files 12 Creating a library 13 Setting up source files 13 Why source files are used 13 Creating a source file 14 Describing database files 17 Describing database files using DDS . . . 18 Specifying database file and member attributes 26 Setting up physical files 33 Creating a physical file 33 Specifying physical file and member attributes 34 Implicit physical file journaling 38 Setting up logical files 38 Creating a logical file 39 Creating a logical file with more than one record format 39 Defining logical file members 43 Describing logical file record formats 45 Describing field use for logical files . . . 47 Deriving new fields from existing fields . . 48 Describing floating-point fields in logical files 50 Describing access paths for logical files . . . 51 Selecting and omitting records for logical files 52 Sharing existing access paths between logical files 55 Setting up a join logical file 58 Example 1: Joining two physical files . . . 59 Example 2: Using more than one field to join files 67 Example 3: Reading duplicate records in the secondary file 69 Example 4: Using join fields whose attributes are different 70 Example 5: Describing fields that never appear in the record format 72 Example 6: Specifying key fields in a join logical file 73 Example 7: Joining three or more physical files 74 Example 8: Joining a physical file to itself 76 Example 9: Using defaults for missing records from secondary files 77 Example 10: A complex join logical file . . 79 Join logical file considerations 80 Describing access paths for database files . . . 82 Using arrival sequence access paths for database files 83 Using keyed sequence access paths for database files 84 Arranging key fields in an alternative collating sequence 84 Arranging key fields with the SRTSEQ parameter 85 Arranging key fields in ascending or descending sequence 86 Using more than one key field 87 Preventing duplicate key values 88 Arranging duplicate keys 89 Using existing access path specifications . . . 91 Using binary floating-point fields in database file access paths 91 Securing database files 92 Granting file and data authority 92 Authorizing a user or group using System i Navigator 92 Types of object authority 93 Types of data authority 94 Specifying public authority 95 Defining public authority using System i Navigator 96 Setting a default public authority using System i Navigator 96 Using database file capabilities to control I/O operations 97 Limiting access to specific fields in a database file 97 Using logical files to secure data 98 Processing database files 99 Database file processing: Runtime considerations 99 File and member name 100 File processing options 100 Specifying the type of processing 100 Specifying the initial file position 101 Reusing deleted records 101 Ignoring the keyed sequence access path 102 Delaying end-of-file processing 102 Specifying the record length 103 © Copyright IBM Corp. 1998, 2010 iii Ignoring record formats 103 Determining whether duplicate keys exist 103 Data recovery and integrity 103 Protecting your files with journaling and commitment control 103 Writing data and access paths to auxiliary storage 104 Checking changes to the record format description 104 Checking the expiration date of a physical file member 105 Preventing the job from changing data in a file 105 Locking shared data 105 Locking records 105 Locking files 106 Locking members 106 Locking record format data 107 Database lock considerations 107 Displaying locked rows using System i Navigator 108 Displaying locked records using the Display Record Locks (DSPRCDLCK) command 109 Sharing database files in the same job or activation group 109 Open considerations for files shared in a job or an activation group 110 Input/output considerations for files shared in a job or an activation group . . 111 Close considerations for files shared in a job or an activation group 111 Sequential-only processing of database files 115 Open considerations for sequential-only processing 116 Input/output considerations for sequential-only processing 117 Close considerations for sequential-only processing 117 Summary of runtime considerations for processing database files 118 Storage pool paging option effect on database performance 121 Opening a database file 121 Opening a database file member 121 Using Open Database File (OPNDBF) command 122 Using Open Query File (OPNQRYF) command 123 Creating queries 124 Dynamically selecting records 128 Arranging records 139 Formatting records 142 Grouping records 146 Joining records 150 Optimizing performance 154 Handling errors 160 Usage notes for the Open Query File (OPNQRYF) command 161 Basic database file operations in programs. . . 193 Setting a position in the file 193 Reading database records 194 Reading database records using an arrival sequence access path 194 Reading database records using a keyed sequence access path 195 Waiting for more records when end of file is reached 197 Releasing locked records 199 Updating database records 200 Adding database records 201 Identifying which record format to add in a file with multiple formats 202 Using the force-end-of-data operation . . 203 Deleting database records 204 Closing a database file 205 Monitoring database file errors in a program 206 System handling of error messages 206 Effect of error messages on file positioning 207 Determining which messages you want to monitor 207 Managing database files 208 Basic operations for managing database files 208 Copying a file 208 Moving a file 208 Managing database members 209 Member operations common to all database files 209 Adding members 209 Changing member attributes 210 Renaming members 210 Removing members 210 Physical file member operations 210 Initializing data in a physical file member 211 Clearing data from a physical file member 211 Reorganizing a physical file member . . 212 Displaying records in a physical file member 217 Using database attribute and cross-reference information 217 Displaying information about database files 218 Displaying attributes of a file using System i Navigator 218 Displaying attributes of a file using the Display File Description (DSPFD) command 218 Displaying the description of the fields in a file 218 Displaying the relationships between files on the system 219 Displaying the files used by programs . . 220 Displaying the system cross-reference files 221 Writing the output from a command directly to a database file 221 Example: A command output file 222 Output files for the Display File Description (DSPFD) command 222 Output files for the Display Journal (DSPJRN) command 223 Output files for the Display Problems (DSPPRB) command 223 iv IBM i: Database Database programming Changing database file descriptions and attributes 223 Effects of changing fields in a file description 223 Changing a physical file description and attributes 225 Example 1: Changing a physical file description and attributes 226 Example 2: Changing a physical file description and attributes 226 Changing a logical file description and attributes 227 Recovering and restoring your database . . . 228 Recovering data in a database file 228 Managing journals 228 Ensuring data integrity with commitment control 236 Reducing time in access path recovery . . . 237 Saving access paths 237 Restoring access paths 238 Journaling access paths 238 System-managed access-path protection 239 Rebuilding access paths 239 Database recovery process after an abnormal system end 242 Database file recovery during the IPL . . 242 Database file recovery after the IPL . . . 243 Effects of the storage pool paging option on database recovery 243 Database file recovery options table . . . 244 Database save and restore 244 Database considerations for save and restore 244 Using source files 245 Working with source files 245 Using the source entry utility 245 Using device source files 246 Copying source file data 246 Loading and unloading data from systems other than System i 247 Using source files in a program 248 Creating an object using a source file . . . 248 Creating an object from source statements in a batch job 249 Determining which source file member was used to create an object 250 Managing a source file 250 Changing source file attributes 250 Reorganizing source file member data . . 251 Determining when a source statement was changed 251 Using source files for documentation . . 252 Controlling the integrity of your database with constraints 252 Setting up constraints for your database . . 252 Removing unique, primary key, or check constraints 253 Working with a group of constraints . . . 254 Details: Working with a group of constraints 254 Working with constraints that are in check pending status 255 Unique constraints 256 Primary key constraints 257 Check constraints 257 Ensuring data integrity with referential constraints 258 Adding referential constraints 258 Before you add referential constraints . . 258 Defining the parent file in a referential constraint 258 Defining the dependent file in a referential constraint 259 Specifying referential constraint rules . . 259 Details: Adding referential constraints . . 261 Details: Avoiding constraint cycles . . . 261 Verifying referential constraints 262 Enabling or disabling referential constraints 262 Removing referential constraints 263 Details: Removing a constraint with the CST parameter 263 Details: Removing a constraint with the TYPE parameter 263 Details: Ensuring data integrity with referential constraints 264 Example: Ensuring data integrity with referential constraints 264 Referential integrity terms 265 Referential integrity enforcement 265 Foreign key enforcement 266 Parent key enforcement 266 Constraint states 266 Check pending status in referential constraints 267 Dependent file restrictions in check pending 267 Parent file restrictions in check pending 268 Referential integrity and CL commands . . 268 Triggering automatic events in your database 270 Uses for triggers 270 Benefits of using triggers in your business 270 Creating trigger programs 270 Adding triggers using System i Navigator 271 How trigger programs work 271 Other important information about working with trigger programs 271 Example: Trigger program 276 Trigger buffer sections 280 Adding triggers 282 Displaying triggers 283 Removing triggers 284 Enabling or disabling physical file triggers 284 Triggers and their relationship to CL commands 284 Triggers and their relationship to referential integrity 286 Database distribution 287 Double-byte character set considerations 287 DBCS field data types 287 DBCS field mapping considerations 288 DBCS field concatenation 288 DBCS field substring operations 289 Comparing DBCS fields in a logical file . . . 289 Contents v Using DBCS fields in the Open Query File (OPNQRYF) command 290 Using the wildcard function with DBCS fields 290 Comparing DBCS fields through the Open Query File (OPNQRYF) command 290 Using concatenation with DBCS fields . . . 291 Using sort sequence with DBCS fields . . . 291 Related information for Database programming 291 Appendix. Notices 295 Programming interface information 296 Trademarks 297 Terms and conditions 297 vi IBM i: Database Database programming Database programming DB2 ® for i provides a wide range of support for setting up, processing, and managing database files. Note: By using the code examples, you agree to the terms of the “Code license and disclaimer information” on page 293. What’s new for IBM i 7.1 Read about new or changed information for the Database programming topic collection. XML Data Type You can now create an XML data type in SQL tables. The XML data type is handled like a LOB field. How to see what is new or changed To help you see where technical changes have been made, the information center uses: v The image to mark where new or changed information begins. v The image to mark where new or changed information ends. In PDF files, you might see revision bars (|) in the left margin of new and changed information. To find other information about what’s new or changed this release, see the Memo to users. PDF file for Database programming You can view and print a PDF file of this information. To view or download the PDF version of this document, select Database programming (about 1800 KB). Saving PDF files To save a PDF on your workstation for viewing or printing: 1. Right-click the PDF link in your browser. 2. Click the option that saves the PDF locally. 3. Navigate to the directory in which you want to save the PDF. 4. Click Save. Downloading Adobe Reader You need Adobe ® Reader installed on your system to view or print these PDFs. You can download a free copy from the Adobe Web site (www.adobe.com/products/acrobat/readstep.html) . © Copyright IBM Corp. 1998, 2010 1 | Related reference “Related information for Database programming” on page 291 Product manuals and other information center topic collections contain information that relates to the Database programming topic collection. You can view or print any of the PDF files. Database file concepts This introduction to i5/OS ® database files includes information about DB2 for i interfaces to database files, the types and maximum sizes of database files, and the ways of describing and protecting database files. DB2 for i DB2 for i is the integrated relational database manager on the i5/OS operating system. DB2 for i is part of the i5/OS operating system. It provides access to and protection for data. It also provides advanced functions such as referential integrity and parallel database processing. With DB2 for i, independent auxiliary storage pools (ASPs), also known as independent disk pools, allow you to have one or more separate databases associated with each ASP group. You can set up databases using primary independent disk pools. Related concepts Independent disk pools examples Interfaces to DB2 for i DB2 for i provides several independent interfaces to the database. Traditional system interface The i5/OS traditional system interface is the full set of system commands and other non-SQL facilities that you can use to access and change DB2 for i data. The traditional system interface provides control language (CL) commands to create and manage database objects. The system interface also has an integrated facility for describing data called data description specifications (DDS). The IBM ® Rational ® Development Studio for i licensed program provides several utilities to describe and process data. The data file utility (DFU) can add, change, and delete data in a database file that is described by RPG, DDS, and the interactive data description utility (IDDU). The source entry utility (SEU) can specify and change data in files. SQL Structured Query Language (SQL) is a standardized language that can be used within host programming languages or interactively to define, access, and manipulate data in a relational database. SQL uses a relational model of data; that is, it perceives all data as existing in tables. The DB2 for i database has SQL processing capability integrated into the system. It processes compiled programs that contain SQL statements. To develop SQL applications, you need the IBM DB2 Query Manager and SQL Development Kit for i licensed program for the system on which you develop your applications. Interactive SQL is a function of the IBM DB2 Query Manager and SQL Development Kit for i licensed program that allows SQL statements to run dynamically instead of in batch mode. Every interactive SQL statement is read from the workstation, prepared, and run dynamically. 2 IBM i: Database Database programming [...]... the database system: interactive data definition utility (IDDU), Structured Query Language (SQL) commands, or data description specifications (DDS) Interactive data definition utility (IDDU) Physical files can be described with IDDU You might use IDDU because it is a menu-driven, interactive method of describing data You might be familiar with describing data using IDDU in a System/36 environment In... describing database files You can create and describe a library and a database file using the traditional system interface The system supports several methods for creating and describing a database file: v Interactive data definition utility (IDDU) 12 IBM i: Database Database programming You can create a database file by using IDDU, part of the IBM Rational Development Studio for i licensed program If... The R in position 17 indicates that a record format is being defined The record format name ORDHDR is specified in positions 19 through 28 You make no entry in position 17 when you are describing a field; a blank in position 17 along with a name in positions 19 through 28 indicates a field name The data type is specified in position 35 The valid data types are: Entry Meaning Database programming 19... special considerations for using binary floating-point fields: v The precision associated with a binary floating-point field is a function of the number of bits (single or double precision) and the internal representation of the binary floating-point value This translates into the number of decimal digits supported in the significand and the maximum values that can be represented in the binary floating-point... description that is stored in the data dictionary You can describe the record format information using the interactive data definition utility (IDDU) Even though the file is program described, IBM Query for i, System i Access, and the data file utility (DFU) use the record format description that is stored in the data dictionary You can use IDDU to describe and then create a file The file created is... record format ITMMST, there are three fields Field ITEM is zoned decimal, 5 digits, with no decimal position Field DESCRP is character, with 18 positions Field PRICE is zoned decimal, 5 digits, with two decimal positions A physical file can have only one record format The record format in a physical file describes the way the data is actually stored A logical file contains no data Logical files are used... extra digits to uniquely establish the internal bit pattern in the internal binary floating-point format, so that identical results are obtained when a binary floating-point number in internal format is converted to decimal and back again to internal format If the data type (position 35) is not specified, the decimal positions entry is used to determine the data type If the decimal positions (positions... concepts IDDU Use PDF SQL programming DB2 for i5 /OS SQL reference “Displaying information about database files” on page 218 By using System i Navigator or CL commands, you can display various types of information about database files Describing database files using DDS: When you describe a database file using data description specifications (DDS), you can describe the file at the file, record-format, join,... files The first field must be a 6-digit sequence number field containing zoned decimal data and two decimal digits v The second field must be a 6-digit date field containing zoned decimal data and zero decimal digits v All fields following the second field must be zoned decimal or character v v v v Describing database files You can use several methods to describe i5 /OS database files This topic discusses... create a physical file for the sole purpose of using its field descriptions That is, the file does not contain data; it is used only as a reference for the field descriptions for other files This type of file is known as a field reference file A field reference file is a physical file containing no data, just field descriptions You can use a field reference file to simplify record format descriptions and . 45 Describing field use for logical files . . . 47 Deriving new fields from existing fields . . 48 Describing floating-point fields in logical files 50 Describing. no decimal position. Field DESCRP is character, with 18 positions. Field PRICE is zoned decimal, 5 digits, with two decimal positions. A physical file can