+ All Categories
Home > Documents > IBM i: DB2 for i SQL Reference · PDF fileAbout DB2 for i SQL Reference IBM i: DB2 for i SQL...

IBM i: DB2 for i SQL Reference · PDF fileAbout DB2 for i SQL Reference IBM i: DB2 for i SQL...

Date post: 30-Jan-2018
Category:
Upload: doanhanh
View: 308 times
Download: 0 times
Share this document with a friend
1876
IBM i DB2 for i SQL Reference 7.1 IBM
Transcript
  • IBM i

    DB2 for i SQL Reference7.1

    IBM

  • IBM i

    DB2 for i SQL Reference7.1

    IBM

  • NoteBefore using this information and the product it supports, read the information in Appendix K,Notices, on page 1823.

    This edition applies to IBM i 7.1 (product number 5770SS1) and to all subsequent releases and modifications untilotherwise indicated in new editions. This version does not run on all reduced instruction set computer (RISC)models nor does it run on CISC models.

    Copyright IBM Corporation 1998, 2010.US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contractwith IBM Corp.

  • Contents

    About DB2 for i SQL Reference . . . . xiAbout SQL reference . . . . . . . . . . . xi

    Standards compliance . . . . . . . . . . xiAssumptions relating to examples of SQLstatements. . . . . . . . . . . . . . xiiHow to read the syntax diagrams. . . . . . xiiiConventions used in this book. . . . . . . xivSQL accessibility . . . . . . . . . . . xv

    PDF file for SQL reference . . . . . . . . . xviWhat's new for IBM i 7.1 . . . . . . . . . xvi

    Chapter 1. Concepts . . . . . . . . . 1Relational database . . . . . . . . . . . . 1Structured Query Language . . . . . . . . . 2Schemas . . . . . . . . . . . . . . . . 5Tables . . . . . . . . . . . . . . . . 5

    Keys . . . . . . . . . . . . . . . . 6Constraints . . . . . . . . . . . . . . 7Indexes . . . . . . . . . . . . . . . 10Triggers. . . . . . . . . . . . . . . 11

    Views . . . . . . . . . . . . . . . . 13User-defined types . . . . . . . . . . . . 14Aliases . . . . . . . . . . . . . . . . 14Packages and access plans . . . . . . . . . 15Routines . . . . . . . . . . . . . . . 16Sequences . . . . . . . . . . . . . . . 17Authorization, privileges and object ownership . . 18Catalog . . . . . . . . . . . . . . . . 20Application processes, concurrency, and recovery. . 20

    Locking, commit, and rollback . . . . . . . 22Unit of work . . . . . . . . . . . . . 23Rolling back work . . . . . . . . . . . 24Threads . . . . . . . . . . . . . . 25

    Isolation level . . . . . . . . . . . . . 26Repeatable read . . . . . . . . . . . . 28Read stability . . . . . . . . . . . . . 29Cursor stability . . . . . . . . . . . . 29Uncommitted read . . . . . . . . . . . 30No commit . . . . . . . . . . . . . 30Comparison of isolation levels . . . . . . . 30

    Storage Structures . . . . . . . . . . . . 31Character conversion . . . . . . . . . . . 32

    Character sets and code pages . . . . . . . 34Coded character sets and CCSIDs . . . . . . 36Default CCSID . . . . . . . . . . . . 36

    Collating sequence . . . . . . . . . . . . 37Distributed relational database . . . . . . . . 40

    Application servers . . . . . . . . . . . 40CONNECT (Type 1) and CONNECT (Type 2) . . 42Remote unit of work . . . . . . . . . . 42Application-directed distributed unit of work . . 44Data representation considerations. . . . . . 46

    Chapter 2. Language elements . . . . 49Characters . . . . . . . . . . . . . . . 49

    Tokens . . . . . . . . . . . . . . . . 50Identifiers . . . . . . . . . . . . . . . 51

    SQL identifiers . . . . . . . . . . . . 52System identifiers . . . . . . . . . . . 52Host identifiers . . . . . . . . . . . . 53

    Naming conventions . . . . . . . . . . . 54SQL path . . . . . . . . . . . . . . 63Qualification of unqualified object names . . . 63SQL names and system names: specialconsiderations . . . . . . . . . . . . 65

    Aliases . . . . . . . . . . . . . . . . 66Authorization IDs and authorization names . . . 68Procedure resolution . . . . . . . . . . . 70Data types. . . . . . . . . . . . . . . 71

    Nulls . . . . . . . . . . . . . . . 72Numbers . . . . . . . . . . . . . . 73Character strings . . . . . . . . . . . 76Character encoding schemes . . . . . . . . 77Graphic strings . . . . . . . . . . . . 78Graphic encoding schemes . . . . . . . . 79Binary strings . . . . . . . . . . . . 79Large objects . . . . . . . . . . . . . 80Datetime values . . . . . . . . . . . . 82XML Values . . . . . . . . . . . . . 88DataLink values . . . . . . . . . . . . 89Row ID values . . . . . . . . . . . . 90User-defined types . . . . . . . . . . . 90

    Promotion of data types . . . . . . . . . . 93Casting between data types . . . . . . . . . 95Assignments and comparisons . . . . . . . . 98

    Numeric assignments . . . . . . . . . . 99String assignments . . . . . . . . . . 102Datetime assignments . . . . . . . . . 105XML assignments . . . . . . . . . . . 106DataLink assignments . . . . . . . . . 106Row ID assignments . . . . . . . . . . 108Distinct type assignments . . . . . . . . 108Array type assignments . . . . . . . . . 109Assignments to LOB locators . . . . . . . 109Numeric comparisons . . . . . . . . . 110String comparisons . . . . . . . . . . 111Datetime comparisons . . . . . . . . . 113XML comparisons . . . . . . . . . . . 113DataLink comparisons . . . . . . . . . 113Row ID comparisons . . . . . . . . . . 113Distinct type comparisons . . . . . . . . 114

    Rules for result data types . . . . . . . . . 115Conversion rules for operations that combinestrings. . . . . . . . . . . . . . . . 120Constants . . . . . . . . . . . . . . 122

    Integer constants . . . . . . . . . . . 122Decimal constants . . . . . . . . . . . 122Floating-point constants . . . . . . . . . 122Decimal floating-point constants . . . . . . 123Character-string constants . . . . . . . . 123Graphic-string constants . . . . . . . . . 124

    Copyright IBM Corp. 1998, 2010 iii

    ||

    ||

    ||

    ||

    ||

    ||

  • Binary-string constants . . . . . . . . . 126Datetime constants . . . . . . . . . . 126Decimal point . . . . . . . . . . . . 127Delimiters . . . . . . . . . . . . . 128

    Special registers . . . . . . . . . . . . 129CURRENT CLIENT_ACCTNG . . . . . . 130CURRENT CLIENT_APPLNAME . . . . . 130CURRENT CLIENT_PROGRAMID . . . . . 130CURRENT CLIENT_USERID . . . . . . . 131CURRENT CLIENT_WRKSTNNAME . . . . 131CURRENT DATE . . . . . . . . . . . 132CURRENT DEBUG MODE . . . . . . . . 132CURRENT DECFLOAT ROUNDING MODE 133CURRENT DEGREE . . . . . . . . . . 134CURRENT IMPLICIT XMLPARSE OPTION . . 135CURRENT PATH . . . . . . . . . . . 135CURRENT SCHEMA . . . . . . . . . . 136CURRENT SERVER . . . . . . . . . . 137CURRENT TIME . . . . . . . . . . . 137CURRENT TIMESTAMP . . . . . . . . 137CURRENT TIMEZONE . . . . . . . . . 138SESSION_USER . . . . . . . . . . . 138SYSTEM_USER. . . . . . . . . . . . 138USER . . . . . . . . . . . . . . . 139

    Column names . . . . . . . . . . . . . 140Qualified column names. . . . . . . . . 140Correlation names . . . . . . . . . . . 140Column name qualifiers to avoid ambiguity . . 142Column name qualifiers in correlated references 144Unqualified column names in correlatedreferences . . . . . . . . . . . . . 145

    Variables . . . . . . . . . . . . . . . 147Global variables . . . . . . . . . . . 147References to host variables . . . . . . . 149Variables in dynamic SQL . . . . . . . . 152References to LOB or XML variables. . . . . 152References to XML variables . . . . . . . 154Host structures . . . . . . . . . . . . 155Host structure arrays . . . . . . . . . . 157

    Functions. . . . . . . . . . . . . . . 158Types of functions . . . . . . . . . . . 158Function invocation . . . . . . . . . . 159Function resolution . . . . . . . . . . 160Determining the best fit . . . . . . . . . 161Best fit considerations . . . . . . . . . 164

    Expressions . . . . . . . . . . . . . . 165Without operators . . . . . . . . . . . 166With arithmetic operators . . . . . . . . 166With the concatenation operator . . . . . . 170Scalar fullselect . . . . . . . . . . . . 173Datetime operands and durations . . . . . 173Datetime arithmetic in SQL. . . . . . . . 174Precedence of operations . . . . . . . . 178ARRAY constructor . . . . . . . . . . 180ARRAY element specification . . . . . . . 181CASE expression . . . . . . . . . . . 182CAST specification . . . . . . . . . . 185OLAP specifications . . . . . . . . . . 190ROW CHANGE expression. . . . . . . . 194Sequence reference . . . . . . . . . . 195XMLCAST specification . . . . . . . . . 199

    Predicates . . . . . . . . . . . . . . 201Basic predicate . . . . . . . . . . . . 202Quantified predicate . . . . . . . . . . 204BETWEEN predicate . . . . . . . . . . 207DISTINCT predicate . . . . . . . . . . 208EXISTS predicate . . . . . . . . . . . 210IN predicate . . . . . . . . . . . . . 211LIKE predicate . . . . . . . . . . . . 213NULL predicate . . . . . . . . . . . 218REGEXP_LIKE predicate . . . . . . . . 219Trigger event predicates . . . . . . . . . 224

    Search conditions . . . . . . . . . . . . 225

    Chapter 3. Built-in functions . . . . . 227Aggregate functions . . . . . . . . . . . 237

    ARRAY_AGG . . . . . . . . . . . . 238AVG . . . . . . . . . . . . . . . 240COUNT . . . . . . . . . . . . . . 242COUNT_BIG . . . . . . . . . . . . 243GROUPING . . . . . . . . . . . . . 244MAX . . . . . . . . . . . . . . . 246MIN . . . . . . . . . . . . . . . 247STDDEV_POP or STDDEV . . . . . . . . 249STDDEV_SAMP . . . . . . . . . . . 250SUM . . . . . . . . . . . . . . . 251VAR_POP or VARIANCE or VAR . . . . . 252VARIANCE_SAMP or VAR_SAMP . . . . . 253XMLAGG . . . . . . . . . . . . . 254XMLGROUP . . . . . . . . . . . . 256

    Scalar functions . . . . . . . . . . . . 259ABS . . . . . . . . . . . . . . . 260ACOS . . . . . . . . . . . . . . . 261ADD_MONTHS . . . . . . . . . . . 262ANTILOG . . . . . . . . . . . . . 263ASCII . . . . . . . . . . . . . . . 264ASIN . . . . . . . . . . . . . . . 265ATAN . . . . . . . . . . . . . . . 266ATANH . . . . . . . . . . . . . . 267ATAN2 . . . . . . . . . . . . . . 268BIGINT . . . . . . . . . . . . . . 269BINARY . . . . . . . . . . . . . . 271BITAND, BITANDNOT, BITOR, BITXOR, andBITNOT . . . . . . . . . . . . . . 272BIT_LENGTH . . . . . . . . . . . .


Recommended