db21

909
DB2 ® SQL Reference Volume 1 DB2 Version 9 for Linux, UNIX, and Windows SC10-4249-00

Upload: api-3742395

Post on 11-Apr-2015

183 views

Category:

Documents


5 download

TRANSCRIPT

DB2

DB2 Version 9for Linux, UNIX, and Windows

SQL Reference Volume 1

SC10-4249-00

DB2

DB2 Version 9for Linux, UNIX, and Windows

SQL Reference Volume 1

SC10-4249-00

Before using this information and the product it supports, be sure to read the general information under Notices.

Edition Notice This document contains proprietary information of IBM. It is provided under a license agreement and is protected by copyright law. The information contained in this publication does not include any product warranties, and any statements provided in this manual should not be interpreted as such. You can order IBM publications online or through your local IBM representative. v To order publications online, go to the IBM Publications Center at www.ibm.com/shop/publications/order v To find your local IBM representative, go to the IBM Directory of Worldwide Contacts at www.ibm.com/ planetwide To order DB2 publications from DB2 Marketing and Sales in the United States or Canada, call 1-800-IBM-4YOU (426-4968). When you send information to IBM, you grant IBM a nonexclusive right to use or distribute the information in any way it believes appropriate without incurring any obligation to you. Copyright International Business Machines Corporation 1993, 2006. All rights reserved. US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

ContentsAbout this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvWho should use this book . . . How this book is structured . . . A brief overview of Volume 2 . How to read the syntax diagrams . Conventions used in this manual . Error conditions . . . . . . Highlighting conventions . . . Related documentation . . . . . xv . xv . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi . . . . . . . . . . . . . . . . . . . . . . . . . . xviii . . . . . . . . . . . . . . . . . . . . . . . . . . xviii . . . . . . . . . . . . . . . . . . . . . . . . . . xviii . . . . . . . . . . . . . . . . . . . . . . . . . . . xix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Chapter 1. Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Relational databases . . . . . . . . . . . . . . . . . . . . . Structured Query Language (SQL) . . . . . . . . . . . . . . . . Queries and table expressions . . . . . . . . . . . . . . . . . DB2 Call level interface (CLI) and open database connectivity (ODBC) . . . . Java database connectivity (JDBC) and embedded SQL for Java (SQLJ) programs Schemas . . . . . . . . . . . . . . . . . . . . . . . . . Tables . . . . . . . . . . . . . . . . . . . . . . . . . Keys . . . . . . . . . . . . . . . . . . . . . . . . . . Constraints . . . . . . . . . . . . . . . . . . . . . . . . Unique constraints . . . . . . . . . . . . . . . . . . . . Referential constraints . . . . . . . . . . . . . . . . . . . Table check constraints . . . . . . . . . . . . . . . . . . . Informational constraints . . . . . . . . . . . . . . . . . . Indexes . . . . . . . . . . . . . . . . . . . . . . . . . Triggers . . . . . . . . . . . . . . . . . . . . . . . . Views . . . . . . . . . . . . . . . . . . . . . . . . . Aliases . . . . . . . . . . . . . . . . . . . . . . . . . Packages . . . . . . . . . . . . . . . . . . . . . . . . Authorization, privileges, and object ownership . . . . . . . . . . . Catalog views . . . . . . . . . . . . . . . . . . . . . . Application processes, concurrency, and recovery . . . . . . . . . . . Isolation levels . . . . . . . . . . . . . . . . . . . . . . Comparison of isolation levels . . . . . . . . . . . . . . . . Table spaces and other storage structures . . . . . . . . . . . . . Character conversion . . . . . . . . . . . . . . . . . . . . Distributed relational databases. . . . . . . . . . . . . . . . . Remote unit of work . . . . . . . . . . . . . . . . . . . Application-directed distributed unit of work . . . . . . . . . . . Data representation considerations. . . . . . . . . . . . . . . Event monitors . . . . . . . . . . . . . . . . . . . . . . Database partitioning across multiple database partitions . . . . . . . . Large object behavior in partitioned tables . . . . . . . . . . . . . DB2 federated systems . . . . . . . . . . . . . . . . . . . Federated systems . . . . . . . . . . . . . . . . . . . . What is a data source? . . . . . . . . . . . . . . . . . . . The federated database . . . . . . . . . . . . . . . . . . The SQL compiler . . . . . . . . . . . . . . . . . . . . Wrappers and wrapper modules . . . . . . . . . . . . . . . Server definitions and server options . . . . . . . . . . . . . . User mappings . . . . . . . . . . . . . . . . . . . . . Nicknames and data source objects . . . . . . . . . . . . . . Nickname column options . . . . . . . . . . . . . . . . . Data type mappings . . . . . . . . . . . . . . . . . . . The federated server . . . . . . . . . . . . . . . . . . . Copyright IBM Corp. 1993, 2006

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . .

. 1 . 1 . 1 . 2 . 2 . 3 . 4 . 5 . 5 . 6 . 6 . 9 . 9 . 10 . 10 . 12 . 12 . 13 . 13 . 18 . 18 . 20 . 22 . 23 . 25 . 27 . 28 . 31 . 35 . 35 . 36 . 37 . 39 . 39 . 40 . 40 . 40 . 41 . 42 . 42 . 43 . 43 . 44 . 45

iii

Supported data sources . . The federated database system The query optimizer . . . Collating sequences. . . .

. . . catalog . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

45 48 49 49

Chapter 2. Language elements . . . . . . . . . . . . . . . . . . . . . . . . . 53Characters . . . . . . . . . . . . . . . . . . . . Tokens . . . . . . . . . . . . . . . . . . . . . Identifiers . . . . . . . . . . . . . . . . . . . . Naming conventions and implicit object name qualifications . . Aliases . . . . . . . . . . . . . . . . . . . . Authorization IDs and authorization names . . . . . . . Column names . . . . . . . . . . . . . . . . . References to host variables . . . . . . . . . . . . . Data types . . . . . . . . . . . . . . . . . . . . Data types . . . . . . . . . . . . . . . . . . . Numbers . . . . . . . . . . . . . . . . . . . Character strings . . . . . . . . . . . . . . . . Graphic strings . . . . . . . . . . . . . . . . . Binary strings . . . . . . . . . . . . . . . . . Large objects (LOBs) . . . . . . . . . . . . . . . Datetime values . . . . . . . . . . . . . . . . . DATALINK values . . . . . . . . . . . . . . . . XML values . . . . . . . . . . . . . . . . . . User-defined types . . . . . . . . . . . . . . . . Promotion of data types . . . . . . . . . . . . . . Casting between data types . . . . . . . . . . . . . Assignments and comparisons. . . . . . . . . . . . Rules for result data types . . . . . . . . . . . . . Rules for string conversions . . . . . . . . . . . . Database partition-compatible data types . . . . . . . . Constants . . . . . . . . . . . . . . . . . . . Integer constants . . . . . . . . . . . . . . . . Floating-point constants . . . . . . . . . . . . . . Decimal constants . . . . . . . . . . . . . . . . Character string constants . . . . . . . . . . . . . Hexadecimal constants . . . . . . . . . . . . . . Graphic string constants . . . . . . . . . . . . . . Special registers . . . . . . . . . . . . . . . . . Special registers . . . . . . . . . . . . . . . . CURRENT CLIENT_ACCTNG . . . . . . . . . . . CURRENT CLIENT_APPLNAME . . . . . . . . . . CURRENT CLIENT_USERID . . . . . . . . . . . . CURRENT CLIENT_WRKSTNNAME . . . . . . . . . CURRENT DATE . . . . . . . . . . . . . . . . CURRENT DBPARTITIONNUM . . . . . . . . . . . CURRENT DEFAULT TRANSFORM GROUP . . . . . . CURRENT DEGREE . . . . . . . . . . . . . . . CURRENT EXPLAIN MODE . . . . . . . . . . . . CURRENT EXPLAIN SNAPSHOT . . . . . . . . . . CURRENT FEDERATED ASYNCHRONY . . . . . . . . CURRENT IMPLICIT XMLPARSE OPTION . . . . . . . CURRENT ISOLATION . . . . . . . . . . . . . . CURRENT LOCK TIMEOUT . . . . . . . . . . . . CURRENT MAINTAINED TABLE TYPES FOR OPTIMIZATION CURRENT PACKAGE PATH . . . . . . . . . . . . CURRENT PATH . . . . . . . . . . . . . . . . CURRENT QUERY OPTIMIZATION . . . . . . . . . CURRENT REFRESH AGE . . . . . . . . . . . . CURRENT SCHEMA . . . . . . . . . . . . . . CURRENT SERVER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 . 55 . 57 . 57 . 61 . 62 . 66 . 71 . 78 . 78 . 80 . 81 . 85 . 86 . 87 . 88 . 91 . 93 . 94 . 97 . 99 . 105 . 116 . 120 . 122 . 124 . 124 . 124 . 125 . 125 . 125 . 126 . 127 . 127 . 129 . 130 . 131 . 132 . 133 . 134 . 135 . 136 . 137 . 138 . 139 . 140 . 141 . 142 . 143 . 144 . 145 . 146 . 147 . 148 . 149

iv

SQL Reference Volume 1

CURRENT TIME . . . . . . . . . . . . CURRENT TIMESTAMP . . . . . . . . . CURRENT TIMEZONE . . . . . . . . . . CURRENT USER . . . . . . . . . . . . SESSION_USER . . . . . . . . . . . . SYSTEM_USER . . . . . . . . . . . . USER . . . . . . . . . . . . . . . . Functions . . . . . . . . . . . . . . . . External, SQL, and sourced user-defined functions . Scalar, column, row, and table user-defined functions Function signatures . . . . . . . . . . . Function resolution . . . . . . . . . . . Function invocation . . . . . . . . . . . Conservative binding semantics . . . . . . . Methods . . . . . . . . . . . . . . . . External and SQL user-defined methods . . . . Method signatures . . . . . . . . . . . . Method resolution . . . . . . . . . . . . Method invocation . . . . . . . . . . . Dynamic dispatch of methods . . . . . . . . Expressions . . . . . . . . . . . . . . . Expressions . . . . . . . . . . . . . . Datetime operations and durations . . . . . . CASE expressions . . . . . . . . . . . . CAST specifications . . . . . . . . . . . XMLCAST specifications . . . . . . . . . Dereference operations . . . . . . . . . . OLAP functions . . . . . . . . . . . . Method invocation . . . . . . . . . . . Subtype treatment . . . . . . . . . . . . Sequence reference . . . . . . . . . . . Predicates . . . . . . . . . . . . . . . Predicates . . . . . . . . . . . . . . Search conditions . . . . . . . . . . . . Basic predicate . . . . . . . . . . . . . Quantified predicate . . . . . . . . . . . BETWEEN predicate . . . . . . . . . . . EXISTS predicate . . . . . . . . . . . . IN predicate . . . . . . . . . . . . . . LIKE predicate . . . . . . . . . . . . . NULL predicate . . . . . . . . . . . . TYPE predicate . . . . . . . . . . . . . VALIDATED predicate . . . . . . . . . . XMLEXISTS predicate . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

150 151 152 153 154 155 156 157 157 157 158 158 162 162 165 165 165 166 169 170 173 173 180 185 187 190 192 194 200 202 203 207 207 208 211 212 215 216 217 219 224 225 227 228

Chapter 3. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231Functions overview . . . . . . . . . . . . . Supported functions and administrative SQL routines and Aggregate functions . . . . . . . . . . . . . AVG . . . . . . . . . . . . . . . . . . CORRELATION . . . . . . . . . . . . . . COUNT . . . . . . . . . . . . . . . . . COUNT_BIG . . . . . . . . . . . . . . . COVARIANCE . . . . . . . . . . . . . . GROUPING . . . . . . . . . . . . . . . MAX . . . . . . . . . . . . . . . . . . MIN . . . . . . . . . . . . . . . . . . XMLAGG . . . . . . . . . . . . . . . . Regression functions . . . . . . . . . . . . . STDDEV . . . . . . . . . . . . . . . . . SUM . . . . . . . . . . . . . . . . . . . . views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 233 258 259 261 262 263 265 266 268 270 271 273 276 277

Contents

v

VARIANCE . . . . . . . . . Scalar functions . . . . . . . ABS or ABSVAL . . . . . . . ACOS . . . . . . . . . . ASCII . . . . . . . . . . . ASIN . . . . . . . . . . . ATAN . . . . . . . . . . ATAN2 . . . . . . . . . . ATANH . . . . . . . . . . BIGINT . . . . . . . . . . BLOB . . . . . . . . . . . CEILING or CEIL . . . . . . . CHAR . . . . . . . . . . CHARACTER_LENGTH . . . . CHR . . . . . . . . . . . CLOB . . . . . . . . . . . COALESCE . . . . . . . . . CONCAT . . . . . . . . . COS . . . . . . . . . . . COSH . . . . . . . . . . COT . . . . . . . . . . . DATAPARTITIONNUM . . . . DATE . . . . . . . . . . . DAY . . . . . . . . . . . DAYNAME . . . . . . . . . DAYOFWEEK . . . . . . . . DAYOFWEEK_ISO . . . . . . DAYOFYEAR . . . . . . . . DAYS . . . . . . . . . . . DBCLOB . . . . . . . . . DBPARTITIONNUM . . . . . . DECIMAL . . . . . . . . . DECRYPT_BIN and DECRYPT_CHAR DEGREES . . . . . . . . . DEREF . . . . . . . . . . DIFFERENCE . . . . . . . . DIGITS . . . . . . . . . . DOUBLE . . . . . . . . . ENCRYPT . . . . . . . . . EVENT_MON_STATE . . . . . EXP . . . . . . . . . . . FLOAT . . . . . . . . . . FLOOR . . . . . . . . . . GETHINT . . . . . . . . . GENERATE_UNIQUE . . . . . GRAPHIC . . . . . . . . . HASHEDVALUE . . . . . . . HEX . . . . . . . . . . . HOUR . . . . . . . . . . IDENTITY_VAL_LOCAL . . . . INSERT . . . . . . . . . . INTEGER . . . . . . . . . JULIAN_DAY . . . . . . . . LCASE or LOWER . . . . . . LCASE (SYSFUN schema) . . . . LEFT . . . . . . . . . . . LENGTH . . . . . . . . . LN . . . . . . . . . . . LOCATE . . . . . . . . . . LOG . . . . . . . . . . . LOG10 . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

278 279 280 281 282 283 284 285 286 287 289 290 291 295 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 315 319 321 322 323 324 325 327 329 330 331 332 333 334 336 338 340 342 343 347 348 350 351 352 353 354 356 357 360 361

vi

SQL Reference Volume 1

LONG_VARCHAR . . LONG_VARGRAPHIC . LTRIM . . . . . . LTRIM (SYSFUN schema) MICROSECOND . . . MIDNIGHT_SECONDS . MINUTE . . . . . MOD . . . . . . . MONTH . . . . . . MONTHNAME . . . MULTIPLY_ALT . . . NULLIF . . . . . . OCTET_LENGTH . . POSITION . . . . . POSSTR . . . . . . POWER . . . . . . QUARTER . . . . . RADIANS . . . . . RAISE_ERROR . . . RAND . . . . . . REAL . . . . . . . REC2XML . . . . . REPEAT . . . . . . REPLACE . . . . . RIGHT . . . . . . ROUND . . . . . . RTRIM . . . . . . RTRIM (SYSFUN schema) SECLABEL . . . . . SECLABEL_BY_NAME . SECLABEL_TO_CHAR . SECOND . . . . . SIGN . . . . . . . SIN . . . . . . . SINH . . . . . . . SMALLINT . . . . . SOUNDEX . . . . . SPACE . . . . . . SQRT . . . . . . . STRIP . . . . . . . SUBSTR . . . . . . SUBSTRING . . . . TABLE_NAME . . . TABLE_SCHEMA . . . TAN . . . . . . . TANH . . . . . . TIME . . . . . . . TIMESTAMP . . . . TIMESTAMP_FORMAT . TIMESTAMP_ISO . . . TIMESTAMPDIFF . . TO_CHAR . . . . . TO_DATE . . . . . TRANSLATE . . . . TRIM . . . . . . . TRUNCATE or TRUNC . TYPE_ID . . . . . TYPE_NAME . . . . TYPE_SCHEMA . . . UCASE or UPPER . . VALUE . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

362 363 364 365 366 367 368 369 370 371 372 374 375 376 379 381 382 383 384 385 386 387 391 392 393 394 396 397 398 399 400 402 403 404 405 406 407 408 409 410 411 414 417 418 420 421 422 423 424 426 427 429 430 431 433 435 436 437 438 439 440

Contents

vii

VARCHAR . . . . VARCHAR_FORMAT VARGRAPHIC . . . WEEK . . . . . WEEK_ISO . . . . XMLATTRIBUTES . XMLCOMMENT . . XMLCONCAT . . . XMLDOCUMENT . XMLELEMENT . . XMLFOREST . . . XMLNAMESPACES . XMLPARSE . . . . XMLPI . . . . . XMLQUERY . . . XMLSERIALIZE . . XMLTEXT . . . . XMLVALIDATE . . XMLXSROBJECTID . YEAR . . . . . . Table functions . . . XMLTABLE . . . . User-defined functions

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

441 443 445 447 448 449 451 452 454 456 462 466 469 471 473 476 479 481 485 486 487 488 492

Chapter 4. Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495Procedures overview . . . . . . XSR_ADDSCHEMADOC procedure . XSR_COMPLETE procedure . . . XSR_DTD procedure . . . . . . XSR_EXTENTITY procedure . . . XSR_REGISTER procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 496 498 499 501 503

Chapter 5. Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505SQL queries . . . . . . . . . . Subselect . . . . . . . . . . . select-clause . . . . . . . . . from-clause . . . . . . . . . table-reference . . . . . . . . joined-table . . . . . . . . . where-clause . . . . . . . . group-by-clause . . . . . . . having-clause . . . . . . . . order-by-clause . . . . . . . . fetch-first-clause . . . . . . . Examples of subselects . . . . . Examples of joins . . . . . . . Examples of grouping sets, cube, and Fullselect . . . . . . . . . . . Examples of a fullselect . . . . . Select-statement . . . . . . . . common-table-expression . . . . update-clause . . . . . . . . read-only-clause . . . . . . . optimize-for-clause . . . . . . isolation-clause . . . . . . . . lock-request-clause . . . . . . Examples of a select-statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . rollup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 506 507 510 510 518 520 521 526 527 530 530 532 535 543 545 548 548 553 554 554 555 555 556

Appendix A. SQL and XQuery limits

. . . . . . . . . . . . . . . . . . . . . . 559

viii

SQL Reference Volume 1

Appendix B. SQLCA (SQL communications area) . . . . . . . . . . . . . . . . . 567SQLCA field descriptions . . . . . . . . Error reporting . . . . . . . . . . . . SQLCA usage in partitioned database systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567 . 570 . 571

Appendix C. SQLDA (SQL descriptor area) . . . . . . . . . . . . . . . . . . . . 573SQLDA field descriptions . . . . . . . . . Fields in the SQLDA header . . . . . . . Fields in an occurrence of a base SQLVAR . . . Fields in an occurrence of a secondary SQLVAR . Effect of DESCRIBE on the SQLDA . . . . . . SQLTYPE and SQLLEN . . . . . . . . . . Unrecognized and unsupported SQLTYPEs . . Packed decimal numbers . . . . . . . . SQLLEN field for decimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 574 575 576 578 579 580 581 581

Appendix D. Catalog views . . . . . . . . . . . . . . . . . . . . . . . . . . 583System catalog views . . . . . . . Road map to the catalog views . . . SYSIBM.SYSDUMMY1 . . . . . . SYSCAT.ATTRIBUTES . . . . . . SYSCAT.BUFFERPOOLDBPARTITIONS SYSCAT.BUFFERPOOLS . . . . . SYSCAT.CASTFUNCTIONS . . . . SYSCAT.CHECKS . . . . . . . . SYSCAT.COLAUTH . . . . . . . SYSCAT.COLCHECKS . . . . . . SYSCAT.COLDIST . . . . . . . SYSCAT.COLGROUPCOLS . . . . SYSCAT.COLGROUPDIST . . . . . SYSCAT.COLGROUPDISTCOUNTS . SYSCAT.COLGROUPS . . . . . . SYSCAT.COLIDENTATTRIBUTES . . SYSCAT.COLOPTIONS . . . . . . SYSCAT.COLUMNS . . . . . . . SYSCAT.COLUSE . . . . . . . . SYSCAT.CONSTDEP . . . . . . . SYSCAT.DATAPARTITIONEXPRESSION SYSCAT.DATAPARTITIONS . . . . SYSCAT.DATATYPES . . . . . . SYSCAT.DBAUTH . . . . . . . SYSCAT.DBPARTITIONGROUPDEF . SYSCAT.DBPARTITIONGROUPS . . SYSCAT.EVENTMONITORS . . . . SYSCAT.EVENTS . . . . . . . . SYSCAT.EVENTTABLES . . . . . SYSCAT.FULLHIERARCHIES . . . . SYSCAT.FUNCMAPOPTIONS . . . SYSCAT.FUNCMAPPARMOPTIONS . SYSCAT.FUNCMAPPINGS . . . . SYSCAT.HIERARCHIES . . . . . SYSCAT.INDEXAUTH . . . . . . SYSCAT.INDEXCOLUSE . . . . . SYSCAT.INDEXDEP . . . . . . . SYSCAT.INDEXES . . . . . . . SYSCAT.INDEXEXPLOITRULES . . . SYSCAT.INDEXEXTENSIONDEP . . SYSCAT.INDEXEXTENSIONMETHODS SYSCAT.INDEXEXTENSIONPARMS . SYSCAT.INDEXEXTENSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 585 589 590 592 593 594 595 596 597 598 599 600 601 602 603 604 605 610 611 612 613 615 617 618 619 620 622 623 624 625 626 627 628 629 630 631 632 637 638 639 640 641

Contents

ix

SYSCAT.INDEXOPTIONS . . . . . . . . . . SYSCAT.INDEXXMLPATTERNS . . . . . . . . SYSCAT.KEYCOLUSE . . . . . . . . . . . SYSCAT.NAMEMAPPINGS . . . . . . . . . SYSCAT.NICKNAMES . . . . . . . . . . . SYSCAT.PACKAGEAUTH . . . . . . . . . . SYSCAT.PACKAGEDEP . . . . . . . . . . SYSCAT.PACKAGES . . . . . . . . . . . . SYSCAT.PARTITIONMAPS . . . . . . . . . SYSCAT.PASSTHRUAUTH . . . . . . . . . SYSCAT.PREDICATESPECS . . . . . . . . . SYSCAT.REFERENCES . . . . . . . . . . . SYSCAT.ROUTINEAUTH . . . . . . . . . . SYSCAT.ROUTINEDEP . . . . . . . . . . . SYSCAT.ROUTINEOPTIONS . . . . . . . . . SYSCAT.ROUTINEPARMOPTIONS . . . . . . . SYSCAT.ROUTINEPARMS . . . . . . . . . . SYSCAT.ROUTINES . . . . . . . . . . . . SYSCAT.ROUTINESFEDERATED . . . . . . . SYSCAT.SCHEMAAUTH . . . . . . . . . . SYSCAT.SCHEMATA . . . . . . . . . . . SYSCAT.SECURITYLABELACCESS . . . . . . . SYSCAT.SECURITYLABELCOMPONENTELEMENTS SYSCAT.SECURITYLABELCOMPONENTS . . . . SYSCAT.SECURITYLABELS . . . . . . . . . SYSCAT.SECURITYPOLICIES . . . . . . . . . SYSCAT.SECURITYPOLICYCOMPONENTRULES . . SYSCAT.SECURITYPOLICYEXEMPTIONS . . . . SYSCAT.SURROGATEAUTHIDS . . . . . . . . SYSCAT.SEQUENCEAUTH . . . . . . . . . SYSCAT.SEQUENCES . . . . . . . . . . . SYSCAT.SERVEROPTIONS . . . . . . . . . SYSCAT.SERVERS . . . . . . . . . . . . SYSCAT.STATEMENTS . . . . . . . . . . . SYSCAT.TABAUTH . . . . . . . . . . . . SYSCAT.TABCONST . . . . . . . . . . . . SYSCAT.TABDEP . . . . . . . . . . . . . SYSCAT.TABDETACHEDDEP . . . . . . . . SYSCAT.TABLES . . . . . . . . . . . . . SYSCAT.TABLESPACES . . . . . . . . . . . SYSCAT.TABOPTIONS . . . . . . . . . . . SYSCAT.TBSPACEAUTH . . . . . . . . . . SYSCAT.TRANSFORMS . . . . . . . . . . SYSCAT.TRIGDEP . . . . . . . . . . . . SYSCAT.TRIGGERS . . . . . . . . . . . . SYSCAT.TYPEMAPPINGS . . . . . . . . . . SYSCAT.USEROPTIONS . . . . . . . . . . SYSCAT.VIEWS . . . . . . . . . . . . . SYSCAT.WRAPOPTIONS . . . . . . . . . . SYSCAT.WRAPPERS . . . . . . . . . . . . SYSCAT.XDBMAPGRAPHS . . . . . . . . . SYSCAT.XDBMAPSHREDTREES . . . . . . . . SYSCAT.XSROBJECTAUTH . . . . . . . . . SYSCAT.XSROBJECTCOMPONENTS . . . . . . SYSCAT.XSROBJECTDEP . . . . . . . . . . SYSCAT.XSROBJECTHIERARCHIES . . . . . . SYSCAT.XSROBJECTS . . . . . . . . . . . SYSSTAT.COLDIST . . . . . . . . . . . . SYSSTAT.COLGROUPDIST . . . . . . . . . SYSSTAT.COLGROUPDISTCOUNTS . . . . . . SYSSTAT.COLGROUPS . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

642 643 644 645 646 649 650 651 656 657 658 659 660 661 662 663 664 666 674 676 677 678 679 680 681 682 683 684 685 686 687 689 690 691 692 694 695 697 698 704 706 707 708 709 710 712 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729

x

SQL Reference Volume 1

SYSSTAT.COLUMNS SYSSTAT.INDEXES . SYSSTAT.ROUTINES SYSSTAT.TABLES . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

730 731 734 735

Appendix E. Federated systems . . . . . . . . . . . . . . . . . . . . . . . . 737Valid server types in SQL statements . . . . BioRS wrapper . . . . . . . . . . . BLAST wrapper . . . . . . . . . . CTLIB wrapper . . . . . . . . . . . DRDA wrapper. . . . . . . . . . . Entrez wrapper . . . . . . . . . . . Excel wrapper . . . . . . . . . . . HMMER wrapper . . . . . . . . . . Informix wrapper . . . . . . . . . . MSSQLODBC3 wrapper . . . . . . . . NET8 wrapper . . . . . . . . . . . ODBC wrapper . . . . . . . . . . . OLE DB wrapper . . . . . . . . . . Table-structured files wrapper . . . . . . Teradata wrapper . . . . . . . . . . Web services wrapper . . . . . . . . WebSphere Business Integration wrapper . . XML wrapper . . . . . . . . . . . Nickname column options for federated systems Function mapping options for federated systems Server options for federated systems . . . . User mapping options for federated systems . . Wrapper options for federated systems . . . . Default forward data type mappings . . . . DB2 Database for Linux, UNIX, and Windows DB2 for iSeries data sources . . . . . . DB2 for VM and VSE data sources . . . . DB2 for z/OS data sources . . . . . . . Informix data sources . . . . . . . . Microsoft SQL Server data sources . . . . ODBC data sources . . . . . . . . . Oracle NET8 data sources . . . . . . . Sybase data sources . . . . . . . . . Teradata data sources. . . . . . . . . Default reverse data type mappings . . . . . DB2 Database for Linux, UNIX, and Windows DB2 for iSeries data sources . . . . . . DB2 for VM and VSE data sources . . . . DB2 for z/OS data sources . . . . . . . Informix data sources . . . . . . . . Microsoft SQL Server data sources . . . . Oracle NET8 data sources . . . . . . . Sybase data sources . . . . . . . . . Teradata data sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . data sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . data sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737 737 737 738 738 738 738 738 739 739 739 739 739 739 739 740 740 740 740 747 748 763 765 767 767 768 768 769 769 770 772 773 773 774 775 775 776 776 777 777 778 779 779 780

Appendix F. The SAMPLE database and SQL Reference examples . . . . . . . . . 781 Appendix G. Reserved schema names and reserved words . . . . . . . . . . . . 783 Appendix H. Interaction of triggers and constraints . . . . . . . . . . . . . . . . 787 Appendix I. Explain tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 789Explain tables . . . . . . EXPLAIN_ARGUMENT table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789 . 790

Contents

xi

EXPLAIN_DIAGNOSTIC table . . EXPLAIN_DIAGNOSTIC_DATA table EXPLAIN_INSTANCE table . . . EXPLAIN_OBJECT table. . . . . EXPLAIN_OPERATOR table . . . EXPLAIN_PREDICATE table . . . EXPLAIN_STATEMENT table . . . EXPLAIN_STREAM table . . . . ADVISE_INDEX table . . . . . ADVISE_INSTANCE table . . . . ADVISE_MQT table . . . . . . ADVISE_PARTITION table . . . . ADVISE_TABLE table . . . . . ADVISE_WORKLOAD table . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

795 796 797 800 803 805 808 810 812 815 816 817 818 819

Appendix J. Explain register values . . . . . . . . . . . . . . . . . . . . . . . 821 Appendix K. Exception tables . . . . . . . . . . . . . . . . . . . . . . . . . 827Rules for creating an exception table Handling rows in an exception table Querying exception tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827 . 828 . 829

Appendix L. SQL statements allowed in routines . . . . . . . . . . . . . . . . . 831 Appendix M. CALL invoked from a compiled statement . . . . . . . . . . . . . . 835 Appendix N. Japanese and traditional-Chinese extended UNIX code (EUC) considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841Language elements Characters . . Tokens . . . Identifiers . . Data types . . Constants . . Functions . . . Expressions . . Predicates . . Functions . . . . LENGTH . . . SUBSTR . . . TRANSLATE . VARGRAPHIC . Statements . . . CONNECT . . PREPARE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841 841 841 841 841 843 844 844 844 845 845 845 845 845 846 846 846

Appendix O. Backus-Naur form (BNF) specifications for DATALINKs . . . . . . . . 847 Appendix P. DB2 Database technical information . . . . . . . . . . . . . . . . . 849Overview of the DB2 technical information . . . . . . . . . . . . . . Documentation feedback . . . . . . . . . . . . . . . . . . . DB2 technical library in hardcopy or PDF format . . . . . . . . . . . . Ordering printed DB2 books . . . . . . . . . . . . . . . . . . . Displaying SQL state help from the command line processor . . . . . . . . Accessing different versions of the DB2 Information Center . . . . . . . . . Displaying topics in your preferred language in the DB2 Information Center . . . Updating the DB2 Information Center installed on your computer or intranet server DB2 tutorials . . . . . . . . . . . . . . . . . . . . . . . . DB2 troubleshooting information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849 849 850 852 853 854 854 855 857 857

xii

SQL Reference Volume 1

Terms and Conditions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. 858

Appendix Q. Notices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863 Contacting IBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883

Contents

xiii

xiv

SQL Reference Volume 1

About this bookThe SQL Reference in its two volumes defines the SQL language used by DB2 Database for Linux, UNIX, and Windows. It includes: v Information about relational database concepts, language elements, functions, and the forms of queries (Volume 1). v Information about the syntax and semantics of SQL statements (Volume 2).

Who should use this bookThis book is intended for anyone who wants to use the Structured Query Language (SQL) to access a database. It is primarily for programmers and database administrators, but it can also be used by those who access databases through the command line processor (CLP). This book is a reference rather than a tutorial. It assumes that you will be writing application programs and therefore presents the full functions of the database manager.

How this book is structuredThis book contains information about the following major topics: v Chapter 1, Concepts, on page 1 discusses the basic concepts of relational databases and SQL. v Chapter 2, Language elements, on page 53 describes the basic syntax of SQL and the language elements that are common to many SQL statements. v Chapter 3, Functions, on page 231 contains syntax diagrams, semantic descriptions, rules, and usage examples of SQL column and scalar functions. v Chapter 4, Procedures, on page 495 contains syntax diagrams, semantic descriptions, rules, and usage examples of procedures. v Chapter 5, Queries, on page 505 describes the various forms of a query. v Appendix A, SQL and XQuery limits, on page 559 lists SQL limitations. v Appendix B, SQLCA (SQL communications area), on page 567 describes the SQLCA structure. v Appendix C, SQLDA (SQL descriptor area), on page 573 describes the SQLDA structure. v Appendix D, Catalog views, on page 583 describes the system catalog views. v Appendix E, Federated systems, on page 737 describes options and type mappings for federated systems. v Appendix F, The SAMPLE database and SQL Reference examples, on page 781 introduces the SAMPLE database, which contains the tables that are used in many examples. v Appendix G, Reserved schema names and reserved words, on page 783 contains the reserved schema names and the reserved words for the IBM SQL and ISO/ANSI SQL99 and SQL2003 standards. v Appendix H, Interaction of triggers and constraints, on page 787 discusses the interaction of triggers and referential constraints. v Appendix I, Explain tables, on page 789 describes the explain tables. Copyright IBM Corp. 1993, 2006

xv

How this book is structuredv Appendix J, Explain register values, on page 821 describes the interaction of the CURRENT EXPLAIN MODE and CURRENT EXPLAIN SNAPSHOT special register values with each other and with the PREP and BIND commands. v Appendix K, Exception tables, on page 827 contains information about user-created tables that are used with the SET INTEGRITY statement. v Appendix L, SQL statements allowed in routines, on page 831 lists the SQL statements that are allowed to execute in routines with different SQL data access contexts. v Appendix M, CALL invoked from a compiled statement, on page 835 describes the CALL statement that can be invoked from a compiled statement. v Appendix N, Japanese and traditional-Chinese extended UNIX code (EUC) considerations, on page 841 lists considerations when using extended UNIX code (EUC) character sets. v Appendix O, Backus-Naur form (BNF) specifications for DATALINKs, on page 847 contains the Backus-Naur form (BNF) specifications for DATALINKs.

A brief overview of Volume 2The second volume of the SQL Reference contains information about the syntax and semantics of SQL statements. v Statements contains syntax diagrams, semantic descriptions, rules, and examples of all SQL statements.

How to read the syntax diagramsThroughout this book, syntax is described using the structure defined as follows: Read the syntax diagrams from left to right and top to bottom, following the path of the line. The symbol indicates the beginning of a syntax diagram.

The symbol indicates that the syntax is continued on the next line. The symbol indicates that the syntax is continued from the previous line. The symbol indicates the end of a syntax diagram.

Syntax fragments start with the symbol and end with the symbol. Required items appear on the horizontal line (the main path).required_item

Optional items appear below the main path.required_item optional_item

If an optional item appears above the main path, that item has no effect on execution, and is used only for readability.

xvi

SQL Reference Volume 1

How to read the syntax diagramsoptional_item required_item

If you can choose from two or more items, they appear in a stack. If you must choose one of the items, one item of the stack appears on the main path.required_item required_choice1 required_choice2

If choosing one of the items is optional, the entire stack appears below the main path.required_item optional_choice1 optional_choice2

If one of the items is the default, it will appear above the main path, and the remaining choices will be shown below.default_choice required_item optional_choice optional_choice

An arrow returning to the left, above the main line, indicates an item that can be repeated. In this case, repeated items must be separated by one or more blanks.

required_item

repeatable_item

If the repeat arrow contains a comma, you must separate repeated items with a comma., required_item repeatable_item

A repeat arrow above a stack indicates that you can make more than one choice from the stacked items or repeat a single choice. Keywords appear in uppercase (for example, FROM). They must be spelled exactly as shown. Variables appear in lowercase (for example, column-name). They represent user-supplied names or values in the syntax. If punctuation marks, parentheses, arithmetic operators, or other such symbols are shown, you must enter them as part of the syntax.

About this book

xvii

How to read the syntax diagramsSometimes a single variable represents a larger fragment of the syntax. For example, in the following diagram, the variable parameter-block represents the whole syntax fragment that is labeled parameter-block:required_item parameter-block

parameter-block:parameter1 parameter2

parameter3 parameter4

Adjacent segments occurring between large bullets (*) may be specified in any sequence.required_item item1 * item2 * item3 * item4

The above diagram shows that item2 and item3 may be specified in either order. Both of the following are valid:required_item item1 item2 item3 item4 required_item item1 item3 item2 item4

Conventions used in this manualThis section specifies some conventions that are used consistently throughout this manual.

Error conditionsAn error condition is indicated within the text of the manual by listing the SQLSTATE associated with the error in parentheses. For example:A duplicate signature returns an SQL error (SQLSTATE 42723).

Highlighting conventionsThe following conventions are used in this book.Bold Italics Indicates commands, keywords, and other items whose names are predefined by the system. Indicates one of the following: v Names or values (variables) that must be supplied by the user v General emphasis v The introduction of a new term v A reference to another source of information Monospace Indicates one of the following: v Files and directories v Information that you are instructed to type at a command prompt or in a window v Examples of specific data values v Examples of text similar to what might be displayed by the system v Examples of system messages

xviii

SQL Reference Volume 1

Related documentation

Related documentationThe following publications might prove useful when you are preparing applications: v Administration Guide Contains information required to design, implement, and maintain a database that is to be accessed either locally or in a client/server environment v Getting Started with Database Application Development Provides an introduction to DB2 application development, including platform prerequisites; supported development software; and guidance on the benefits and limitations of the supported programming APIs. v Developing SQL and External Routines Contains information that explains how to design and create databases and database objects including tables, constraints, triggers, views, and user-defined SQL stored procedures and functions. It also explains how to query and modify data, and how to control access to database objects. v DB2 Universal Database for iSeries SQL Reference This book defines SQL as supported by DB2 Query Manager and SQL Development Kit on iSeries (AS/400). It contains reference information for the tasks of system administration, database administration, application programming, and operation. This manual includes syntax, usage notes, keywords, and examples for each of the SQL statements used on iSeries (AS/400) systems running DB2. v DB2 Universal Database for z/OS and OS/390 SQL Reference This book defines SQL used in DB2 for z/OS (OS/390). It provides query forms, SQL statements, SQL procedure statements, DB2 limits, SQLCA, SQLDA, catalog tables, and SQL reserved words for z/OS systems running DB2. v DB2 Spatial Extender Users Guide and Reference This book discusses how to write applications to create and use a geographic information system (GIS). Creating and using a GIS involves supplying a database with resources and then querying the data to obtain information such as locations, distances, and distributions within areas. v IBM SQL Reference This book contains all the common elements of SQL that span IBMs database products. It provides limits and rules that assist in preparing portable programs using IBM databases. This manual provides a list of SQL extensions and incompatibilities among the following standards and products: SQL92E, XPG4-SQL, IBM-SQL, and the IBM relational database products. v American National Standard X3.135-1992, Database Language SQL Contains the ANSI standard definition of SQL. v ISO/IEC 9075:1992, Database Language SQL Contains the 1992 ISO standard definition of SQL. v ISO/IEC 9075-2:1999, Database Language SQL -- Part 2: Foundation (SQL/Foundation) Contains a large portion of the 1999 ISO standard definition of SQL. v ISO/IEC 9075-4:1999, Database Language SQL -- Part 4: Persistent Stored Modules (SQL/PSM) Contains the 1999 ISO standard definition for SQL procedure control statements.About this book

xix

Related documentationv ISO/IEC 9075-5:1999, Database Language SQL -- Part 4: Host Language Bindings (SQL/Bindings) Contains the 1999 ISO standard definition for host language bindings and dynamic SQL.

xx

SQL Reference Volume 1

Chapter 1. ConceptsThis chapter provides a high-level view of concepts that are important to understand when using Structured Query Language (SQL). The reference material contained in the rest of this manual provides a more detailed view.

Relational databasesA relational database is a database that is treated as a set of tables and manipulated in accordance with the relational model of data. It contains a set of objects used to store, manage, and access data. Examples of such objects are tables, views, indexes, functions, triggers, and packages. Objects can be either defined by the system (system-defined objects) or defined by the user (user-defined objects). A partitioned relational database is a relational database whose data is managed across multiple database partitions. This separation of data across database partitions is transparent to users of most SQL statements. However, some data definition language (DDL) statements take database partition information into consideration (for example, CREATE DATABASE PARTITION GROUP). Data definition language is the subset of SQL statements used to describe data relationships in a database. A federated database is a relational database whose data is stored in multiple data sources (such as separate relational databases). The data appears as if it were all in a single large database and can be accessed through traditional SQL queries. Changes to the data can be explicitly directed to the appropriate data source.

Structured Query Language (SQL)SQL is a standardized language for defining and manipulating data in a relational database. In accordance with the relational model of data, the database is treated as a set of tables, relationships are represented by values in tables, and data is retrieved by specifying a result table that can be derived from one or more base tables. SQL statements are executed by a database manager. One of the functions of the database manager is to transform the specification of a result table into a sequence of internal operations that optimize data retrieval. The transformation occurs in two phases: preparation and binding. All executable SQL statements must be prepared before they can be executed. The result of preparation is the executable or operational form of the statement. The method of preparing an SQL statement and the persistence of its operational form distinguish static SQL from dynamic SQL.

Queries and table expressionsA query is a component of certain SQL statements; it specifies a (temporary) result table.

Copyright IBM Corp. 1993, 2006

1

Queries and table expressionsA table expression creates a temporary result table from a simple query. Clauses further refine the result table. For example, you can use a table expression as a query to select all of the managers from several departments, specify that they must have over 15 years of working experience, and be located at the New York branch office. A common table expression is like a temporary view within a complex query. It can be referenced in other places within the query, and can be used in place of a view. Each use of a specific common table expression within a complex query shares the same temporary view. Recursive use of a common table expression within a query can be used to support applications such as airline reservation systems, bill of materials (BOM) generators, and network planning. Related reference: v Select-statement on page 548 v SQL queries on page 505

DB2 Call level interface (CLI) and open database connectivity (ODBC)The DB2 call level interface is an application programming interface that provides functions for processing dynamic SQL statements to application programs. CLI programs can also be compiled using an open database connectivity Software Developers Kit (available from Microsoft or other vendors), which enables access to ODBC data sources. Unlike embedded SQL, this interface requires no precompilation. Applications can be run against a variety of databases without having to be compiled against each of these databases. Applications use procedure calls at run time to connect to databases, issue SQL statements, and retrieve data and status information. The DB2 CLI interface provides many features not available in embedded SQL. For example: v CLI provides function calls that support a way of querying database catalogs that is consistent across the DB2 family. This reduces the need to write catalog queries that must be tailored to specific database servers. v CLI provides the ability to scroll through a cursor: Forward by one or more rows Backward by one or more rows Forward from the first row by one or more rows Backward from the last row by one or more rows From a previously stored location in the cursor. v Stored procedures called from application programs that were written using CLI can return result sets to those programs.

Java database connectivity (JDBC) and embedded SQL for Java (SQLJ) programsDB2 implements two standards-based Java programming APIs: Java database connectivity (JDBC) and embedded SQL for Java (SQLJ). Both can be used to create Java applications and applets that access DB2:

2

SQL Reference Volume 1

Java database connectivity (JDBC) and embedded SQL for Java (SQLJ) programsv JDBC calls are translated into DB2 CLI calls through Java native methods. JDBC requests flow from the DB2 client through DB2 CLI to the DB2 server. JDBC cannot use static SQL. v SQLJ applications use JDBC as a foundation for such tasks as connecting to databases and handling SQL errors, but can also contain embedded static SQL statements in the SQLJ source files. An SQLJ source file must be translated by the SQLJ translator before the resulting Java source code can be compiled.

SchemasA schema is a collection of named objects; it provides a way to group those objects logically. A schema is also a name qualifier; it provides a way to use the same natural name for several objects, and to prevent ambiguous references to those objects; for example, the schema names INTERNAL and EXTERNAL make it easy to distinguish two different SALES tables (INTERNAL.SALES, EXTERNAL.SALES). Schemas also enable multiple applications to store data in a single database without encountering namespace collisions. A schema is distinct from, and should not be confused with, an XML schema, which is a standard that describes the structure and validates the content of XML documents. A schema can contain tables, views, nicknames, triggers, functions, packages, and other objects. A schema is itself a database object. It is explicitly created using the CREATE SCHEMA statement, with the current user or a specified authorization ID recorded as the schema owner. It can also be implicitly created when another object is created, if the user has IMPLICIT_SCHEMA database authority. A schema name is used as the high order part of a two-part object name. If the object is specifically qualified with a schema name when created, the object is assigned to that schema. If no schema name is specified when the object is created, the default schema name is used. For example, a user with DBADM authority creates a schema called C for user A:CREATE SCHEMA C AUTHORIZATION A

User A can then issue the following statement to create a table called X in schema C (provided that user A has the CREATETAB database authority):CREATE TABLE C.X (COL1 INT)

Some schema names are reserved. For example, built-in functions belong to the SYSIBM schema, and the pre-installed user-defined functions belong to the SYSFUN schema. When a database is created, all users have IMPLICIT_SCHEMA authority. This allows any user to create objects in any schema that does not already exist. An implicitly-created schema allows any user to create other objects in this schema. The ability to create aliases, distinct types, functions, and triggers is extended to implicitly-created schemas. The default privileges on an implicitly-created schema provide backward compatibility with previous versions. If IMPLICIT_SCHEMA authority is revoked from PUBLIC, schemas can be explicitly created using the CREATE SCHEMA statement, or implicitly created byChapter 1. Concepts

3

Schemasusers (such as those with DBADM authority) who have been granted IMPLICIT_SCHEMA authority. Although revoking IMPLICIT_SCHEMA authority from PUBLIC increases control over the use of schema names, it can result in authorization errors when existing applications attempt to create objects. Schemas also have privileges, allowing the schema owner to control which users have the privilege to create, alter, copy, and drop objects in the schema. This provides a way to control the manipulation of a subset of objects in the database. A schema owner is initially given all of these privileges on the schema, with the ability to grant the privileges to others. An implicitly-created schema is owned by the system, and all users are initially given the privilege to create objects in such a schema. A user with SYSADM or DBADM authority can change the privileges that are held by users on any schema. Therefore, access to create, alter, copy, and drop objects in any schema (even one that was implicitly created) can be controlled. Related concepts: v Grouping objects by schema in Administration Guide: Implementation v Schema privileges in Administration Guide: Implementation Related tasks: v Creating a schema in Administration Guide: Implementation Related reference: v Appendix G, Reserved schema names and reserved words, on page 783

TablesTables are logical structures maintained by the database manager. Tables are made up of columns and rows. The rows are not necessarily ordered within a table (order is determined by the application program). At the intersection of every column and row is a specific data item called a value. A column is a set of values of the same type or one of its subtypes. A row is a sequence of values arranged so that the nth value is a value of the nth column of the table. A base table is created with the CREATE TABLE statement and is used to hold persistent user data. A result table is a set of rows that the database manager selects or generates from one or more base tables to satisfy a query. A summary table is a table defined by a query that is also used to determine the data in the table. Summary tables can be used to improve the performance of queries. If the database manager determines that a portion of a query can be resolved using a summary table, the database manager can rewrite the query to use the summary table. This decision is based on database configuration settings, such as the CURRENT REFRESH AGE and the CURRENT QUERY OPTIMIZATION special registers. A table can define the data type of each column separately, or base the types on the attributes of a user-defined structured type. This is called a typed table. A user-defined structured type may be part of a type hierarchy. A subtype inherits attributes from its supertype. Similarly, a typed table can be part of a table hierarchy. A subtable inherits columns from its supertable. Note that the term subtype applies to a user-defined structured type and all user-defined structured types that are below it in the type hierarchy. A proper subtype of a structured type T is a structured type below T in the type hierarchy. Similarly, the term subtable applies to

4

SQL Reference Volume 1

Tablesa typed table and all typed tables that are below it in the table hierarchy. A proper subtable of a table T is a table below T in the table hierarchy. A declared temporary table is created with a DECLARE GLOBAL TEMPORARY TABLE statement and is used to hold temporary data on behalf of a single application. This table is dropped implicitly when the application disconnects from the database.

KeysA key is a set of columns that can be used to identify or access a particular row or rows. The key is identified in the description of a table, index, or referential constraint. The same column can be part of more than one key. A key that is composed of more than one column is called a composite key. In a table with a composite key, the order of the columns within the composite key is not constrained by the order of the columns within the table. The value of a composite key denotes a composite value. Thus, a rule such as the value of the foreign key must be equal to the value of the primary key means that each component of the value of the foreign key must be equal to the corresponding component of the value of the primary key. A unique key is a key that is constrained so that no two of its values are equal. The columns of a unique key cannot contain null values. The constraint is enforced by the database manager during the execution of any operation that changes data values, such as INSERT or UPDATE. The mechanism used to enforce the constraint is called a unique index. Thus, every unique key is a key of a unique index. Such an index is also said to have the UNIQUE attribute. A primary key is a special case of a unique key. A table cannot have more than one primary key. A foreign key is a key that is specified in the definition of a referential constraint. A distribution key is a key that is part of the definition of a table in a partitioned database. The distribution key is used to determine the database partition on which the row of data is stored. If a distribution key is defined, unique keys and primary keys must include the same columns as the distribution key, but can have additional columns. A table cannot have more than one distribution key. A table partitioning key is an ordered set of one or more columns in a table. The values in the table partitioning key columns are used to determine the data partition to which each table row belongs.

ConstraintsA constraint is a rule that the database manager enforces. There are four types of constraints: v A unique constraint is a rule that forbids duplicate values in one or more columns within a table. Unique and primary keys are the supported unique constraints. For example, a unique constraint can be defined on the supplier identifier in the supplier table to ensure that the same supplier identifier is not given to two suppliers.Chapter 1. Concepts

5

Constraintsv A referential constraint is a logical rule about values in one or more columns in one or more tables. For example, a set of tables shares information about a corporations suppliers. Occasionally, a suppliers name changes. You can define a referential constraint stating that the ID of the supplier in a table must match a supplier ID in the supplier information. This constraint prevents insert, update, or delete operations that would otherwise result in missing supplier information. v A table check constraint sets restrictions on data added to a specific table. For example, a table check constraint can ensure that the salary level for an employee is at least $20,000 whenever salary data is added or updated in a table containing personnel information. v An informational constraint is a rule that can be used by the SQL compiler, but that is not enforced by the database manager. Referential and table check constraints can be turned on or off. It is generally a good idea, for example, to turn off the enforcement of a constraint when large amounts of data are loaded into a database.

Unique constraintsA unique constraint is the rule that the values of a key are valid only if they are unique within a table. Unique constraints are optional and can be defined in the CREATE TABLE or ALTER TABLE statement using the PRIMARY KEY clause or the UNIQUE clause. The columns specified in a unique constraint must be defined as NOT NULL. The database manager uses a unique index to enforce the uniqueness of the key during changes to the columns of the unique constraint. A table can have an arbitrary number of unique constraints, with at most one unique constraint defined as the primary key. A table cannot have more than one unique constraint on the same set of columns. A unique constraint that is referenced by the foreign key of a referential constraint is called the parent key. When a unique constraint is defined in a CREATE TABLE statement, a unique index is automatically created by the database manager and designated as a primary or unique system-required index. When a unique constraint is defined in an ALTER TABLE statement and an index exists on the same columns, that index is designated as unique and system-required. If such an index does not exist, the unique index is automatically created by the database manager and designated as a primary or unique system-required index. Note that there is a distinction between defining a unique constraint and creating a unique index. Although both enforce uniqueness, a unique index allows nullable columns and generally cannot be used as a parent key.

Referential constraintsReferential integrity is the state of a database in which all values of all foreign keys are valid. A foreign keyis a column or a set of columns in a table whose values are required to match at least one primary key or unique key value of a row in its parent table. A referential constraint is the rule that the values of the foreign key are valid only if one of the following conditions is true: v They appear as values of a parent key. v Some component of the foreign key is null.

6

SQL Reference Volume 1

ConstraintsThe table containing the parent key is called the parent table of the referential constraint, and the table containing the foreign key is said to be a dependent of that table. Referential constraints are optional and can be defined in the CREATE TABLE statement or the ALTER TABLE statement. Referential constraints are enforced by the database manager during the execution of INSERT, UPDATE, DELETE, ALTER TABLE, ADD CONSTRAINT, and SET INTEGRITY statements. Referential constraints with a delete or an update rule of RESTRICT are enforced before all other referential constraints. Referential constraints with a delete or an update rule of NO ACTION behave like RESTRICT in most cases. Note that referential constraints, check constraints, and triggers can be combined. Referential integrity rules involve the following concepts and terminology: Parent key A primary key or a unique key of a referential constraint. Parent row A row that has at least one dependent row. Parent table A table that contains the parent key of a referential constraint. A table can be a parent in an arbitrary number of referential constraints. A table that is the parent in a referential constraint can also be the dependent in a referential constraint. Dependent table A table that contains at least one referential constraint in its definition. A table can be a dependent in an arbitrary number of referential constraints. A table that is the dependent in a referential constraint can also be the parent in a referential constraint. Descendent table A table is a descendent of table T if it is a dependent of T or a descendent of a dependent of T. Dependent row A row that has at least one parent row. Descendent row A row is a descendent of row r if it is a dependent of r or a descendent of a dependent of r. Referential cycle A set of referential constraints such that each table in the set is a descendent of itself. Self-referencing table A table that is a parent and a dependent in the same referential constraint. The constraint is called a self-referencing constraint. Self-referencing row A row that is a parent of itself.

Insert ruleThe insert rule of a referential constraint is that a non-null insert value of the foreign key must match some value of the parent key of the parent table. The

Chapter 1. Concepts

7

Constraintsvalue of a composite foreign key is null if any component of the value is null. This rule is implicit when a foreign key is specified.

Update ruleThe update rule of a referential constraint is specified when the referential constraint is defined. The choices are NO ACTION and RESTRICT. The update rule applies when a row of the parent or a row of the dependent table is updated. In the case of a parent row, when a value in a column of the parent key is updated, the following rules apply: v If any row in the dependent table matches the original value of the key, the update is rejected when the update rule is RESTRICT. v If any row in the dependent table does not have a corresponding parent key when the update statement is completed (excluding AFTER triggers), the update is rejected when the update rule is NO ACTION. In the case of a dependent row, the NO ACTION update rule is implicit when a foreign key is specified. NO ACTION means that a non-null update value of a foreign key must match some value of the parent key of the parent table when the update statement is completed. The value of a composite foreign key is null if any component of the value is null.

Delete ruleThe delete rule of a referential constraint is specified when the referential constraint is defined. The choices are NO ACTION, RESTRICT, CASCADE, or SET NULL. SET NULL can be specified only if some column of the foreign key allows null values. The delete rule of a referential constraint applies when a row of the parent table is deleted. More precisely, the rule applies when a row of the parent table is the object of a delete or propagated delete operation (defined below), and that row has dependents in the dependent table of the referential constraint. Consider an example where P is the parent table, D is the dependent table, and p is a parent row that is the object of a delete or propagated delete operation. The delete rule works as follows: v With RESTRICT or NO ACTION, an error occurs and no rows are deleted. v With CASCADE, the delete operation is propagated to the dependents of p in table D. v With SET NULL, each nullable column of the foreign key of each dependent of p in table D is set to null. Each referential constraint in which a table is a parent has its own delete rule, and all applicable delete rules are used to determine the result of a delete operation. Thus, a row cannot be deleted if it has dependents in a referential constraint with a delete rule of RESTRICT or NO ACTION, or the deletion cascades to any of its descendents that are dependents in a referential constraint with the delete rule of RESTRICT or NO ACTION. The deletion of a row from parent table P involves other tables and can affect rows of these tables: v If table D is a dependent of P and the delete rule is RESTRICT or NO ACTION, then D is involved in the operation but is not affected by the operation.

8

SQL Reference Volume 1

Constraintsv If D is a dependent of P and the delete rule is SET NULL, then D is involved in the operation, and rows of D can be updated during the operation. v If D is a dependent of P and the delete rule is CASCADE, then D is involved in the operation and rows of D can be deleted during the operation. If rows of D are deleted, then the delete operation on P is said to be propagated to D. If D is also a parent table, then the actions described in this list apply, in turn, to the dependents of D. Any table that can be involved in a delete operation on P is said to be delete-connected to P. Thus, a table is delete-connected to table P if it is a dependent of P, or a dependent of a table to which delete operations from P cascade. The following restrictions apply to delete-connected relationships: v When a table is delete-connected to itself in a referential cycle of more than one table, the cycle must not contain a delete rule of either RESTRICT or SET NULL. v A table must not both be a dependent table in a CASCADE relationship (self-referencing or referencing another table) and have a self-referencing relationship with a delete rule of either RESTRICT or SET NULL. v When a table is delete-connected to another table through multiple relationships where such relationships have overlapping foreign keys, these relationships must have the same delete rule and none of these can be SET NULL. v When a table is delete-connected to another table through multiple relationships where one of the relationships is specified with delete rule SET NULL, the foreign key definition of this relationship must not contain any distribution key or MDC key column. v When two tables are delete-connected to the same table through CASCADE relationships, the two tables must not be delete-connected to each other where the delete connected paths end with delete rule RESTRICT or SET NULL.

Table check constraintsA table check constraint is a rule that specifies the values allowed in one or more columns of every row in a table. A constraint is optional, and can be defined using the CREATE TABLE or the ALTER TABLE statement. Specifying table check constraints is done through a restricted form of a search condition. One of the restrictions is that a column name in a table check constraint on table T must identify a column of table T. A table can have an arbitrary number of table check constraints. A table check constraint is enforced by applying its search condition to each row that is inserted or updated. An error occurs if the result of the search condition is false for any row. When one or more table check constraints is defined in the ALTER TABLE statement for a table with existing data, the existing data is checked against the new condition before the ALTER TABLE statement completes. The SET INTEGRITY statement can be used to put the table in set integrity pending state, which allows the ALTER TABLE statement to proceed without checking the data.

Informational constraintsAn informational constraint is a rule that can be used by the SQL compiler to improve the access path to data. Informational constraints are not enforced by the database manager, and are not u