1285
IBM DB2 10.5 for Linux, UNIX, and Windows SQL Reference Volume 1 Updated October, 2014 SC27-5509-01

SQL Reference Volume 1 - IBMpublic.dhe.ibm.com/ps/products/db2/info/vr105/pdf/en_US/DB2SQLR… · SQL ReferenceVolume 1 Updated October,2014 SC27-5509-01. IBM DB2 10.5 for Linux,UNIX,andWindows

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

  • IBM DB2 10.5for Linux, UNIX, and Windows

    SQL Reference Volume 1Updated October, 2014

    SC27-5509-01

    ���

  • IBM DB2 10.5for Linux, UNIX, and Windows

    SQL Reference Volume 1Updated October, 2014

    SC27-5509-01

    ���

  • NoteBefore using this information and the product it supports, read the general information under Appendix M, “Notices,” onpage 1237.

    Edition Notice

    This document contains proprietary information of IBM. It is provided under a license agreement and is protectedby copyright law. The information contained in this publication does not include any product warranties, and anystatements 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 http://www.ibm.com/shop/publications/

    order

    v To find your local IBM representative, go to the IBM Directory of Worldwide Contacts at http://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 anyway it believes appropriate without incurring any obligation to you.

    © Copyright IBM Corporation 1993, 2014.US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contractwith IBM Corp.

    http://www.ibm.com/shop/publications/orderhttp://www.ibm.com/shop/publications/orderhttp://www.ibm.com/planetwide/http://www.ibm.com/planetwide/

  • Contents

    About this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvWho should use this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvHow this book is structured . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvHow to read the syntax diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiConventions used in this manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

    Error conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixHighlighting conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixConventions describing Unicode data . . . . . . . . . . . . . . . . . . . . . . . . . xix

    Related documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

    Chapter 1. Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Structured Query Language (SQL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Queries and table expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Introduction to DB2 Call Level Interface and ODBC . . . . . . . . . . . . . . . . . . . . . . 2Java application development for IBM data servers . . . . . . . . . . . . . . . . . . . . . . 4Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

    Types of tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Authorization, privileges, and object ownership . . . . . . . . . . . . . . . . . . . . . . . 14System catalog views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Application processes, concurrency, and recovery. . . . . . . . . . . . . . . . . . . . . . . 20Isolation levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Table spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Character conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Multicultural support and SQL statements . . . . . . . . . . . . . . . . . . . . . . . . . 31Connecting to distributed relational databases . . . . . . . . . . . . . . . . . . . . . . . . 32

    Remote unit of work for distributed relational databases . . . . . . . . . . . . . . . . . . . 33Application-directed distributed unit of work . . . . . . . . . . . . . . . . . . . . . . . 36Application process connection states. . . . . . . . . . . . . . . . . . . . . . . . . . 37Connection states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Options that govern unit of work semantics . . . . . . . . . . . . . . . . . . . . . . . 40Data representation considerations. . . . . . . . . . . . . . . . . . . . . . . . . . . 41

    Event monitors that write to tables, files, and pipes . . . . . . . . . . . . . . . . . . . . . . 41Database partitioning across multiple database partitions . . . . . . . . . . . . . . . . . . . . 42Large object behavior in partitioned tables . . . . . . . . . . . . . . . . . . . . . . . . . 43DB2 federated systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

    Federated systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44What is a data source?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45The federated database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45The SQL compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Wrappers and wrapper modules . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Server definitions and server options . . . . . . . . . . . . . . . . . . . . . . . . . . 47User mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Nicknames and data source objects . . . . . . . . . . . . . . . . . . . . . . . . . . 48Nickname column options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Data type mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50The federated server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50Supported data sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51The federated database system catalog . . . . . . . . . . . . . . . . . . . . . . . . . 53

    © Copyright IBM Corp. 1993, 2014 iii

  • The query optimizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54Collating sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

    Chapter 2. Language elements . . . . . . . . . . . . . . . . . . . . . . . . . 57Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Data types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

    Data type list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Promotion of data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Casting between data types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Assignments and comparisons. . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Rules for result data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Rules for string conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151String comparisons in a Unicode database . . . . . . . . . . . . . . . . . . . . . . . . 152Resolving the anchor object for an anchored type . . . . . . . . . . . . . . . . . . . . . 154Resolving the anchor object for an anchored row type . . . . . . . . . . . . . . . . . . . . 156Database partition-compatible data types . . . . . . . . . . . . . . . . . . . . . . . . 158

    Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160Special registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

    CURRENT CLIENT_ACCTNG . . . . . . . . . . . . . . . . . . . . . . . . . . . 169CURRENT CLIENT_APPLNAME . . . . . . . . . . . . . . . . . . . . . . . . . . 170CURRENT CLIENT_USERID . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171CURRENT CLIENT_WRKSTNNAME . . . . . . . . . . . . . . . . . . . . . . . . . 172CURRENT DATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173CURRENT DBPARTITIONNUM . . . . . . . . . . . . . . . . . . . . . . . . . . . 174CURRENT DECFLOAT ROUNDING MODE . . . . . . . . . . . . . . . . . . . . . . . 175CURRENT DEFAULT TRANSFORM GROUP. . . . . . . . . . . . . . . . . . . . . . . 176CURRENT DEGREE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177CURRENT EXPLAIN MODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178CURRENT EXPLAIN SNAPSHOT . . . . . . . . . . . . . . . . . . . . . . . . . . 179CURRENT FEDERATED ASYNCHRONY . . . . . . . . . . . . . . . . . . . . . . . . 180CURRENT IMPLICIT XMLPARSE OPTION . . . . . . . . . . . . . . . . . . . . . . . 181CURRENT ISOLATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182CURRENT LOCALE LC_MESSAGES . . . . . . . . . . . . . . . . . . . . . . . . . 183CURRENT LOCALE LC_TIME . . . . . . . . . . . . . . . . . . . . . . . . . . . 184CURRENT LOCK TIMEOUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185CURRENT MAINTAINED TABLE TYPES FOR OPTIMIZATION . . . . . . . . . . . . . . . . 186CURRENT MDC ROLLOUT MODE . . . . . . . . . . . . . . . . . . . . . . . . . . 187CURRENT MEMBER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188CURRENT OPTIMIZATION PROFILE . . . . . . . . . . . . . . . . . . . . . . . . . 189CURRENT PACKAGE PATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190CURRENT PATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191CURRENT QUERY OPTIMIZATION . . . . . . . . . . . . . . . . . . . . . . . . . 192CURRENT REFRESH AGE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193CURRENT SCHEMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194CURRENT SERVER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195CURRENT SQL_CCFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196CURRENT TEMPORAL BUSINESS_TIME . . . . . . . . . . . . . . . . . . . . . . . . 197CURRENT TEMPORAL SYSTEM_TIME . . . . . . . . . . . . . . . . . . . . . . . . 199CURRENT TIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201CURRENT TIMESTAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202CURRENT TIMEZONE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203CURRENT USER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204SESSION_USER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205SYSTEM_USER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206USER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

    Global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Types of global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Authorization required for global variables . . . . . . . . . . . . . . . . . . . . . . . 209Resolution of global variable references. . . . . . . . . . . . . . . . . . . . . . . . . 209

    iv SQL Reference Volume 1

  • Using global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228Conservative binding semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

    Datetime operations and durations . . . . . . . . . . . . . . . . . . . . . . . . . . 252CASE expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257CAST specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260Field reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265XMLCAST specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266ARRAY element specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268Array constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Dereference operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271Method invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273OLAP specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275ROW CHANGE expression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284Sequence reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286Subtype treatment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Determining data types of untyped expressions . . . . . . . . . . . . . . . . . . . . . . 291

    Row expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

    Search conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300Basic predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303Quantified predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305ARRAY_EXISTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308BETWEEN predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309Cursor predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310EXISTS predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312IN predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313LIKE predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315NULL predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320Trigger event predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321TYPE predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322VALIDATED predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323XMLEXISTS predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

    Chapter 3. Built-in global variables . . . . . . . . . . . . . . . . . . . . . . . 329CLIENT_HOST global variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330CLIENT_IPADDR global variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . 331CLIENT_ORIGUSERID global variable . . . . . . . . . . . . . . . . . . . . . . . . . . 332CLIENT_USRSECTOKEN global variable . . . . . . . . . . . . . . . . . . . . . . . . . 333MON_INTERVAL_ID global variable . . . . . . . . . . . . . . . . . . . . . . . . . . 334NLS_STRING_UNITS global variable . . . . . . . . . . . . . . . . . . . . . . . . . . 335PACKAGE_NAME global variable . . . . . . . . . . . . . . . . . . . . . . . . . . . 336PACKAGE_SCHEMA global variable . . . . . . . . . . . . . . . . . . . . . . . . . . 337PACKAGE_VERSION global variable . . . . . . . . . . . . . . . . . . . . . . . . . . 338ROUTINE_MODULE global variable . . . . . . . . . . . . . . . . . . . . . . . . . . 339ROUTINE_SCHEMA global variable . . . . . . . . . . . . . . . . . . . . . . . . . . 340ROUTINE_SPECIFIC_NAME global variable . . . . . . . . . . . . . . . . . . . . . . . . 341ROUTINE_TYPE global variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342TRUSTED_CONTEXT global variable . . . . . . . . . . . . . . . . . . . . . . . . . . 343

    Chapter 4. Built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . 345Aggregate functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357

    ARRAY_AGG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359AVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363CORRELATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365COUNT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366COUNT_BIG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367COVARIANCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

    Contents v

  • GROUPING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370LISTAGG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376Regression functions (REGR_AVGX, REGR_AVGY, REGR_COUNT, ...) . . . . . . . . . . . . . . 377STDDEV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380SUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381VARIANCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382XMLAGG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383XMLGROUP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

    Scalar functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387ABS or ABSVAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389ACOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390ADD_MONTHS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391ARRAY_DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393ARRAY_FIRST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394ARRAY_LAST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395ARRAY_NEXT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396ARRAY_PRIOR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397ASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398ASIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399ATAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400ATAN2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401ATANH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402BIGINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403BITAND, BITANDNOT, BITOR, BITXOR, and BITNOT . . . . . . . . . . . . . . . . . . . 405BLOB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407CARDINALITY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408CEILING or CEIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409CHAR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410CHARACTER_LENGTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417CHR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419CLOB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420COALESCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421COLLATION_KEY_BIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422COMPARE_DECFLOAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424CONCAT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426COS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427COSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428COT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429CURSOR_ROWCOUNT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430DATAPARTITIONNUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431DATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432DAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433DAYNAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434DAYOFWEEK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436DAYOFWEEK_ISO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437DAYOFYEAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438DAYS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439DBCLOB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440DBPARTITIONNUM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441DECFLOAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443DECFLOAT_FORMAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445DECIMAL or DEC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447DECODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451DECRYPT_BIN and DECRYPT_CHAR . . . . . . . . . . . . . . . . . . . . . . . . . 453DEGREES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455DEREF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456DIFFERENCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457DIGITS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458DOUBLE_PRECISION or DOUBLE . . . . . . . . . . . . . . . . . . . . . . . . . . 459

    vi SQL Reference Volume 1

  • EMPTY_BLOB, EMPTY_CLOB, EMPTY_DBCLOB, and EMPTY_NCLOB . . . . . . . . . . . . . 461ENCRYPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462EVENT_MON_STATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465EXP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466EXTRACT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467FLOAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469FLOOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470GENERATE_UNIQUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471GETHINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473GRAPHIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474GREATEST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481HASHEDVALUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482HEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484HEXTORAW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486HOUR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487IDENTITY_VAL_LOCAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488INITCAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494INSTR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498INSTR2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499INSTR4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500INSTRB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501INTEGER or INT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502JULIAN_DAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504LAST_DAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505LCASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506LCASE (locale sensitive) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507LCASE (SYSFUN schema) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508LEAST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509LEFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510LENGTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513LENGTH2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515LENGTH4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516LENGTHB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517LN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518LOCATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519LOCATE_IN_STRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523LOG10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526LONG_VARCHAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527LONG_VARGRAPHIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528LOWER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529LOWER (locale sensitive) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530LPAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532LTRIM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535LTRIM (SYSFUN schema) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538MAX_CARDINALITY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539MICROSECOND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540MIDNIGHT_SECONDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541MIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542MINUTE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543MOD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544MOD (SYSFUN schema). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546MONTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547MONTHNAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548MONTHS_BETWEEN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550MULTIPLY_ALT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552NCHAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554NCLOB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556NVARCHAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557NEXT_DAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559

    Contents vii

  • NORMALIZE_ DECFLOAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561NULLIF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562NVL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563NVL2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564OCTET_LENGTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565OVERLAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566PARAMETER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570POSITION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571POSSTR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574POWER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576QUANTIZE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577QUARTER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579RADIANS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580RAISE_ERROR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581RAND. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582REAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583REC2XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585REPEAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589REPLACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590REPLACE (SYSFUN schema) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594RID_BIT and RID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595RIGHT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597ROUND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600ROUND_TIMESTAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606RPAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608RTRIM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611RTRIM (SYSFUN schema) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613SECLABEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614SECLABEL_BY_NAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615SECLABEL_TO_CHAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616SECOND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618SIGN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620SIN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621SINH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622SMALLINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623SOUNDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624SPACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625SQRT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626STRIP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627SUBSTR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628SUBSTR2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631SUBSTR4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635SUBSTRB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639SUBSTRING. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642TABLE_NAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644TABLE_SCHEMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645TAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647TANH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648TIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649TIMESTAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650TIMESTAMP_FORMAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652TIMESTAMP_ISO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659TIMESTAMPDIFF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660TO_CHAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662TO_CLOB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663TO_DATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664TO_NCHAR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665TO_NCLOB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666TO_NUMBER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667TO_SINGLE_BYTE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668TO_TIMESTAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669

    viii SQL Reference Volume 1

  • TOTALORDER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670TRANSLATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672TRIM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675TRIM_ARRAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677TRUNC_TIMESTAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678TRUNCATE or TRUNC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680TYPE_ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683TYPE_NAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684TYPE_SCHEMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685UCASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686UCASE (locale sensitive) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687UPPER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688UPPER (locale sensitive). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689VALUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691VARCHAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692VARCHAR_BIT_FORMAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698VARCHAR_FORMAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700VARCHAR_FORMAT_BIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708VARGRAPHIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709VERIFY_GROUP_FOR_USER . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715VERIFY_ROLE_FOR_USER. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716VERIFY_TRUSTED_CONTEXT_ROLE_FOR_USER . . . . . . . . . . . . . . . . . . . . . 717WEEK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718WEEK_ISO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719XMLATTRIBUTES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720XMLCOMMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722XMLCONCAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723XMLDOCUMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724XMLELEMENT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726XMLFOREST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733XMLNAMESPACES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736XMLPARSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738XMLPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741XMLQUERY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742XMLROW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745XMLSERIALIZE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747XMLTEXT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749XMLVALIDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751XMLXSROBJECTID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756XSLTRANSFORM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757YEAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761

    Table functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761BASE_TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762UNNEST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764XMLTABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766

    User-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770

    Chapter 5. Built-in procedures . . . . . . . . . . . . . . . . . . . . . . . . . 773XSR_ADDSCHEMADOC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773XSR_COMPLETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775XSR_DTD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776XSR_EXTENTITY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777XSR_REGISTER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778XSR_UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780

    Chapter 6. SQL queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783Queries and table expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783subselect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785

    select-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787from-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791

    Contents ix

  • where-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816group-by-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817having-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831order-by-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832fetch-first-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835isolation-clause (subselect query) . . . . . . . . . . . . . . . . . . . . . . . . . . . 836Examples of subselect queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838

    fullselect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840Examples of fullselect queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844

    select-statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846common-table-expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847update-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853read-only-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854optimize-for-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855isolation-clause (select-statement query) . . . . . . . . . . . . . . . . . . . . . . . . 856lock-request-clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857concurrent-access-resolution-clause . . . . . . . . . . . . . . . . . . . . . . . . . . 858Examples of select-statement queries . . . . . . . . . . . . . . . . . . . . . . . . . 859

    Appendix A. SQL and XML limits . . . . . . . . . . . . . . . . . . . . . . . . 861

    Appendix B. SQLCA (SQL communications area) . . . . . . . . . . . . . . . . . 875

    Appendix C. SQLDA (SQL descriptor area) . . . . . . . . . . . . . . . . . . . . 881

    Appendix D. Catalog views . . . . . . . . . . . . . . . . . . . . . . . . . . 891Road map to the catalog views . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894SYSCAT.ATTRIBUTES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899SYSCAT.AUDITPOLICIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901SYSCAT.AUDITUSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903SYSCAT.BUFFERPOOLDBPARTITIONS . . . . . . . . . . . . . . . . . . . . . . . . . 904SYSCAT.BUFFERPOOLEXCEPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . 905SYSCAT.BUFFERPOOLS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906SYSCAT.CASTFUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907SYSCAT.CHECKS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908SYSCAT.COLAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910SYSCAT.COLCHECKS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911SYSCAT.COLDIST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912SYSCAT.COLGROUPCOLS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913SYSCAT.COLGROUPDIST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 914SYSCAT.COLGROUPDISTCOUNTS . . . . . . . . . . . . . . . . . . . . . . . . . . . 915SYSCAT.COLGROUPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 916SYSCAT.COLIDENTATTRIBUTES . . . . . . . . . . . . . . . . . . . . . . . . . . . 917SYSCAT.COLOPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918SYSCAT.COLUMNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919SYSCAT.COLUSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925SYSCAT.CONDITIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926SYSCAT.CONSTDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927SYSCAT.CONTEXTATTRIBUTES . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928SYSCAT.CONTEXTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929SYSCAT.CONTROLDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930SYSCAT.CONTROLS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931SYSCAT.DATAPARTITIONEXPRESSION . . . . . . . . . . . . . . . . . . . . . . . . . 933SYSCAT.DATAPARTITIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934SYSCAT.DATATYPEDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937SYSCAT.DATATYPES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938SYSCAT.DBAUTH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942SYSCAT.DBPARTITIONGROUPDEF . . . . . . . . . . . . . . . . . . . . . . . . . . . 944SYSCAT.DBPARTITIONGROUPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945SYSCAT.EVENTMONITORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946

    x SQL Reference Volume 1

  • SYSCAT.EVENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948SYSCAT.EVENTTABLES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949SYSCAT.FULLHIERARCHIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951SYSCAT.FUNCMAPOPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952SYSCAT.FUNCMAPPARMOPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . 953SYSCAT.FUNCMAPPINGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954SYSCAT.HIERARCHIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955SYSCAT.HISTOGRAMTEMPLATEBINS . . . . . . . . . . . . . . . . . . . . . . . . . 956SYSCAT.HISTOGRAMTEMPLATES . . . . . . . . . . . . . . . . . . . . . . . . . . . 957SYSCAT.HISTOGRAMTEMPLATEUSE . . . . . . . . . . . . . . . . . . . . . . . . . . 958SYSCAT.INDEXAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959SYSCAT.INDEXCOLUSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960SYSCAT.INDEXDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961SYSCAT.INDEXES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963SYSCAT.INDEXEXPLOITRULES . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970SYSCAT.INDEXEXTENSIONDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971SYSCAT.INDEXEXTENSIONMETHODS . . . . . . . . . . . . . . . . . . . . . . . . . 973SYSCAT.INDEXEXTENSIONPARMS. . . . . . . . . . . . . . . . . . . . . . . . . . . 974SYSCAT.INDEXEXTENSIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975SYSCAT.INDEXOPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976SYSCAT.INDEXPARTITIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977SYSCAT.INDEXXMLPATTERNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 980SYSCAT.INVALIDOBJECTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981SYSCAT.KEYCOLUSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 982SYSCAT.MEMBERSUBSETATTRS. . . . . . . . . . . . . . . . . . . . . . . . . . . . 983SYSCAT.MEMBERSUBSETMEMBERS . . . . . . . . . . . . . . . . . . . . . . . . . . 984SYSCAT.MEMBERSUBSETS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985SYSCAT.MODULEAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986SYSCAT.MODULEOBJECTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 987SYSCAT.MODULES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 988SYSCAT.NAMEMAPPINGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989SYSCAT.NICKNAMES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 990SYSCAT.PACKAGEAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993SYSCAT.PACKAGEDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994SYSCAT.PACKAGES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 996SYSCAT.PARTITIONMAPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1004SYSCAT.PASSTHRUAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005SYSCAT.PERIODS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006SYSCAT.PREDICATESPECS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007SYSCAT.REFERENCES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1008SYSCAT.ROLEAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009SYSCAT.ROLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1010SYSCAT.ROUTINEAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011SYSCAT.ROUTINEDEP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1012SYSCAT.ROUTINEOPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1014SYSCAT.ROUTINEPARMOPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . 1015SYSCAT.ROUTINEPARMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1016SYSCAT.ROUTINES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019SYSCAT.ROUTINESFEDERATED . . . . . . . . . . . . . . . . . . . . . . . . . . . 1029SYSCAT.ROWFIELDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031SYSCAT.SCHEMAAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032SYSCAT.SCHEMATA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033SYSCAT.SCPREFTBSPACES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1034SYSCAT.SECURITYLABELACCESS. . . . . . . . . . . . . . . . . . . . . . . . . . . 1035SYSCAT.SECURITYLABELCOMPONENTELEMENTS . . . . . . . . . . . . . . . . . . . . 1036SYSCAT.SECURITYLABELCOMPONENTS . . . . . . . . . . . . . . . . . . . . . . . . 1037SYSCAT.SECURITYLABELS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1038SYSCAT.SECURITYPOLICIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1039SYSCAT.SECURITYPOLICYCOMPONENTRULES . . . . . . . . . . . . . . . . . . . . . . 1040SYSCAT.SECURITYPOLICYEXEMPTIONS . . . . . . . . . . . . . . . . . . . . . . . . 1041SYSCAT.SEQUENCEAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1042

    Contents xi

  • SYSCAT.SEQUENCES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1043SYSCAT.SERVEROPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045SYSCAT.SERVERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1046SYSCAT.SERVICECLASSES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047SYSCAT.STATEMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049SYSCAT.STOGROUPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1050SYSCAT.STATEMENTTEXTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1051SYSCAT.SURROGATEAUTHIDS . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052SYSCAT.TABAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1053SYSCAT.TABCONST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055SYSCAT.TABDEP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056SYSCAT.TABDETACHEDDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058SYSCAT.TABLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059SYSCAT.TABLESPACES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067SYSCAT.TABOPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069SYSCAT.TBSPACEAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1070SYSCAT.THRESHOLDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071SYSCAT.TRANSFORMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074SYSCAT.TRIGDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1075SYSCAT.TRIGGERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077SYSCAT.TYPEMAPPINGS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1080SYSCAT.USAGELISTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083SYSCAT.USEROPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1084SYSCAT.VARIABLEAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1085SYSCAT.VARIABLEDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1086SYSCAT.VARIABLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1088SYSCAT.VIEWS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1090SYSCAT.WORKACTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1091SYSCAT.WORKACTIONSETS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1094SYSCAT.WORKCLASSATTRIBUTES . . . . . . . . . . . . . . . . . . . . . . . . . . 1095SYSCAT.WORKCLASSES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096SYSCAT.WORKCLASSSETS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1097SYSCAT.WORKLOADAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1098SYSCAT.WORKLOADCONNATTR. . . . . . . . . . . . . . . . . . . . . . . . . . . 1099SYSCAT.WORKLOADS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1100SYSCAT.WRAPOPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1103SYSCAT.WRAPPERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1104SYSCAT.XDBMAPGRAPHS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1105SYSCAT.XDBMAPSHREDTREES . . . . . . . . . . . . . . . . . . . . . . . . . . . 1106SYSCAT.XMLSTRINGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1107SYSCAT.XSROBJECTAUTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1108SYSCAT.XSROBJECTCOMPONENTS . . . . . . . . . . . . . . . . . . . . . . . . . . 1109SYSCAT.XSROBJECTDEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1110SYSCAT.XSROBJECTDETAILS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1112SYSCAT.XSROBJECTHIERARCHIES . . . . . . . . . . . . . . . . . . . . . . . . . . 1113SYSCAT.XSROBJECTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1114SYSIBM.SYSDUMMY1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1115SYSSTAT.COLDIST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1116SYSSTAT.COLGROUPDIST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1117SYSSTAT.COLGROUPDISTCOUNTS . . . . . . . . . . . . . . . . . . . . . . . . . . 1118SYSSTAT.COLGROUPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119SYSSTAT.COLUMNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1120SYSSTAT.INDEXES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1122SYSSTAT.ROUTINES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1126SYSSTAT.TABLES. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1127

    xii SQL Reference Volume 1

  • Appendix E. The SAMPLE database . . . . . . . . . . . . . . . . . . . . . . 1129

    Appendix F. Reserved schema names and reserved words . . . . . . . . . . . . 1155

    Appendix G. Examples of interaction between triggers and referential constraints 1159

    Appendix H. Explain tables . . . . . . . . . . . . . . . . . . . . . . . . . . 1161ADVISE_INDEX table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1163ADVISE_INSTANCE table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1167ADVISE_MQT table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1168ADVISE_PARTITION table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1170ADVISE_TABLE table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1172ADVISE_WORKLOAD table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173EXPLAIN_ACTUALS table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1174EXPLAIN_ARGUMENT table . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175EXPLAIN_DIAGNOSTIC table . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1187EXPLAIN_DIAGNOSTIC_DATA table . . . . . . . . . . . . . . . . . . . . . . . . . . 1188EXPLAIN_FORMAT_OUTPUT table . . . . . . . . . . . . . . . . . . . . . . . . . . 1189EXPLAIN_INSTANCE table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1191EXPLAIN_OBJECT table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194EXPLAIN_OPERATOR table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1198EXPLAIN_PREDICATE table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1201EXPLAIN_STATEMENT table . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1204EXPLAIN_STREAM table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1207OBJECT_METRICS table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1209

    Appendix I. Explain register values . . . . . . . . . . . . . . . . . . . . . . 1215

    Appendix J. Exception tables . . . . . . . . . . . . . . . . . . . . . . . . . 1221

    Appendix K. SQL statements that can be executed in routines and triggers . . . . . 1225

    Appendix L. DB2 technical information . . . . . . . . . . . . . . . . . . . . . 1231DB2 technical library in hardcopy or PDF format . . . . . . . . . . . . . . . . . . . . . . 1232Displaying SQL state help from the command line processor . . . . . . . . . . . . . . . . . . 1234Accessing DB2 documentation online for different DB2 versions . . . . . . . . . . . . . . . . . 1234Terms and conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1235

    Appendix M. Notices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1237

    Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1241

    Contents xiii

  • xiv SQL Reference Volume 1

  • About this book

    The 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 QueryLanguage (SQL) to access a database. It is primarily for programmers and databaseadministrators, but it can also be used by those who access databases through thecommand line processor (CLP).

    This book is a reference rather than a tutorial. It assumes that you will be writingapplication programs and therefore presents the full functions of the databasemanager.

    How this book is structuredThe first volume of the SQL Reference contains information about relationaldatabase concepts, language elements, functions, and the forms of queries. Thespecific chapters and appendixes in that volume are briefly described here.v “Concepts” discusses the basic concepts of relational databases and SQL.v “Language elements” describes the basic syntax of SQL and the language

    elements that are common to many SQL statements.v “Functions” contains syntax diagrams, semantic descriptions, rules, and usage

    examples of SQL aggregate and scalar functions.v “Procedures” contains syntax diagrams, semantic descriptions, rules, and usage

    examples of procedures.v “SQL queries” describes the various forms of a query.v “SQL and XML limits” lists the SQL limitations.v “SQLCA (SQL communications area)” describes the SQLCA structure.v “SQLDA (SQL descriptor area)” describes the SQLDA structure.v “Catalog views” describes the catalog views.v “Federated systems” describes options and type mappings for federated systems.v “The SAMPLE database” introduces the SAMPLE database, which contains the

    tables that are used in many examples.v “Reserved schema names and reserved words” contains the reserved schema

    names and the reserved words for the IBM® SQL and ISO/ANSI SQL2003standards.

    v “Examples of interaction between triggers and referential constraints” discussesthe interaction of triggers and referential constraints.

    v “Explain tables” describes the explain tables.

    © Copyright IBM Corp. 1993, 2014 xv

  • v “Explain register values” describes the interaction of the CURRENT EXPLAINMODE and CURRENT EXPLAIN SNAPSHOT special register values with eachother and with the PREP and BIND commands.

    v “Exception tables” contains information about user-created tables that are usedwith the SET INTEGRITY statement.

    v “SQL statements allowed in routines” lists the SQL statements that are allowedto execute in routines with different SQL data access contexts.

    v “CALL invoked from a compiled statement” describes the CALL statement thatcan be invoked from a compiled statement.

    How this book is structured

    xvi SQL Reference Volume 1

  • How to read the syntax diagramsThis topic describes the structure of SQL syntax diagrams.

    Read the syntax diagrams from left to right and top to bottom, following the pathof the line.

    The double right arrowhead and line symbol ��── indicates the beginning of asyntax diagram.

    The line and single right arrowhead symbol ──� indicates that the syntax iscontinued on the next line.

    The right arrowhead and line symbol �── indicates that the syntax is continuedfrom the previous line.

    The line, right arrowhead, and left arrowhead symbol ──�� symbol indicates theend of a syntax diagram.

    Syntax fragments start with the pipe and line symbol |── and end with the ──|line and pipe symbol.

    Required items appear on the horizontal line (the main path).

    �� required_item ��

    Optional items appear below the main path.

    �� required_itemoptional_item

    ��

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

    �� required_itemoptional_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 mainpath.

    �� required_item required_choice1required_choice2

    ��

    If choosing one of the items is optional, the entire stack appears below the mainpath.

    �� required_itemoptional_choice1optional_choice2

    ��

    How to read the syntax diagrams

    About this book xvii

  • If one of the items is the default, it will appear above the main path, and theremaining choices will be shown below.

    �� required_itemdefault_choice

    optional_choiceoptional_choice

    ��

    An arrow returning to the left, above the main line, indicates an item that can berepeated. 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 acomma.

    �� required_item �

    ,

    repeatable_item ��

    A repeat arrow above a stack indicates that you can make more than one choicefrom the stacked items or repeat a single choice.

    Keywords appear in uppercase (for example, FROM). They must be spelled exactlyas shown. Variables appear in lowercase (for example, column-name). Theyrepresent user-supplied names or values in the syntax.

    If punctuation marks, parentheses, arithmetic operators, or other such symbols areshown, you must enter them as part of the syntax.

    Sometimes a single variable represents a larger fragment of the syntax. Forexample, in the following diagram, the variable parameter-block represents thewhole syntax fragment that is labeled parameter-block:

    �� required_item parameter-block ��

    parameter-block:

    parameter1parameter2 parameter3

    parameter4

    Adjacent segments occurring between “large bullets” (*) may be specified in anysequence.

    �� required_item item1 * item2 * item3 * item4 ��

    How to read the syntax diagrams

    xviii SQL Reference Volume 1

  • 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 item4required_item item1 item3 item2 item4

    Conventions used in this manual

    Error conditionsAn error condition is indicated within the text of the manual by listing theSQLSTATE associated with the error in parentheses.

    For example:A duplicate signature returns an SQL error (SQLSTATE 42723).

    Highlighting conventionsThis topic covers the conventions used in the SQL Reference.v Bold indicates commands, keywords, and other items whose names are

    predefined by the system.v Italics indicates one of the following items:

    – Names or values (variables) that must be supplied by the user– General emphasis– The introduction of a new term– A reference to another source of information

    Conventions describing Unicode dataWhen a specific Unicode code point is referenced, it is expressed as U+n where nis four to six hexadecimal digits, using the digits 0-9 and uppercase letters A-F.

    Leading zeros are omitted unless the code point would have fewer than fourhexadecimal digits. The space character, for example, is expressed as U+0020. Inmost cases, the n value is the same as the UTF-16BE encoding.

    Related documentationThe following publications might prove useful when you are preparingapplications:v Getting Started with Database Application Development

    – Provides an introduction to DB2 application development, including platformprerequisites; supported development software; and guidance on the benefitsand limitations of the supported programming APIs.

    v DB2 for i5/OS SQL Reference– This book defines SQL as supported by DB2 Query Manager and SQL

    Development Kit on System i®. It contains reference information for the tasksof system administration, database administration, application programming,and operation. This manual includes syntax, usage notes, keywords, andexamples for each of the SQL statements used on i5/OS™ systems runningDB2.

    v DB2 for z/OS SQL Reference– This book defines SQL used in DB2 for z/OS®. It provides query forms, SQL

    statements, SQL procedure statements, DB2 limits, SQLCA, SQLDA, catalogtables, and SQL reserved words for z/OS systems running DB2.

    How to read the syntax diagrams

    About this book xix

  • v DB2 Spatial Extender User's 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 adatabase with resources and then querying the data to obtain informationsuch as locations, distances, and distributions within areas.

    v IBM SQL Reference– This book contains all the common elements of SQL that span IBM's database

    products. It provides limits and rules that assist in preparing portableprograms using IBM databases. This manual provides a list of SQL extensionsand 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:2003, Information technology -- Database Languages -- SQL -- Part 2:Foundation (SQL/Foundation)

    – Contains a large portion of the 2003 ISO standard definition of SQL.v ISO/IEC 9075-4:2003, Information technology -- Database Languages -- SQL -- Part 4:

    Persistent Stored Modules (SQL/PSM)

    – Contains the 2003 ISO standard definition for SQL procedure controlstatements.

    Related documentation

    xx SQL Reference Volume 1

  • Chapter 1. Concepts

    DatabasesA DB2 database is a relational database. The database stores all data in tables that arerelated to one another. Relationships are established between tables such that datais shared and duplication is minimized.

    A relational database is a database that is treated as a set of tables and manipulatedin accordance with the relational model of data. It contains a set of objects used tostore, manage, and access data. Examples of such objects are tables, views, indexes,functions, triggers, and packages. Objects can be either defined by the system(built-in objects) or defined by the user (user-defined objects).

    A distributed relational database consists of a set of tables and other objects that arespread across different but interconnected computer systems. Each computersystem has a relational database manager to manage the tables in its environment.The database managers communicate and cooperate with each other in a way thatallows a given database manager to execute SQL statements on another computersystem.

    A partitioned relational database is a relational database whose data is managedacross multiple database partitions. This separation of data across databasepartitions is transparent to most SQL statements. However, some data definitionlanguage (DDL) statements take database partition information into consideration(for example, CREATE DATABASE PARTITION GROUP). DDL is the subset of SQLstatements used to describe data relationships in a database.

    A federated database is a relational database whose data is stored in multiple datasources (such as separate relational databases). The data appears as if it were all ina 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 relationaldatabase.

    In accordance with the relational model of data, the database is treated as a set oftables, relationships are represented by values in tables, and data is retrieved byspecifying 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 thedatabase manager is to transform the specification of a result table into a sequenceof internal operations that optimize data retrieval. The transformation occurs intwo phases: preparation and binding.

    All executable SQL statements must be prepared before they can be executed. Theresult of preparation is the executable or operational form of the statement. Themethod of preparing an SQL statement and the persistence of its operational formdistinguish static SQL from dynamic SQL.

    © Copyright IBM Corp. 1993, 2014 1

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

    A table expression creates a temporary result table from a simple query. Clausesfurther refine the result table. For example, you can use a table expression as aquery to select all of the managers from several departments, specify that theymust have over 15 years of working experience, and be located at the New Yorkbranch office.

    A common table expression is like a temporary view within a complex query. It canbe 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 thesame temporary view.

    Recursive use of a common table expression within a query can be used to supportapplications such as airline reservation systems, bill of materials (BOM) generators,and network planning.

    Introduction to DB2 Call Level Interface and ODBCDB2 Call Level Interface (CLI) is IBM's callable SQL interface to the DB2 family ofdatabase servers. It is a 'C' and 'C++' application programming interface forrelational database access that uses function calls to pass dynamic SQL statementsas function arguments.

    CLI is an alternative to embedded dynamic SQL, but unlike embedded SQL, itdoes not require host variables or a precompiler. Applications can be run against avariety of databases without having to be compiled against each of thesedatabases. Applications use procedure calls at run time to connect to databases,issue SQL statements, and retrieve data and status information.

    The CLI interface provides many features not available in embedded SQL. Forexample: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 catalogqueries 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 CLIcan return result sets to those programs.

    CLI is based on the Microsoft Open Database Connectivity (ODBC) specification,and the International Standard for SQL/CLI. These specifications were chosen asthe basis for the DB2 Call Level Interface in an effort to follow industry standardsand to provide a shorter learning curve for those application programmers alreadyfamiliar with either of these database interfaces. In addition, some DB2 specificextensions have been added to help the application programmer specifically exploitDB2 features.

    Queries and table expressions

    2 SQL Reference Volume 1

  • The CLI driver also acts as an ODBC driver when loaded by an ODBC drivermanager. It conforms to ODBC 3.51.

    CLI Background information

    To understand CLI or any callable SQL interface, it is helpful to understand what itis based on, and to compare it with existing interfaces.

    The X/Open Company and the SQL Access Group jointly developed a specificationfor a callable SQL interface referred to as the X/Open Call Level Interface. The goal ofthis interface is to increase the portability of applications by enabling them tobecome independent of any one database vendor's programming interface. Most ofthe X/Open Call Level Interface specification has been accepted as part of the ISOCall Level Interface International Standard (ISO/IEC 9075-3:1995 SQL/CLI).

    Microsoft developed a callable SQL interface called Open Database Connectivity(ODBC) for Microsoft operating systems based on a preliminary draft of X/OpenCLI.

    The ODBC specification also includes an operating environment wheredatabase-specific ODBC drivers are dynamically loaded at run time by a drivermanager based on the data source (database name) provided on the connectrequest. The application is linked directly to a single driver manager library ratherthan to each DBMS's library. The driver manager mediates the application'sfunction calls at run time and ensures they are directed to the appropriateDBMS-specific ODBC driver. Because the ODBC driver manager only knows aboutthe ODBC-specific functions, DBMS-specific functions cannot be accessed in anODBC environment. DBMS-specific dynamic SQL statements are supportedthrough a mechanism called an escape clause.

    ODBC is not limited to Microsoft operating systems; other implementations areavailable on various platforms.

    The CLI load library can be loaded as an ODBC driver by an ODBC drivermanager. For ODBC application development, you must obtain an ODBC SoftwareDevelopment Kit. For the Windows platform, the ODBC SDK is available as part ofthe Microsoft Data Access Components (MDAC) SDK, available for download fromhttp://www.microsoft.com/downloads. For non-Windows platforms, the ODBCSDK is provided by other vendors. When developing ODBC applications that mayconnect to DB2 servers, use the Call Level Interface Guide and Reference Volume 1and the Call Level Interface Guide and Reference Volume 2 (for information aboutDB2 specific extensions and diagnostic information), in conjunction with the ODBCProgrammer's Reference and SDK Guide available from Microsoft.

    Applications written using CLI APIs link directly to the CLI library. CLI includessupport for many ODBC and ISO SQL/CLI functions, as well as DB2 specificfunctions.

    The following DB2 features are available to both ODBC and CLI applications:v double byte (graphic) data typesv stored proceduresv Distributed Unit of Work (DUOW), two phase commitv compound SQLv user defined types (UDT)

    Introduction to DB2 Call Level Interface and ODBC

    Chapter 1. Concepts 3

    http://www.microsoft.com/downloads

  • v user defined functions (UDF)

    Java application development for IBM data serversThe DB2 and IBM Informix® database systems provide driver support for clientapplications and applets that are written in Java™.

    You can access data in DB2 and IBM Informix database systems using JDBC, SQL,or pureQuery.

    JDBC

    JDBC is an application programming interface (API) that Java applications use toaccess relational databases. IBM data server support for JDBC lets you write Javaapplications that access local DB2 or IBM Informix data or remote relational dataon a server that supports DRDA®.

    SQLJ

    SQLJ provides support for embedded static SQL in Java applications. SQLJ wasinitially developed by IBM, Oracle, and Tandem to complement the dynamic SQLJDBC model with a static SQL model.

    For connections to DB2, in general, Java applications use JDBC for dynamic SQLand SQLJ for static SQL.

    For connections to IBM Informix, SQL statements in JDBC or SQLJ applications rundynamically.

    Because SQLJ can inter-operate with JDBC, an application program can use JDBCand SQLJ within the same unit of work.

    pureQuery

    pureQuery is a high-performance data access platform that makes it easier todevelop, optimize, secure, and manage data access. It consists of:v Application programming interfaces that are built for ease of use and for

    simplifying the use of best practicesv Development tools, which are delivered in IBM Data Studio, for Java and SQL

    developmentv A runtime, which is delivered in IBM InfoSphere® Optim™ pureQuery® Runtime,

    for optimizing and securing database access and simplifying management tasks

    With pureQuery, you can write Java applications that treat relational data asobjects, whether that data is in databases or JDBC DataSource objects. Yourapplications can also treat objects that are stored in in-memory Java collections asthough those objects are relational data. To query or update your relational data orJava objects, you use SQL.

    For more information on pureQuery, see the Integrated Data ManagementInformation Center.

    Introduction to DB2 Call Level Interface and ODBC

    4 SQL Reference Volume 1

  • SchemasA schema is a collection of named objects; it provides a way to group those objectslogically. A schema is also a name qualifier; it provides a way to use the samenatural name for several objects, and to prevent ambiguous references to thoseobjects.

    For example, the schema names 'INTERNAL' and 'EXTERNAL' make it easy todistinguish two different SALES tables (INTERNAL.SALES, EXTERNAL.SALES).

    Schemas also enable multiple applications to store data in a single databasewithout 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 XMLdocuments.

    A schema can contain tables, views, nicknames, triggers, functions, packages, andother objects. A schema is itself a database object. It is explicitly created using theCREATE SCHEMA statement, with the current user or a specified authorization IDrecorded as the schema owner. It can also be implicitly created when anotherobject is created, if the user has IMPLICIT_SCHEMA authority.

    A schema name is used as the high order part of a two-part object name. If theobject is specifically qualified with a schema name when created, the object isassigned to that schema. If no schema name is specified when the object is created,the default schema name is used (specified in the CURRENT SCHEMA specialregister).

    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 schemaC (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 theSYSIBM schema, and the pre-installed user-defined functions belong to theSYSFUN schema.

    When a database is created, if it is not created with the RESTRICTIVE option, allusers have IMPLICIT_SCHEMA authority. With this authority, users implicitlycreate a schema whenever they create an object with a schema name that does notalready exist. When schemas are implicitly created, CREATEIN privileges aregranted which allows any user to create other objects in this schema. The ability tocreate objects such as aliases, distinct types, functions, and triggers is extended toimplicitly created schemas. The default privileges on an implicitly created schemaprovide backward compatibility with previous versions.

    The owner of an implicitly created schema is SYSIBM. When the database isrestrictive, PUBLIC does not have the CREATEIN privilege on the schema. Theuser who implicitly creates the schema has CREATEIN privilege on the schema.When the database is not restrictive, PUBLIC has the CREATEIN privilege on theschema.

    Schemas

    Chapter 1. Concepts 5

  • If IMPLICIT_SCHEMA authority is revoked from PUBLIC, schemas can beexplicitly created using the CREATE SCHEMA statement, or implicitly created byusers (such as those with DBADM authority) who have been grantedIMPLICIT_SCHEMA authority. Although revoking IMPLICIT_SCHEMA authorityfrom PUBLIC increases control over the use of schema names, it can result inauthorization errors when existing applications attempt to create objects.

    Schemas also have privileges, allowing the schema owner to control which usershave the privilege to create, alter, and drop objects in the schema. This abilityprovides 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 theability to grant the privileges to others. An implicitly created schema is owned bythe system, and all users are initially given the privilege to create objects in such aschema, except in a restrictive database environment. A user with ACCESSCTRL orSECADM authority can change the privileges that are held by users on anyschema. Therefore, access to create, alter, and drop objects in any schema (even onethat was implicitly created) can be controlled.

    TablesTables are logical structures maintained by the database manager. Tables are madeup of columns and rows.

    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 asequence of values arranged so that the nth value is a value of the nth column ofthe table.

    An application program can determine the order in which the rows are populatedinto the table, but the actual order of rows is determined by the database manager,and typically cannot be controlled. Multidimensional clustering (MDC) providessome sense of clustering, but not actual ordering between the rows.

    Types of tablesDB2 databases store data in tables. In addition to tables used to store persistentdata, there are also tables that are used for presenting results, summary tables andtemporary tables; multidimensional clustering tables offer specific advantages in awarehouse environment.

    Base tablesThese types of tables hold persistent data. There are different kinds of basetables, including

    Regular tablesRegular tables with indexes are the "general purpose" table choice.

    Multidimensional clustering (MDC) tablesThese types of tables are implemented as tables that are physicallyclustered on more than one key, or dimension, at the same time.MDC tables are used in data warehousing and large databaseenvironments. Clustering indexes on regular tables supportsingle-dimensional clustering of data. MDC tables provide thebenefits of data clustering across more than one dimension. MDCtables provide guaranteed clustering within the compositedimensions. By contrast, although you can have a clustered indexwith regular tables, clustering in this case is attempted by the

    Schemas

    6 SQL Reference Volume 1

  • database manager, but not guaranteed and it typically degradesover time. MDC tables can coexist with partitioned tables and canthemselves be partitioned tables.

    Multidimensional clustering tables are not supported in a DB2pureScale® environment.

    Insert time clustering (ITC) tablesThese types of tables are conceptually, and physically similar toMDC tables, but rather than being clustered by one or more userspecified dimensions, rows are clustered by the time they areinserted into the table. ITC tables can be partitioned tables.

    ITC tables are not supported in a DB2 pureScale environment.

    Range-clustered tables (RCT)These types of tables are implemented as sequential clusters ofdata that provide fast, direct access. Each record in the table has apredetermined record ID (RID) which is an internal identifier usedto locate a record in a table. RCT tables are used where the data istightly clustered across one or more columns in the table. Thelargest and smallest values in the columns define the range ofpossible values. You use these columns to access records in thetable; this is the most optimal method of using the predeterminedrecord identifier (RID) aspect of RCT tables.

    Range-clustered tables are not supported in a DB2 pureScaleenvironment.

    Partitioned tablesThese types of tables use a data organization scheme in whichtable data is divided across multiple storage objects, called datapartitions or ranges, according to values in one or more tablepartitioning key columns of the table. Data partitions can be addedto, attached to, and detached from a partitioned table, and you canstore multiple data partition ranges from a table in one table space.Partitioned tables can contain large amounts of data and simplifythe rolling in and rolling out of table data.

    Temporal tablesThese types of tables are used to associate time-based stateinformation to your data. Data in tables that do not use temporalsupport represents the present, while data in temporal tables isvalid for a period defined by the database system, customerapplications, or both. For example, a database can store the historyof a table (deleted rows or the original values of rows that havebeen updated) so you can query the past state of your data. Youcan also assign a date range to a row of data to indicate when it isdeemed to be valid by your application or business rules.

    Temporary tablesThese types of tables are used as temporary work tables for variousdatabase operations. Declared temporary tables (DGTTs) do not appear in thesystem catalog, which makes them not persistent for use by, and not ableto be shared with other applications. When the application using this tableterminates or disconnects from the database, any data in the table isdeleted and the table is dropped. By contrast, created temporary tables(CGTTs) do appear in the system catalog and are not required to be

    Types of tables

    Chapter 1. Concepts 7

  • defined in every session where they are used. As a result, they arepersistent and able to be shared with other applications across differentconnections.

    Neither type of temporary table supportsv User-defined reference or user-defined structured type columnsv LONG VARCHAR columnsIn addition XML columns cannot be used in created temporary tables.

    Materialized query tablesThese types of tables are defined by a query that is also used to determinethe data in the table. Materialized query tables can be used to improve theperformance of queries. If the database manager determines that a portionof a query can be resolved using a summary table, the database managercan rewrite the query to use the summary table. This decision is based ondatabase configuration settings, such as the CURRENT REFRESH AGE andthe CURRENT QUERY OPTIMIZATION special registers. A summary tableis a specialized type of materialized query table.

    You can create all of the preceding types of tables using the CREATE TABLEstatement.

    Depending on what your data is going to look like, you might find one table typeoffers specific capabilities that can optimize storage and query performance. Forexample, if you have data records that are loosely clustered (not monotonicallyincreasing), consider using a regular table and indexes. If you have data recordsthat have duplicate (but not unique) values in the key, do not use a range-clusteredtable. Also, if you cannot afford to preallocate a fixed amount of storage on diskfor the range-clustered tables you might want, do not use this type of table. If youhave data that has the potential for being clustered along multiple dimensions,such as a table tracking retail sales by geographic region, division and supplier, amultidimensional clustering table might suit your purposes.

    In addition to the various table types described previously, you also have optionsfor such characteristics as partitioning, which can improve performance for taskssuch as rolling in table data. Partitioned tables can also hold much moreinformation than a regular, nonpartitioned table. You can also use capabilities suchas compression, which can help you significantly reduce your data storage costs.

    ConstraintsWithin any business, data must often adhere to certain restrictions or rules. Forexample, an employee number must be unique. The database manager providesconstraints as a way to enforce such rules.

    The following types of constraints are available:v NOT NULL constraintsv Unique (or unique key) constraintsv Primary key constraintsv Foreign key (or referential integrity) constraintsv (Table) Check constraintsv Informational constraints

    Constraints are only associated with tables and are either defined as part of thetable creation process (using the CREATE TABLE statement) or are added to a

    Types of tables

    8 SQL Reference Volume 1

  • table's definition after the table has been created (using the ALTER TABLEstatement). You can use the ALTER TABLE statement to modify constraints. Inmost cases, existing constraints can be dropped at any time; this action does notaffect the table's structure or the data stored in it.

    Note: Unique and primary constraints are only associated with table objects, theyare often enforced through the use of one or more unique or primary key indexes.

    IndexesAn index is a set of pointers that are logically ordered by the values of one or morekeys. The pointers can refer to rows in a table, blocks in an MDC or ITC table,XML data in an XML storage object, and so on.

    Indexes are used to:v Improve performance. In most cases, access to data is faster with an index.

    Although an index cannot be created for a view, an index created for the tableon which a view is based can sometimes improve the performance of operationson that view.

    v Ensure uniqueness. A table with a unique index cannot have rows with identicalkeys.

    As data is added to a table, it is appended to the bottom (unless other actions havebeen carried out on the table or the data being added). There is no inherent orderto the data. When searching for a particular row of data, each row of the tablefrom first to last must be checked. Indexes are used as a means to access the datawithin the table in an order that might otherwise not be available.

    Typically, when you search for data in a table, you are looking for rows withcolumns that have specific values. A column value in a row of data can be used toidentify the entire row. For example, an employee number would probablyuniquely define a specific individual employee. Or, more than one column mightbe needed to identify the row. For example, a combination of customer name andtelephone number. Columns in an index used to identify data rows are known askeys. A column can be used in more than one key.

    An index is ordered by the values within a key. Keys can be unique or non-unique.Each table should have at least one unique key; but can also have other,non-unique keys. Each index has exactly one key. For example, you might use theemployee ID number (unique) as the key for one index and the departmentnumber (non-unique) as the key for a different index.

    Not all indexes point to rows in a table. MDC and ITC block indexes point toextents (or blocks) of the data. XML indexes for XML data use particular XMLpattern expressions to index paths and values in XML documents stored within asingle column. The data type of that column must be XML. Both MDC and ITCblock indexes and XML indexes are system generated indexes.

    Example

    Table A in Figure 1 on page 10 has an index based on the employee numbers in thetable. This key value provides a pointer to the rows in the table. For example,employee number 19 points to employee KMP. An index allows efficient access torows in a table by creating