Database SQL Programming 7.1
Total Page:16
File Type:pdf, Size:1020Kb
IBM IBM i Database SQL programming 7.1 IBM IBM i Database SQL programming 7.1 Note Before using this information and the product it supports, read the information in “Notices,” on page 493. This edition applies to IBM i 7.1 (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 IBM Corporation 1998, 2010. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents SQL programming .......... 1 | Defining field procedures......... 28 | What's new for IBM i 7.1 .......... 1 | Field definition for field procedures .... 29 PDF file for SQL programming ........ 4 | Specifying the field procedure ...... 29 Introduction to DB2 for i Structured Query Language 4 | When field procedures are invoked .... 29 SQL concepts ............. 5 | Parameter list for execution of field SQL relational database and system | procedures ............ 30 terminology ............ 6 | The field procedure parameter value list SQL and system naming conventions .... 7 | (FPPVL) ............ 32 Types of SQL statements ........ 7 | Parameter value descriptors for field SQL communication area ........ 9 | procedures ........... 32 SQL diagnostics area.......... 9 | Field-definition (function code 8) .... 33 SQL objects .............. 9 | Field-encoding (function code 0) .... 34 Schemas .............. 9 | Field-decoding (function code 4) .... 35 Journals and journal receivers ...... 9 | Example field procedure program .... 36 Catalogs ............. 10 | General guidelines for writing field Tables, rows, and columns ....... 10 | procedures ............ 37 Aliases .............. 10 | Index considerations ........ 38 Views .............. 10 | Thread considerations ........ 38 Indexes .............. 11 | Debug considerations ........ 38 Constraints ............ 11 | Guidelines for writing field procedures that Triggers ............. 12 | mask data ............. 38 Stored procedures .......... 12 | Example field procedure program that Sequences ............. 12 | masks data ........... 41 | Global variables ........... 12 Creating descriptive labels using the LABEL ON User-defined functions......... 12 statement .............. 42 User-defined types .......... 13 Describing an SQL object using COMMENT ON 43 | XSR objects ............ 13 Changing a table definition ........ 44 SQL packages ........... 13 Adding a column .......... 44 Application program objects ........ 13 Changing a column.......... 44 User source file ........... 15 Allowable conversions of data types .... 45 Output source file member ....... 15 Deleting a column .......... 46 Program ............. 15 Order of operations for the ALTER TABLE SQL package ............ 15 statement ............. 46 Module.............. 16 | Using CREATE OR REPLACE TABLE ... 47 Service program ........... 16 Creating and using ALIAS names ...... 48 Data definition language .......... 16 Creating and using views......... 48 Creating a schema ........... 16 WITH CHECK OPTION on a view .... 50 Creating a table ............ 17 WITH CASCADED CHECK OPTION .. 51 Adding and removing constraints ..... 17 WITH LOCAL CHECK OPTION .... 51 Referential integrity and tables ...... 18 Example: Cascaded check option .... 52 Adding and removing referential Creating indexes............ 53 constraints............ 18 | Creating and using global variables ..... 53 Example: Adding referential constraints .. 19 | Replacing existing objects......... 54 Example: Removing constraints...... 20 Catalogs in database design ........ 54 Check pending ........... 20 Getting catalog information about a table .. 55 Creating a table using LIKE ........ 21 Getting catalog information about a column 55 Creating a table using AS......... 21 Dropping a database object ........ 55 Creating and altering a materialized query table 22 Data manipulation language......... 56 Declaring a global temporary table ..... 23 Retrieving data using the SELECT statement .. 56 | Creating a table with remote server data ... 23 Basic SELECT statement ........ 56 Creating a row change timestamp column ... 24 Specifying a search condition using the Creating and altering an identity column ... 24 WHERE clause ........... 57 Using ROWID ............ 25 Expressions in the WHERE clause .... 58 Creating and using sequences ....... 26 Comparison operators ........ 59 Comparison of identity columns and NOT keyword .......... 60 sequences ............. 27 GROUP BY clause .......... 60 © Copyright IBM Corp. 1998, 2010 iii HAVING clause ........... 62 Including subqueries in the WHERE or ORDER BY clause .......... 63 HAVING clause ......... 123 Static SELECT statements........ 65 Correlated subqueries ........ 124 Handling null values ......... 65 Correlated names and references .... 124 Special registers in SQL statements .... 66 Example: Correlated subquery in a Casting data types .......... 68 WHERE clause .......... 125 Date, time, and timestamp data types ... 68 Example: Correlated subquery in a Specifying current date and time values .. 68 HAVING clause ......... 126 Date/time arithmetic ........ 69 Example: Correlated subquery in a Row change expressions ........ 69 select-list ............ 127 Handling duplicate rows ........ 69 Example: Correlated subquery in an Defining complex search conditions .... 70 UPDATE statement ........ 127 Special considerations for LIKE..... 71 Example: Correlated subquery in a Multiple search conditions within a DELETE statement ........ 128 WHERE clause .......... 72 Sort sequences and normalization in SQL .... 128 Using OLAP specifications ....... 73 Sort sequence used with ORDER BY and row Joining data from more than one table ... 76 selection .............. 129 Inner join ............ 76 Sort sequence and ORDER BY...... 129 Left outer join .......... 77 Sort sequence and row selection ..... 131 Right outer join .......... 78 Sort sequence and views ........ 131 Exception join .......... 78 Sort sequence and the CREATE INDEX Cross join ............ 79 statement.............. 132 Full outer join .......... 80 Sort sequence and constraints ....... 132 Multiple join types in one statement ... 81 ICU sort sequence ........... 132 Using table expressions ........ 81 Normalization ............ 133 Using recursive queries ........ 83 Data protection ............. 134 Using the UNION keyword to combine Security for SQL objects ......... 134 subselects ............. 95 Authorization ID .......... 135 Specifying the UNION ALL keyword... 98 Views .............. 135 Using the EXCEPT keyword ...... 100 Auditing ............. 135 Using the INTERSECT keyword ..... 102 Data integrity ............ 136 Data retrieval errors ......... 104 Concurrency............ 136 Inserting rows using the INSERT statement .. 105 Journaling ............ 138 Inserting rows using the VALUES clause .. 106 Commitment control ......... 138 Inserting rows using a select-statement ... 107 Savepoints ............ 142 Inserting multiple rows using the blocked Atomic operations.......... 144 INSERT statement .......... 108 Constraints ............ 145 Inserting data into tables with referential Adding and using check constraints ... 146 constraints ............ 108 Save and restore functions ....... 146 Inserting values into an identity column .. 109 Damage tolerance .......... 147 Selecting inserted values........ 110 Index recovery ........... 148 | Inserting data from a remote database ... 110 Catalog integrity .......... 148 Changing data in a table using the UPDATE User auxiliary storage pool ....... 149 statement .............. 111 Independent auxiliary storage pool .... 149 Updating a table using a scalar-subselect .. 112 Routines ............... 149 Updating a table with rows from another Stored procedures ........... 149 table .............. 112 Defining an external procedure ..... 150 Updating tables with referential constraints 113 Defining an SQL procedure....... 151 Examples: UPDATE rules ...... 114 | Defining a procedure with default Updating an identity column ...... 114 | parameters ............ 156 Updating data as it is retrieved from a table 114 Calling a stored procedure ....... 158 Removing rows from a table using the DELETE Using the CALL statement where statement .............. 116 procedure definition exists ...... 158 Removing rows from tables with referential Using the embedded CALL statement constraints ............ 117 where no procedure definition exists .. 159 Example: DELETE rules ....... 118 Using the embedded CALL statement | Merging data ............ 119 with an SQLDA ......... 160 Using subqueries ........... 120 Using the dynamic CALL statement where Subqueries in SELECT statements .... 121 no CREATE PROCEDURE exists .... 161 Subqueries and search conditions.... 122 Examples: CALL statements ..... 161 Usage notes on subqueries ...... 122 Returning result sets from stored procedures 167 iv IBM i: Database SQL programming Example 1: Calling a stored procedure Improving implementation of procedures and that returns a single result set ..... 168 functions ............. 231 Example 2: Calling a stored procedure Redesigning routines for performance ... 233 that returns a result set from a nested Processing special data types ........ 234 procedure ........... 169 Large objects ............ 234 | Writing a program or SQL procedure to Large object data types ........ 235 | receive the result sets from a