sql refresher on db2 10.5, sql standard update and more · pdf fileperformance is based on...
TRANSCRIPT
copy 2013 IBM Corporation 1
SQL Refresher on DB2 105 SQL Standard Update and More
August 29 2013
Presented by
Rick Swagerman DB2 Language Architect IBM Labs
copy 2013 IBM Corporation 2 Need webcast troubleshooting help Click attachments
Rick Swagerman Host and Todayrsquos Presenter
DB2 Language Architect
1 The presentation for this Tech Talk
bitlyttfileaug2013
2 Next steps and troubleshooting guide
click ldquoAttachmentsldquo in this webcast window
A few details hellip
DB2 Tech Talk series host and today‟s presenter
Todayrsquos SQL enhancements are available in the DB2 105 trial download ibmcodb2download
Currently shipping at FP1 FP1 includes GA level JSON capabilities amp more httpbitlydb2105fp1
copy 2013 IBM Corporation 3 Need webcast troubleshooting help Click attachments
Disclaimer
The information contained in this presentation is provided for informational purposes only
While efforts were made to verify the completeness and accuracy of the information contained in this presentation it is provided ldquoas isrdquo without warranty of any kind express or implied
In addition this information is based on IBM‟s current product plans and strategy which are subject to change by IBM without notice
IBM shall not be responsible for any damages arising out of the use of or otherwise related to this presentation or any other documentation
Nothing contained in this presentation is intended to or shall have the effect of
bull Creating any warranty or representation from IBM (or its affiliates or its or their suppliers andor licensors) or
bull Altering the terms and conditions of the applicable license agreement governing the use of IBM software
Performance is based on measurements and projections using standard IBM benchmarks in a controlled environment The actual throughput or performance that any user will experience will vary depending upon many factors including considerations such as the amount of multiprogramming in the users job stream the IO configuration the storage configuration and the workload processed Therefore no assurance can be given that an individual user will achieve results similar to those stated here
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
4 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard (a ldquoshortrdquo introduction)
Well-known de jure database language standard
Mature standard but still evolving
ISO standard (ISOIEC 9075) developed collaboratively by a number of national bodies
(USA UK Germany Canada Japan Netherlands Sweden etc)
National body standards identical to the ISO standard
Many implementations - with varying degrees of conformance
Large number of applications
5 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Basics
Data structured as tables
Declarative query expressions to retrieve data from tables
Update operations (INSERT UPDATE DELETE)
Declarative constraints and triggers
Security model based on the concept of privileges and grantingrevoking those privileges
Control of concurrent access and system failures through transaction mechanism
Schema evolution
Recent enhancements SQLXML and temporal queries
many more
6 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard (ANSIISOIEC 9075) ndash A Brief History
SQL Standard (ANSIISOIEC 9075
1986 (1987)
First version
published as
ANSI (ISO)
standard
1989
First revision
published as ISO
and (subsequently
as) ANSI standard
1992
Second revision
19951996
Two additional
incremental parts
published (PSM
and CLI)
1999
Third revision
Five parts
(Framework
Foundation CLI
PSM Bindings)
20012002
Three additional
incremental parts
published (MED
OLB and JRT)
2003
Fourth revision
Nine parts
(Framework
Foundation CLI
PSM MED OLB
Schemata JRT
XML)
2006
Revision of
SQLXML
published
2008
Fifth
revision
Nine parts
(Framework
Foundation
CLI PSM
MED OLB
Schemata
JRT XML)
2011
Sixth revision
Five parts
(Framework
Foundation PSM
Schemata XML)
CLI MED OLB and
JRT currently dormant
7 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview
Part 1 SQLFramework
ndash Structure of the standard and relationship between various parts
ndash Common definitions and concepts
ndash Conformance requirements statement
Part 2 SQLFoundation
ndash Specifies the core language - all of SQL1999Foundation plus a few extensions
bull DDL for creating altering and dropping various persistent objects including tables
views user-defined types and SQL-invoked routines
bull Predefined data types + type constructors
bull DML for retrieving and updating persistent data
bull Scalar and table expressions
bull Predicates
bull Host language bindings dynamic SQL and direct SQL
8 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 3 SQLCLI
ndash A Call-Level Interface for invoking SQL from applications
ndash Consists of over 60 routine specifications
bull Control connections to SQL-servers
bull Allocate and deallocate resources
bull Execute SQL statements
bull Control transaction termination
bull Obtain information about the implementation
ndash Provided for vendors of truly portable shrink wrapped software
bull CLI does not require pre-compilation of the application program
bull Application program can be delivered in shrink wrapped object-code form
9 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 4 SQLPSM (Persistent Stored Modules)
ndash Procedural language constructs (similar to those found in block-structured languages)
bull Improve performance in centralized and clientserver environments
Multiple SQL statements in a single EXEC SQL
Multi-statement procedures functions and methods
ndash Gives great power to DBMS
bull Several control statements (procedural language extension)
beginend block assignment call case if loop for signalresignal variables
exception handling
ndash SQL-only implementation of complex functions
bull Without worrying about security (firewall)
bull Without worrying about performance (local call)
10 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation 2 Need webcast troubleshooting help Click attachments
Rick Swagerman Host and Todayrsquos Presenter
DB2 Language Architect
1 The presentation for this Tech Talk
bitlyttfileaug2013
2 Next steps and troubleshooting guide
click ldquoAttachmentsldquo in this webcast window
A few details hellip
DB2 Tech Talk series host and today‟s presenter
Todayrsquos SQL enhancements are available in the DB2 105 trial download ibmcodb2download
Currently shipping at FP1 FP1 includes GA level JSON capabilities amp more httpbitlydb2105fp1
copy 2013 IBM Corporation 3 Need webcast troubleshooting help Click attachments
Disclaimer
The information contained in this presentation is provided for informational purposes only
While efforts were made to verify the completeness and accuracy of the information contained in this presentation it is provided ldquoas isrdquo without warranty of any kind express or implied
In addition this information is based on IBM‟s current product plans and strategy which are subject to change by IBM without notice
IBM shall not be responsible for any damages arising out of the use of or otherwise related to this presentation or any other documentation
Nothing contained in this presentation is intended to or shall have the effect of
bull Creating any warranty or representation from IBM (or its affiliates or its or their suppliers andor licensors) or
bull Altering the terms and conditions of the applicable license agreement governing the use of IBM software
Performance is based on measurements and projections using standard IBM benchmarks in a controlled environment The actual throughput or performance that any user will experience will vary depending upon many factors including considerations such as the amount of multiprogramming in the users job stream the IO configuration the storage configuration and the workload processed Therefore no assurance can be given that an individual user will achieve results similar to those stated here
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
4 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard (a ldquoshortrdquo introduction)
Well-known de jure database language standard
Mature standard but still evolving
ISO standard (ISOIEC 9075) developed collaboratively by a number of national bodies
(USA UK Germany Canada Japan Netherlands Sweden etc)
National body standards identical to the ISO standard
Many implementations - with varying degrees of conformance
Large number of applications
5 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Basics
Data structured as tables
Declarative query expressions to retrieve data from tables
Update operations (INSERT UPDATE DELETE)
Declarative constraints and triggers
Security model based on the concept of privileges and grantingrevoking those privileges
Control of concurrent access and system failures through transaction mechanism
Schema evolution
Recent enhancements SQLXML and temporal queries
many more
6 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard (ANSIISOIEC 9075) ndash A Brief History
SQL Standard (ANSIISOIEC 9075
1986 (1987)
First version
published as
ANSI (ISO)
standard
1989
First revision
published as ISO
and (subsequently
as) ANSI standard
1992
Second revision
19951996
Two additional
incremental parts
published (PSM
and CLI)
1999
Third revision
Five parts
(Framework
Foundation CLI
PSM Bindings)
20012002
Three additional
incremental parts
published (MED
OLB and JRT)
2003
Fourth revision
Nine parts
(Framework
Foundation CLI
PSM MED OLB
Schemata JRT
XML)
2006
Revision of
SQLXML
published
2008
Fifth
revision
Nine parts
(Framework
Foundation
CLI PSM
MED OLB
Schemata
JRT XML)
2011
Sixth revision
Five parts
(Framework
Foundation PSM
Schemata XML)
CLI MED OLB and
JRT currently dormant
7 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview
Part 1 SQLFramework
ndash Structure of the standard and relationship between various parts
ndash Common definitions and concepts
ndash Conformance requirements statement
Part 2 SQLFoundation
ndash Specifies the core language - all of SQL1999Foundation plus a few extensions
bull DDL for creating altering and dropping various persistent objects including tables
views user-defined types and SQL-invoked routines
bull Predefined data types + type constructors
bull DML for retrieving and updating persistent data
bull Scalar and table expressions
bull Predicates
bull Host language bindings dynamic SQL and direct SQL
8 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 3 SQLCLI
ndash A Call-Level Interface for invoking SQL from applications
ndash Consists of over 60 routine specifications
bull Control connections to SQL-servers
bull Allocate and deallocate resources
bull Execute SQL statements
bull Control transaction termination
bull Obtain information about the implementation
ndash Provided for vendors of truly portable shrink wrapped software
bull CLI does not require pre-compilation of the application program
bull Application program can be delivered in shrink wrapped object-code form
9 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 4 SQLPSM (Persistent Stored Modules)
ndash Procedural language constructs (similar to those found in block-structured languages)
bull Improve performance in centralized and clientserver environments
Multiple SQL statements in a single EXEC SQL
Multi-statement procedures functions and methods
ndash Gives great power to DBMS
bull Several control statements (procedural language extension)
beginend block assignment call case if loop for signalresignal variables
exception handling
ndash SQL-only implementation of complex functions
bull Without worrying about security (firewall)
bull Without worrying about performance (local call)
10 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation 3 Need webcast troubleshooting help Click attachments
Disclaimer
The information contained in this presentation is provided for informational purposes only
While efforts were made to verify the completeness and accuracy of the information contained in this presentation it is provided ldquoas isrdquo without warranty of any kind express or implied
In addition this information is based on IBM‟s current product plans and strategy which are subject to change by IBM without notice
IBM shall not be responsible for any damages arising out of the use of or otherwise related to this presentation or any other documentation
Nothing contained in this presentation is intended to or shall have the effect of
bull Creating any warranty or representation from IBM (or its affiliates or its or their suppliers andor licensors) or
bull Altering the terms and conditions of the applicable license agreement governing the use of IBM software
Performance is based on measurements and projections using standard IBM benchmarks in a controlled environment The actual throughput or performance that any user will experience will vary depending upon many factors including considerations such as the amount of multiprogramming in the users job stream the IO configuration the storage configuration and the workload processed Therefore no assurance can be given that an individual user will achieve results similar to those stated here
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
4 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard (a ldquoshortrdquo introduction)
Well-known de jure database language standard
Mature standard but still evolving
ISO standard (ISOIEC 9075) developed collaboratively by a number of national bodies
(USA UK Germany Canada Japan Netherlands Sweden etc)
National body standards identical to the ISO standard
Many implementations - with varying degrees of conformance
Large number of applications
5 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Basics
Data structured as tables
Declarative query expressions to retrieve data from tables
Update operations (INSERT UPDATE DELETE)
Declarative constraints and triggers
Security model based on the concept of privileges and grantingrevoking those privileges
Control of concurrent access and system failures through transaction mechanism
Schema evolution
Recent enhancements SQLXML and temporal queries
many more
6 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard (ANSIISOIEC 9075) ndash A Brief History
SQL Standard (ANSIISOIEC 9075
1986 (1987)
First version
published as
ANSI (ISO)
standard
1989
First revision
published as ISO
and (subsequently
as) ANSI standard
1992
Second revision
19951996
Two additional
incremental parts
published (PSM
and CLI)
1999
Third revision
Five parts
(Framework
Foundation CLI
PSM Bindings)
20012002
Three additional
incremental parts
published (MED
OLB and JRT)
2003
Fourth revision
Nine parts
(Framework
Foundation CLI
PSM MED OLB
Schemata JRT
XML)
2006
Revision of
SQLXML
published
2008
Fifth
revision
Nine parts
(Framework
Foundation
CLI PSM
MED OLB
Schemata
JRT XML)
2011
Sixth revision
Five parts
(Framework
Foundation PSM
Schemata XML)
CLI MED OLB and
JRT currently dormant
7 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview
Part 1 SQLFramework
ndash Structure of the standard and relationship between various parts
ndash Common definitions and concepts
ndash Conformance requirements statement
Part 2 SQLFoundation
ndash Specifies the core language - all of SQL1999Foundation plus a few extensions
bull DDL for creating altering and dropping various persistent objects including tables
views user-defined types and SQL-invoked routines
bull Predefined data types + type constructors
bull DML for retrieving and updating persistent data
bull Scalar and table expressions
bull Predicates
bull Host language bindings dynamic SQL and direct SQL
8 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 3 SQLCLI
ndash A Call-Level Interface for invoking SQL from applications
ndash Consists of over 60 routine specifications
bull Control connections to SQL-servers
bull Allocate and deallocate resources
bull Execute SQL statements
bull Control transaction termination
bull Obtain information about the implementation
ndash Provided for vendors of truly portable shrink wrapped software
bull CLI does not require pre-compilation of the application program
bull Application program can be delivered in shrink wrapped object-code form
9 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 4 SQLPSM (Persistent Stored Modules)
ndash Procedural language constructs (similar to those found in block-structured languages)
bull Improve performance in centralized and clientserver environments
Multiple SQL statements in a single EXEC SQL
Multi-statement procedures functions and methods
ndash Gives great power to DBMS
bull Several control statements (procedural language extension)
beginend block assignment call case if loop for signalresignal variables
exception handling
ndash SQL-only implementation of complex functions
bull Without worrying about security (firewall)
bull Without worrying about performance (local call)
10 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
4 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard (a ldquoshortrdquo introduction)
Well-known de jure database language standard
Mature standard but still evolving
ISO standard (ISOIEC 9075) developed collaboratively by a number of national bodies
(USA UK Germany Canada Japan Netherlands Sweden etc)
National body standards identical to the ISO standard
Many implementations - with varying degrees of conformance
Large number of applications
5 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Basics
Data structured as tables
Declarative query expressions to retrieve data from tables
Update operations (INSERT UPDATE DELETE)
Declarative constraints and triggers
Security model based on the concept of privileges and grantingrevoking those privileges
Control of concurrent access and system failures through transaction mechanism
Schema evolution
Recent enhancements SQLXML and temporal queries
many more
6 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard (ANSIISOIEC 9075) ndash A Brief History
SQL Standard (ANSIISOIEC 9075
1986 (1987)
First version
published as
ANSI (ISO)
standard
1989
First revision
published as ISO
and (subsequently
as) ANSI standard
1992
Second revision
19951996
Two additional
incremental parts
published (PSM
and CLI)
1999
Third revision
Five parts
(Framework
Foundation CLI
PSM Bindings)
20012002
Three additional
incremental parts
published (MED
OLB and JRT)
2003
Fourth revision
Nine parts
(Framework
Foundation CLI
PSM MED OLB
Schemata JRT
XML)
2006
Revision of
SQLXML
published
2008
Fifth
revision
Nine parts
(Framework
Foundation
CLI PSM
MED OLB
Schemata
JRT XML)
2011
Sixth revision
Five parts
(Framework
Foundation PSM
Schemata XML)
CLI MED OLB and
JRT currently dormant
7 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview
Part 1 SQLFramework
ndash Structure of the standard and relationship between various parts
ndash Common definitions and concepts
ndash Conformance requirements statement
Part 2 SQLFoundation
ndash Specifies the core language - all of SQL1999Foundation plus a few extensions
bull DDL for creating altering and dropping various persistent objects including tables
views user-defined types and SQL-invoked routines
bull Predefined data types + type constructors
bull DML for retrieving and updating persistent data
bull Scalar and table expressions
bull Predicates
bull Host language bindings dynamic SQL and direct SQL
8 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 3 SQLCLI
ndash A Call-Level Interface for invoking SQL from applications
ndash Consists of over 60 routine specifications
bull Control connections to SQL-servers
bull Allocate and deallocate resources
bull Execute SQL statements
bull Control transaction termination
bull Obtain information about the implementation
ndash Provided for vendors of truly portable shrink wrapped software
bull CLI does not require pre-compilation of the application program
bull Application program can be delivered in shrink wrapped object-code form
9 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 4 SQLPSM (Persistent Stored Modules)
ndash Procedural language constructs (similar to those found in block-structured languages)
bull Improve performance in centralized and clientserver environments
Multiple SQL statements in a single EXEC SQL
Multi-statement procedures functions and methods
ndash Gives great power to DBMS
bull Several control statements (procedural language extension)
beginend block assignment call case if loop for signalresignal variables
exception handling
ndash SQL-only implementation of complex functions
bull Without worrying about security (firewall)
bull Without worrying about performance (local call)
10 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
SQL Standard (a ldquoshortrdquo introduction)
Well-known de jure database language standard
Mature standard but still evolving
ISO standard (ISOIEC 9075) developed collaboratively by a number of national bodies
(USA UK Germany Canada Japan Netherlands Sweden etc)
National body standards identical to the ISO standard
Many implementations - with varying degrees of conformance
Large number of applications
5 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Basics
Data structured as tables
Declarative query expressions to retrieve data from tables
Update operations (INSERT UPDATE DELETE)
Declarative constraints and triggers
Security model based on the concept of privileges and grantingrevoking those privileges
Control of concurrent access and system failures through transaction mechanism
Schema evolution
Recent enhancements SQLXML and temporal queries
many more
6 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard (ANSIISOIEC 9075) ndash A Brief History
SQL Standard (ANSIISOIEC 9075
1986 (1987)
First version
published as
ANSI (ISO)
standard
1989
First revision
published as ISO
and (subsequently
as) ANSI standard
1992
Second revision
19951996
Two additional
incremental parts
published (PSM
and CLI)
1999
Third revision
Five parts
(Framework
Foundation CLI
PSM Bindings)
20012002
Three additional
incremental parts
published (MED
OLB and JRT)
2003
Fourth revision
Nine parts
(Framework
Foundation CLI
PSM MED OLB
Schemata JRT
XML)
2006
Revision of
SQLXML
published
2008
Fifth
revision
Nine parts
(Framework
Foundation
CLI PSM
MED OLB
Schemata
JRT XML)
2011
Sixth revision
Five parts
(Framework
Foundation PSM
Schemata XML)
CLI MED OLB and
JRT currently dormant
7 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview
Part 1 SQLFramework
ndash Structure of the standard and relationship between various parts
ndash Common definitions and concepts
ndash Conformance requirements statement
Part 2 SQLFoundation
ndash Specifies the core language - all of SQL1999Foundation plus a few extensions
bull DDL for creating altering and dropping various persistent objects including tables
views user-defined types and SQL-invoked routines
bull Predefined data types + type constructors
bull DML for retrieving and updating persistent data
bull Scalar and table expressions
bull Predicates
bull Host language bindings dynamic SQL and direct SQL
8 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 3 SQLCLI
ndash A Call-Level Interface for invoking SQL from applications
ndash Consists of over 60 routine specifications
bull Control connections to SQL-servers
bull Allocate and deallocate resources
bull Execute SQL statements
bull Control transaction termination
bull Obtain information about the implementation
ndash Provided for vendors of truly portable shrink wrapped software
bull CLI does not require pre-compilation of the application program
bull Application program can be delivered in shrink wrapped object-code form
9 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 4 SQLPSM (Persistent Stored Modules)
ndash Procedural language constructs (similar to those found in block-structured languages)
bull Improve performance in centralized and clientserver environments
Multiple SQL statements in a single EXEC SQL
Multi-statement procedures functions and methods
ndash Gives great power to DBMS
bull Several control statements (procedural language extension)
beginend block assignment call case if loop for signalresignal variables
exception handling
ndash SQL-only implementation of complex functions
bull Without worrying about security (firewall)
bull Without worrying about performance (local call)
10 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
SQL Basics
Data structured as tables
Declarative query expressions to retrieve data from tables
Update operations (INSERT UPDATE DELETE)
Declarative constraints and triggers
Security model based on the concept of privileges and grantingrevoking those privileges
Control of concurrent access and system failures through transaction mechanism
Schema evolution
Recent enhancements SQLXML and temporal queries
many more
6 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard (ANSIISOIEC 9075) ndash A Brief History
SQL Standard (ANSIISOIEC 9075
1986 (1987)
First version
published as
ANSI (ISO)
standard
1989
First revision
published as ISO
and (subsequently
as) ANSI standard
1992
Second revision
19951996
Two additional
incremental parts
published (PSM
and CLI)
1999
Third revision
Five parts
(Framework
Foundation CLI
PSM Bindings)
20012002
Three additional
incremental parts
published (MED
OLB and JRT)
2003
Fourth revision
Nine parts
(Framework
Foundation CLI
PSM MED OLB
Schemata JRT
XML)
2006
Revision of
SQLXML
published
2008
Fifth
revision
Nine parts
(Framework
Foundation
CLI PSM
MED OLB
Schemata
JRT XML)
2011
Sixth revision
Five parts
(Framework
Foundation PSM
Schemata XML)
CLI MED OLB and
JRT currently dormant
7 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview
Part 1 SQLFramework
ndash Structure of the standard and relationship between various parts
ndash Common definitions and concepts
ndash Conformance requirements statement
Part 2 SQLFoundation
ndash Specifies the core language - all of SQL1999Foundation plus a few extensions
bull DDL for creating altering and dropping various persistent objects including tables
views user-defined types and SQL-invoked routines
bull Predefined data types + type constructors
bull DML for retrieving and updating persistent data
bull Scalar and table expressions
bull Predicates
bull Host language bindings dynamic SQL and direct SQL
8 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 3 SQLCLI
ndash A Call-Level Interface for invoking SQL from applications
ndash Consists of over 60 routine specifications
bull Control connections to SQL-servers
bull Allocate and deallocate resources
bull Execute SQL statements
bull Control transaction termination
bull Obtain information about the implementation
ndash Provided for vendors of truly portable shrink wrapped software
bull CLI does not require pre-compilation of the application program
bull Application program can be delivered in shrink wrapped object-code form
9 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 4 SQLPSM (Persistent Stored Modules)
ndash Procedural language constructs (similar to those found in block-structured languages)
bull Improve performance in centralized and clientserver environments
Multiple SQL statements in a single EXEC SQL
Multi-statement procedures functions and methods
ndash Gives great power to DBMS
bull Several control statements (procedural language extension)
beginend block assignment call case if loop for signalresignal variables
exception handling
ndash SQL-only implementation of complex functions
bull Without worrying about security (firewall)
bull Without worrying about performance (local call)
10 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
SQL Standard (ANSIISOIEC 9075) ndash A Brief History
SQL Standard (ANSIISOIEC 9075
1986 (1987)
First version
published as
ANSI (ISO)
standard
1989
First revision
published as ISO
and (subsequently
as) ANSI standard
1992
Second revision
19951996
Two additional
incremental parts
published (PSM
and CLI)
1999
Third revision
Five parts
(Framework
Foundation CLI
PSM Bindings)
20012002
Three additional
incremental parts
published (MED
OLB and JRT)
2003
Fourth revision
Nine parts
(Framework
Foundation CLI
PSM MED OLB
Schemata JRT
XML)
2006
Revision of
SQLXML
published
2008
Fifth
revision
Nine parts
(Framework
Foundation
CLI PSM
MED OLB
Schemata
JRT XML)
2011
Sixth revision
Five parts
(Framework
Foundation PSM
Schemata XML)
CLI MED OLB and
JRT currently dormant
7 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview
Part 1 SQLFramework
ndash Structure of the standard and relationship between various parts
ndash Common definitions and concepts
ndash Conformance requirements statement
Part 2 SQLFoundation
ndash Specifies the core language - all of SQL1999Foundation plus a few extensions
bull DDL for creating altering and dropping various persistent objects including tables
views user-defined types and SQL-invoked routines
bull Predefined data types + type constructors
bull DML for retrieving and updating persistent data
bull Scalar and table expressions
bull Predicates
bull Host language bindings dynamic SQL and direct SQL
8 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 3 SQLCLI
ndash A Call-Level Interface for invoking SQL from applications
ndash Consists of over 60 routine specifications
bull Control connections to SQL-servers
bull Allocate and deallocate resources
bull Execute SQL statements
bull Control transaction termination
bull Obtain information about the implementation
ndash Provided for vendors of truly portable shrink wrapped software
bull CLI does not require pre-compilation of the application program
bull Application program can be delivered in shrink wrapped object-code form
9 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 4 SQLPSM (Persistent Stored Modules)
ndash Procedural language constructs (similar to those found in block-structured languages)
bull Improve performance in centralized and clientserver environments
Multiple SQL statements in a single EXEC SQL
Multi-statement procedures functions and methods
ndash Gives great power to DBMS
bull Several control statements (procedural language extension)
beginend block assignment call case if loop for signalresignal variables
exception handling
ndash SQL-only implementation of complex functions
bull Without worrying about security (firewall)
bull Without worrying about performance (local call)
10 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview
Part 1 SQLFramework
ndash Structure of the standard and relationship between various parts
ndash Common definitions and concepts
ndash Conformance requirements statement
Part 2 SQLFoundation
ndash Specifies the core language - all of SQL1999Foundation plus a few extensions
bull DDL for creating altering and dropping various persistent objects including tables
views user-defined types and SQL-invoked routines
bull Predefined data types + type constructors
bull DML for retrieving and updating persistent data
bull Scalar and table expressions
bull Predicates
bull Host language bindings dynamic SQL and direct SQL
8 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 3 SQLCLI
ndash A Call-Level Interface for invoking SQL from applications
ndash Consists of over 60 routine specifications
bull Control connections to SQL-servers
bull Allocate and deallocate resources
bull Execute SQL statements
bull Control transaction termination
bull Obtain information about the implementation
ndash Provided for vendors of truly portable shrink wrapped software
bull CLI does not require pre-compilation of the application program
bull Application program can be delivered in shrink wrapped object-code form
9 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 4 SQLPSM (Persistent Stored Modules)
ndash Procedural language constructs (similar to those found in block-structured languages)
bull Improve performance in centralized and clientserver environments
Multiple SQL statements in a single EXEC SQL
Multi-statement procedures functions and methods
ndash Gives great power to DBMS
bull Several control statements (procedural language extension)
beginend block assignment call case if loop for signalresignal variables
exception handling
ndash SQL-only implementation of complex functions
bull Without worrying about security (firewall)
bull Without worrying about performance (local call)
10 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 3 SQLCLI
ndash A Call-Level Interface for invoking SQL from applications
ndash Consists of over 60 routine specifications
bull Control connections to SQL-servers
bull Allocate and deallocate resources
bull Execute SQL statements
bull Control transaction termination
bull Obtain information about the implementation
ndash Provided for vendors of truly portable shrink wrapped software
bull CLI does not require pre-compilation of the application program
bull Application program can be delivered in shrink wrapped object-code form
9 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 4 SQLPSM (Persistent Stored Modules)
ndash Procedural language constructs (similar to those found in block-structured languages)
bull Improve performance in centralized and clientserver environments
Multiple SQL statements in a single EXEC SQL
Multi-statement procedures functions and methods
ndash Gives great power to DBMS
bull Several control statements (procedural language extension)
beginend block assignment call case if loop for signalresignal variables
exception handling
ndash SQL-only implementation of complex functions
bull Without worrying about security (firewall)
bull Without worrying about performance (local call)
10 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 4 SQLPSM (Persistent Stored Modules)
ndash Procedural language constructs (similar to those found in block-structured languages)
bull Improve performance in centralized and clientserver environments
Multiple SQL statements in a single EXEC SQL
Multi-statement procedures functions and methods
ndash Gives great power to DBMS
bull Several control statements (procedural language extension)
beginend block assignment call case if loop for signalresignal variables
exception handling
ndash SQL-only implementation of complex functions
bull Without worrying about security (firewall)
bull Without worrying about performance (local call)
10 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 9 SQLMED (Management of External Data)
ndash Two distinct concepts
bull Datalinks
External data coordinated by SQL-server
External data accessed through native interface
External data kept consistent with SQL-data
bull Foreign Data (or Wrapper interface)
External data not controlled by SQL-server
External data accessed through SQL-server
Data access from multiple sources transparently
Part 10 SQLOLB (Object Language Bindings)
ndash Embedding of SQL statements in Java programs
ndash Many differences from the traditional host language bindings
bull specification in terms of JDBC but static compilation
bull provides typed cursors and better exception handling
bull platform independence (binary portability)
ndash precursor SQLJ Part 0
11 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 11 SQLSchemata
ndash Specification of over 65 views that describe the metadata in the SQL-environment
bull TABLES
bull COLUMNS
bull USER_DEFINED_TYPES etc
ndash Exist in schema INFORMATION_SCHEMA
ndash Can be queried by users - see only those objects that are either owned or have some
privilege on
ndash Cannot be updated by users
ndash Underlying base tables created in a hypothetical schema DEFINITION_SCHEMA -
automatically updated every time a DDL statement is executed
Part 13 SQLJRT (Javatrade Routines and Types)
ndash SQL extensions that allow creation of
bull SQL-invoked routines corresponding to Java static methods
bull SQL user-defined structured types corresponding to Java classes
ndash precursor SQLJ Parts 1 amp 2
12 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
SQL Standard ndash Parts Overview (cont)
Part 14 SQLXML
ndash Major goals
bull Publish SQL query results as XML documents
bull Ability to store and retrieve XML documents
bull Query XML data
ndash Rules for mapping SQL types SQL identifiers and SQL data values to and from
corresponding XML concepts
ndash A new built-in type XML
bull Based on the XQuery data model
bull Can be an XML document or more complex (ie a sequence in the XQuery data
model)
ndash A number of built-in operators that produce values of type XML
ndash A number of built-in expressions to querymanipulate XML values
bull XMLTable
bull XMLQuery
bull XMLExists
bull XMLCast
13 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Conformance
SQL-92 used incremental levels of conformance (Entry Intermediate Full)
SQL1999 SQL2003 SQL2008 and SQL2011 consist of a large number of small
features each identified and precisely specified as to its content
Each feature is specified either to be a constituent of Core SQL or not a constituent of
Core SQL
ndash SQL2003 SQL2008 and SQL2011 did not add any features to Core
Implementations can claim
ndash minimum conformance (Core SQL)
bull conformance to certain features of parts 2 (Foundation) and 11 (Schemata)
ndash conformance to a part (0 or more)
bull mandatory features of part + addtl conformance requirements of part
ndash conformance to optional feature (0 or more)
bull may imply conformance to other features
14 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Overview of SQL2011 Core Features
All of SQL-92 Entry level
Some Intermediate SQL-92 features
Some Full SQL-92 features
The following features of SQL1999
ndash Distinct data types including USER_DEFINED_TYPES view
ndash WITH HOLD cursors
ndash SQL-invoked routines (but not the ability to explicitly specify a PATH or overloading)
bull SQL-invoked routines written in SQL or an external language (one can conform by
supporting only one)
bull RETURN statement (for functions written in SQL)
bull CALL statement (with the extension to dynamic SQL to support CALL)
bull ROUTINES and PARAMETERS view
ndash Value expression in order by clause
SQL2003 SQL2008 and SQL2011 did not add any features to Core
15 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z)
B033 Untyped SQL-invoked function arguments
F033 ALTER TABLE statement DROP COLUMN clause (LUW only)
F121-01 GET DIAGNOSTICS statement
F171 Multiple schemas per user
F191 Referential delete actions
F200 TRUNCATE TABLE statement
F302 INTERSECT table operator
F304 EXCEPT ALL table operator
F312 MERGE statement F313 Enhanced MERGE statementand F314 MERGE statement with DELETE branch (LUW complete z subset)
F381 Extended schema manipulation
F382 Alter column data type
F383 Set column not null clause
F385 Drop column generation expression clause
F386 Set identity column generation clause
F391 Long identifiers
F401-02 FULL OUTER JOIN
F411 Time zone specification (z only)
F431 Read-only scrollable cursors (z only)
F492 Optional table constraint enforcement
F491 Constraint management
F492 Optional table constraint enforcement
F555 Enhanced seconds precision
F591 Derived tables
F751 View CHECK enhancements
F763 CURRENT_SCHEMA
F791 Insensitive cursors (z only)
F801 Full set function
F831 Full cursor update (subset)
F850 Top-level ltorder by clausegt in ltquery expressiongt F855 Nested ltorder by clausegt in ltquery expressiongt
F857 Top-level ltfetch first clausegt in ltquery expressiongt Nested ltfetch first clausegt in ltquery expressiongt
S023 Basic structured types S151 Type predicate S161 Subtype treatment S241 Transform functions (LUW only)
S071 SQL paths in function and type name resolution
16 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
S401 Distinct types based on array types S098 ARRAY_AGG S403 ARRAY_MAX_CARDINALITY S404 TRIM_ARRAY (LUW only)
T021 BINARY and VARBINARY data types (z only)
T041 Basic LOB data type support
T071 BIGINT data type
T121 WITH (excluding RECURSIVE) in query expression
T174 Identity columns T178 Identity columns simple restart option
T175 Generated columns
T176 Sequence generator support T177 Sequence generator support simple restart option
T180 System-versioned tables
T181 Application-time period tables
T191 Referential action RESTRICT
T201 Comparable data types for referential constraints
T211 Basic trigger capability
T213 INSTEAD OF triggers
T231 Sensitive cursors (z only)
T271 Savepoints T272 Enhanced savepoint management
T325 Qualified SQL parameter references
T326 Table functions
T341 Overloading of SQL-invoked functions and SQL-invoked procedures
T441 ABS and MOD functions
T471 Result sets return value
T491 LATERAL derived table (different keyword)
T495 Combined data change and retrieval
T521 Named arguments in CALL statement T522 Default values for IN parameters of SQL-invoked procedures (LUW only)
T611 Elementary OLAP operations
T613 Sampling (LUW only)
T641 Multiple column assignment
T651 SQL-schema statements in SQL routines T652 SQL-dynamic statements in SQL routines T653 SQL-schema statements in external routines T654 SQL-dynamic statements in external routines
17 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Examples of non-core SQL Standard features supported by DB2 (LUW andor z) (cont)
P002 Computational completeness
P005 Qualified SQL variable references
P006 Multiple assignment
X010 XML type
X016 Persistent XML values
X020 XMLConcat X025 XMLCast X030 XMLDocument X031 XMLElement X032 XMLForest
X034 XMLAgg X035 XMLAgg ORDER BY option
X036 XMLComment X037 XMLPI X038 XMLText
X061 XMLParse Character string input and DOCUMENT option X066 XMLParse BLOB input and DOCUMENT option
X070 XMLSerialize Character string serialization and CONTENT option X073 XMLSerialize BLOB serialization and CONTENT option
X085 Predefined namespace prefixes
X096 XMLExists
X200 XMLQuery
X272 XMLValidate ACCORDING TO clause
X300 XMLTable
18 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
What‟s New in SQL2011
Temporal table and query support (major new feature)
ndash Supports system-time period tables
ndash Application-time period tables
ndash Combination of both (sometimes referred to as bitemporal tables)
Delete in MERGE statement
Select from data change delta table
Procedures with defaults for parameters and named arguments
Limit rows on SELECT
(FETCH FIRST|NEXT n ROWS ONLY|WITH TIES and OFFSET)
User-defined collection types
Not enforced table constraints
Window function additions
(NTILE LAG LEAD NTH_VALUE FIRST_VALUE LAST_VALUE)
19 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
20 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
IBM and the SQL Standard
DB2 is IBM‟s premier relational database management system
Available on your favorite hardware platform running ndash zOS ndash Linux UNIX and Windows (LUW) ndash IBM i
Roots go back to System R in the 1970s ndash before there was an SQL Standard
IBM representation on ANSIISO committees from SQL86 to present day
DB2 for LUW certified SQL92 Entry level with NIST back in Version 5 (1995)
Continuously enhanced since that time (both DB2 and SQL standard)
Current releases ndash DB2 10 for zOS (GA‟d in 2011) ndash DB2 105 for LUW (GA‟d in 2013) ndash DB2 71 TR6 for i (GA‟d in 2010 TR6 in 2013)
Each platform releases enhancements in between major releases through fixpacks etc
Significant support for SQL2011 CORE and beyond
21 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
The IBM SQL Language Council (SLC)
Mission of the SLC is to define SQL syntax and semantics that are
ndash Common across all DB2 platforms (enabling portability)
ndash Usable
ndash Extensible
ndash Standard compatible (where possible)
ndash Industry compatible (where applicable)
Core team consists of SQL standards team reps from DB2 for LUW DB2 for zOS
and DB2 for i
Instrumental in getting the DB2 family aligned with respect to SQL functionality (family
compatibility)
Instrumental in getting DB2 aligned with the SQL standard
Key to minimizing divergence between the standard and products
22 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
The SQL Reference for Cross-Platform Development
This reference makes it easy for you to develop applications using SQL that is portable across the DB2 family
Freely available as a pdf at httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Latest version (Version 4 - dated June 13 2012) covers ndash DB2 for zOS Version 10 ndash DB2 for i Version 71 ndash DB2 for LUW Version 97
New in Version 4 ndash XML data type XMLCAST specification XML Scalar Functions XMLAGG ndash Datetime constants ndash Extended indicator variables Enhanced untyped parameter marker support ndash New functions
bull ADD_MONTHS EXTRACT LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND_TIMESTAMP and TRUNC_TIMESTAMP BITAND BITANDNOT BITNOT BITOR BITXOR TRIM Enhanced TIMESTAMP_FORMAT and VARCHAR_FORMAT functions
ndash CROSS JOIN ndash Retrieval of result sets in embedded SQL (ALLOCATE CURSOR and ASSOCIATE
LOCATORS statements) ndash Expressions in a CALL statement ndash Distinct types in a temporary table ndash hellip
23 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation 24 Need webcast troubleshooting help Click attachments
DB2 for zOS vs DB2 for LUW (not exhaustive features may be missing)
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation
Global Temporary Tables CASE 100+ Built-in Functions Limited Fetch Insensitive Scroll
Cursors UNION Everywhere MINMAX Single Index Support Self Referencing Updates with
Subqueries Sort Avoidance for ORDER BY and Row Expressions Call from trigger
statement isolation
Updateable UNION in Views ORDER BYFETCH FIRST in subselects amp table expressions
GROUPING SETS ROLLUP CUBE INSTEAD OF TRIGGER EXCEPT INTERSECT 16
Built-in Functions MERGE Native SQL Procedure Language SET CURRENT ISOLATION
BIGINT data type file reference variables SELECT FROM UPDATE DELETE amp MERGE
multi-site join 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect
Materialized Query Tables Common Table Expressions Recursive SQL CURRENT
PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names
Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column FOR READ
ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL
object names SELECT from INSERT
z
L
U
W
C o m m o n
Range partitioning
DB2 for zOS 7 and DB2 Linux Unix amp Windows v82
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
DB2 for zOS vs DB2 for LUW (cont) (not exhaustive features may be missing)
2
5
2
5
DB2 10 for zOS and DB2 101 Linux Unix amp Windows
Multi-row INSERT FETCH amp multi-row cursor UPDATE Dynamic Scrollable Cursors GET DIAGNOSTICS Enhanced UNICODE SQL join across encoding schemes IS NOT DISTINCT FROM VARBINARY FETCH CONTINUE MERGE SELECT from MERGE routine versioning timestamps wtimezone
Inner and Outer Joins Table Expressions Subqueries GROUP BY Complex Correlation Global Temporary Tables CASE 100+ Built-in Functions including SQLXML Limited Fetch Insensitive Scroll Cursors UNION Everywhere MINMAX Single Index Self Referencing Updates with Subqueries Sort Avoidance for ORDER BY and Row Expressions 2M Statement Length GROUP BY Expression Sequences Scalar Fullselect Materialized Query Tables Common Table Expressions Recursive SQL CURRENT PACKAGE PATH VOLATILE Tables Star Join Sparse Index Qualified Column names Multiple DISTINCT clauses ON COMMIT DROP Transparent ROWID Column Call from trigger statement isolation FOR READ ONLY KEEP UPDATE LOCKS SET CURRENT SCHEMA Client special registers long SQL object names SELECT from INSERT UPDATE or DELETE INSTEAD OF TRIGGER Native SQL Procedure Language BIGINT file reference variables XML FETCH FIRST amp ORDER BY in subselect amp fullselect caseless comparisons INTERSECT EXCEPT not logged tables OmniFind spatial range partitions data compression session variables DECIMAL FLOAT optimistic locking ROLE TRUNCATE index amp XML compression created temps inline LOB administrative privileges implicit cast datetime changes currently committed moving sum amp average index include columns row and column access control time travel query
Updateable UNION in Views GROUPING SETS ROLLUP CUBE more Built-in Functions SET CURRENT ISOLATION multi-site join MERGE MDC XQuery XML enhancements additional data type (array row cursor)
global variables even more vendor syntax temp table compression MODULEs
z
L U W
C o m m o n
25 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
References SQL Standard and DB2 SQL
SQL Standard httpwwwisoorgisohomestorecatalogue_tccatalogue_detailhtmcsnumber=53682
httpwwwniadindecmdlevel=tpl-art-detailansichtampcommitteeid=54738935ampartid=149085971
httpwebstoreansiorgRecordDetailaspxsku=ISO2FIEC+9075-23A2011
DB2 CP SQL Ref httpwwwibmcomdeveloperworksdatalibrarytecharticle0206sqlref0206sqlrefhtml
Selected common SQL features for developers of portable DB2 applications httpwwwibmcomdeveloperworksdatalibrarytecharticledb2common
DB2 for zOS SQL Ref httppicdheibmcominfocenterdzichelpv2r2topiccomibmdb2z10docsqlrefsrcsqlrefdb2z_sqlrefhtm
DB2 for LUW SQL Ref httppicdheibmcominfocenterdb2luwv10r5indexjsptopic=comibmdb2luwsqlrefdocdocc000410
0html
DB2 for i SQL Ref httppicdheibmcominfocenteriseriesv7r1m0topicdb2rbafzintrohtm
26 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
27 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Features added in DB2 1012
Weakly typed distinct types
Pipelined table functions
28 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types
Purpose
ndash Compatibility to Oracle SUBTYPE
ndash Use a unique alias for the data type of a group of variables parameters and columns to provide a single point of control over their type
ndash Provide check constraints for distinct types which allows for data types with more restrictive domains than base types (eg COLOR ZIPCODE)
ndash Relax current distinct type strong typing rules
Scope ndash Parameters variables and table columns (without constraints only)
29
29
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Weakly typed distinct types example
CREATE TYPE color AS CHAR(1) WITH WEAK TYPE RULES
NOT NULL CHECK (VALUE IN (lsquoRrsquolsquoGrsquolsquoBrsquo))
CREATE VARIABLE my_color color DEFAULT lsquoRrsquo
SET my_color = lsquoWrsquo
SQL20552N The cast or assignment failed because the value does not conform to the data type constraint of the user-defined type User-defined type COLOR Value W
CREATE TYPE hellip WITH WEAK TYPE RULES
ndash Function resolution treats type as base type
ndash Optional check constraint and NOT NULL constraint
Ref ldquoDistinctly weak type rules with a constrained twistrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryweak_type
30
30
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions
Purpose ndash Compatibility to pipelined functions in Oracle
ndash Incrementally produce a result set for consumption on demand
Problem ndash Java C and NET table functions provide support but require host
language skill
ndash Inline SQL table functions must compose the result set in a single RETURN statement
Solution ndash PIPE statement returns a row to caller but continues at next statement
if caller wants another row
31
31
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Pipelined table functions Example
CREATE FUNCTION dates(IN dt DATE) RETURNS TABLE(dt DATE) BEGIN LOOP PIPE (dt) SET dt = dt + 1 DAY END LOOP RETURN END
SELECT FROM TABLE(dates(2013-01-01)) FETCH FIRST 2 ROWS ONLY DT 2013-01-01 2013-01-02
Ref ldquoIn the PIPE Compiled SQL PL and PLSQL table functionsrdquo at wwwibmcomdeveloperworkscommunityblogsSQLTips4DB2LUWentryin_the_pipe
32
32
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Features added in DB2 105
Indexes based on expressions
Indexes that exclude null keys
Indexes with random order
Not enforced unique and primary key constraints
Ad-hoc federated table access
Tables with extended row size
33 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions
Purpose ndash Add support for indexes with expression-based keys such that queries
with these predicates on expressions can exploit the indexes
ndash Avoid the drawbacks of work-around (index on generated column)
ndash Compatibility with DB2 family
ndash Commonly used in Oracle to perform case insensitive searches and to index over complex expressions
Solution
ndash Allow expressions for parts of index key
ndash Index statistics are collected through an associated statistical view that is generated automatically
34
34
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes based on expressions Examples
-- an index on employee name and id columns
CREATE INDEX emp_name ON employee(name id)
-- an index with an expression-based key using upper case
-- of employee name (still include the id column)
CREATE INDEX emp_upper ON employee(upper(name) id)
-- an index with an expression-based key using upper case
-- of employee name total employee compensation and id column
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
35
35
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Index based on expressions Catalog representation
CREATE INDEX emp_comp on employee(upper(name) salary + bonus id)
SYSCATINDEXES
SYSCATINDEXCOLUSE
36
INDNAME COLCOUNT COLNAMES INDEXTYPE
EMP_COMP 3 +K00+K01+ID REG
INDNAME COLSEQ COLNAME TEXT
EMP_COMP 1 K00 UPPER(NAME)
EMP_COMP 2 K01 SALARY + BONUS
EMP_COMP 3 ID -
36
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Index based on expressions Statistical view
CREATE INDEX user1emp_comp on user1employee (upper(name) salary + bonus id)
Generated statistical view
CREATE VIEW ldquoUSER1EMP_COMP_V(K00 K01 ldquoID) AS
SELECT UPPER(NAME)
SALARY + BONUS
ldquoID
FROM ldquoUSER1EMPLOYEEldquo
RUNSTATS against table and indexes as usual also collects statistics for the statistical view
Caution The generated statistical view cannot be used in other statements by any user select k00 k01 id from user1emp_comp_v
SQL0551N The statement failed because the authorization ID does not have the
required authorization or privilege to perform the operation Authorization
ID ldquoSWAGRMAN Operation SELECT Object ldquoUSER1EMP_COMP_V
SQLSTATE=42501
37 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS
Purpose ndash Allow for the creation of unique constraints that are not enforced for
duplicate null keys
ndash Reduce the size of indexes over a large number of null keys
ndash Compatibility with Oracle where indexes do not include null keys
Solution ndash INCLUDE NULL KEYS and EXCLUDE NULL KEYS clauses are added to the
CREATE INDEX statement
ndash INCLUDE NULL KEYS is the default
38
38
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
A NULL key is a key where all the key parts are NULL
Consider possible indexes
ndash create unique index ENK on t1 (c1 c2) exclude null keys
ndash create unique index INK on t1 (c1 c2) include null keys
Indexes that EXCLUDE NULL KEYS
Unique Constraint Insert
C1 C2
NULL NULL
C1 C2 ENK Index INK Index
NULL 1 Success Success
2 NULL Success Success
NULL NULL Success Success
NULL NULL Success Fail
39
Excluded from ENK index
(included in table)
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Usage
Indexes created with the ENK clause do not index rows where all key parts are null
ENK indexes can be used for query optimization when one or more index key parts are used in null-rejecting predicates ndash SELECT FROM T1 WHERE C1 = 5
ndash SELECT FROM T1 T2 WHERE T1C1 = T2C1
40
40
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Indexes that EXCLUDE NULL KEYS Examples
-- an EXCLUDE NULL KEYS index on customer email
CREATE INDEX cust_email ON customer(email) EXCLUDE NULL KEYS
-- an INCLUDE NULL KEYS index on customer id name
CREATE INDEX cust_key ON customer(id name) INCLUDE NULL KEYS
SYSCATINDEXES
41
INDNAME NULLKEYS
CUST_EMAIL N
CUST_KEY Y
41
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Indexes with random order
Sample motivating scenario
ndash An online store defines an ORDER_NUM column
ndash ORDER_NUM is indexed for fast lookups
ndash Each concurrent transaction gets a newly incremented ORDER_NUM value
ndash Results in frequent attempts by each member to update last index leaf page to
add the latest ORDER_NUM
Random key indexes
CREATE INDEX i1 ON t1 (INT ORDER_NUM RANDOM)
ndash Each ORDER_NUM is randomized before insertion into the index
ndash Spreads access requests evenly across index leaf pages
ndash Lookups apply reverse algorithm
ndash Not usable for scans
42 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
NOT ENFORCED unique and primary key constraints
Constraint attribute NOT ENFORCED extended to include unique and primary key constraints
ndash Already supported for check and referential constraints
Use when data is known unique but index and enforcement not needed
No longer and underlying index to enforce the uniqueness
Optimizer still takes advantage of the information but without an index
Target usage is for column-organized tables where indexes are not used
If an enforced referential constraint exists the parent unique or primary key constraint must also be enforced
There is no ALTER TABLE support to toggle between NOT ENFORCED and ENFORCED
ndash Drop the constraint and add it again with the different enforcement
43 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Ad-hoc federated table access
Problem
ndash Historically DB2 required creation of a NICKNAME to access a
remote table
ndash Syntax is incompatible with Oracle
Solution
ndash Support an ad-hoc reference to a remote table using the server in
the identifier DB2 105 adds this support for static (SQL PL and
PLSQL) statements
ndash DB2 select from serverschematable
ndash Oracle select from schematableserver
44
44
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size
Purpose ndash Support applications that require large row size
ndash Avoid length table redefinitions to change pagesize on migration
ndash Allow tables with defined row size gt 32K max page size
Target users ndash Database schema design that defines string columns as bigmaximum
length when the actual content is typically much smaller
ndash Tables with many columns that are usually NULL but can be large
ndash Migrating tables with row sizes greater than 32KB to DB2
Solution ndash Large strings treated like LOB if actual row size exceeds page size limit
ndash Designed assuming that a small percentage of rows will spill over
45
45
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Externals
EXTENDED_ROW_SZ database configuration parameter
ndash Defaults to ENABLED for databases created in DB2 105 or later
ndash Defaults to OFF for databases created prior to DB2 105
In CREATE TABLE table space selection defaults to the table space
with the smallest page size that can accommodate the table without
extended row size
If this search fails a second search is conducted that defaults to the
largest page size that can accommodate the table with extended row
size
Of course you should always specify the table space you want
46
46
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Internals
INSERT or UPDATE which cause the row to exceed the page size of
the table space causes a subset of varying length string columns to
be stored as LOB data
UPDATE which shrinks the size of the row to be within the page size
will return to using the varcharvargraphic storage of string data
47
47
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Limits
The extended row size length limit is 1048319 bytes
(just under 1MB)
Individual VARCHAR and VARGRAPHIC column length limits are not
changed (from 32672 and 16338 respectively)
The maximum number of columns allowed for a particular page size
is not changed (500-1012 currently)
48
48
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation Need webcast troubleshooting help Click attachments
Tables with extended row size Catalog representation
SYSCATTABLESEXTENDED_ROW_SIZE
ndash Y ndash Row size exceeds the maximum record length for the page size
ndash N - Row size does not exceed the maximum for the page size
SYSCATTABLESPCTEXTENDEDROWS
ndash Extended rows as a percentage of total rows in the table
ndash -1 if statistics are not collected
Performance penalty when need to go bdquooff page‟ for a portion of row
ndash For intended scenarios instances of long rows are rare
ndash If you expect long rows to be common use a larger page size if possible
49
49
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
SQL Compatibility A Steady Increase
Steady increase in compatibility over time
ndash More and more complex applications
DB2 105 estimated to provide gt98 statement compatibility
Data is based on DCW (Database Conversion Workbench) DB2 reports
50 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Agenda
Overview and Status of the SQL Standard
IBM and the SQL Standard
SQL changes in DB2 Version 105 for Linux UNIX and Windows
Powerful SQL ndash Are you using it
51 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Reflections of a database language architect
SQL has grown tremendously over the past 20 years (DB2 and Standard)
Tendency to look at the changes incrementally from release to release ndash Eg Can I use this new feature that was delivered
Are we taking advantage of the incredible power of SQL that the
combination of these incremental changes has provided
Are there ldquoforgotten old toolsrdquo in the tool box that you could be using
perhaps together with the ldquoshiny new toolsrdquo
52 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Early SQL (circa 1990)
Life as SQL user was simpler but way more limited
The DB2 SQL Reference manual was about 200 pages
Consider that
ndashFROM clause allowed table or view names with implicit inner join
ndashLogic was mostly in a WHERE clause with a set of predicates (basic quantified BETWEEN EXISTS IS NULL LIKE and IN)
bull Also in the HAVING clause for grouping sets
ndashThere were only 5 aggregate functions
ndashThere were only 20 built-in scalar functions
ndashThere were no user-defined procedures functions types triggers etc
53 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Modern SQL (circa 2013)
Consider current SQL support particularly for query and data manipulation
ndashFROM clause
ndashLogic and control capabilities
ndashBuilt-in functions
ndashTriggers
ndashUser-defined procedures functions and modules
54 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
FROM clause
Explicit JOIN (INNER LEFT RIGHT and FULL OUTER) with ON clause
Nested table expression or derived table allows a fullselect ndash Allows a query to be performed on the result of another query (maybe
including aggregations or order by and fetch first clauses) ndash Much like specifying query from a view instead of the view name ndash Includes possibility of a ldquoconstant tablerdquo using the VALUES form of fullselect
References to federated tables as nicknames or 3-part names
References to data change table representing ndash new rows from insert operation ndash old rows from delete operation ndash old or new rows from update operation
References to built-in or user-defined table functions including ndash UNNEST to convert an array to a result table ndash XMLTABLE to query an XML document and return a result table based on the
sequence
Period specification for temporal table queries (AS OF FROM TO and BETWEEN)
55 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Logic and control capabilities
Basic quantified and IN predicates with comparison of row value
expression
Some additional specialized predicates added ndash ARRAY_EXISTS XMLEXISTS trigger event predicates cursor predicates
CASE expression provides powerful inline logic
SQL PL extended logic support to also control flow
ndashEg CASE FOR IF ITERATE REPEAT WHILE etc
Compound SQL statement can use SQL PL and be a stand alone
statement or get used as the body of a procedure function or trigger
MERGE statement provides statement level logic to determine update
delete or insert processing of data
56 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Built-in functions
Over 20 aggregate functions
Over 200 scalar functions ndash string numeric datetime cast array XML etc
8 specific window (or OLAP) functions plus use of most aggregate
functions over a window ndash LAG LEAD
ndash RANK DENSE_RANK ROW_NUMBER
ndash FIRST_VALUE LAST_VALUE RATIO_TO_REPORT
57 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Triggers
Associate automatic operations with data change operations on tables
within the database
BEFORE triggers can change the values to be inserted or updated and
can also be used for more complex checking of new values
AFTER triggers can be used for other operations related to the data
change
Use SQL PL and compound SQL for triggered action definition
Ability to invoke user-defined procedures and functions within trigger
Combine or separate triggers by event ndash Insert update delete or any combination of these
ndash For combination trigger use trigger event predicates (inserting updating
deleting)
58 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
User-defined procedures functions and modules
Allows user to define application logic or commonly used operations in the
database
Procedures and functions can be written in SQL or an external language
like C or Java
Definitions of procedures and functions can be overloaded (by various
criteria)
More recent additions made definition and use even more flexible ndash Defined defaults for parameters
ndash Associate parameter name with arguments when invoking procedure or
function
Modules can be defined to group a set of procedures functions types
variables and conditions ndash Module objects can be published or defined for use only within the module
ndash Module object usage outside of the module generally requires module name as
additional qualifier
There are also user-defined types to be consideredhellip 59 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Example Destructive read
Retrieve and delete the next lowest order number
SQL features used 1 Data change operation in a FROM clause
2 Fullselect as target of DELETE
3 ORDER BY and FETCH FIRST in subselectfullselect
4 Deletable even with ORDER BY clause
60
SELECT ordernum INTO ordernum
FROM OLD TABLE(DELETE -- 1
FROM (SELECT FROM orders -- 2
ORDER BY ordernum -- 3
FETCH FIRST ROW ONLY)) -- 4
Could also use a separate SELECT INTO (which would still use features 3
and 4) followed by searched DELETE
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Example ndash Top 2 per department
Retrieve the top 2 wage earners by workdept
SQL features used 1 Nested table expression in a FROM clause
2 RANK window function
61
SELECT EMPNO WORKDEPT WAGES RANK_WAGES
FROM (SELECT EMPNO WORKDEPT SALARY+BONUS AS WAGES -- 1
RANK() OVER (PARTITION BY WORKDEPT -- 2
ORDER BY SALARY+BONUS DESC)
AS RANK_WAGES
FROM EMPLOYEE) AS RANK_BY_DEPT
WHERE RANK_WAGES lt 3 ORDER BY WORKDEPT RANK_WAGES
Nested table expression was used to compute the rank before the rank could
be used in the WHERE clause Common table expression could also be used
Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation
Powerful SQL ndash Are you using it
When faced with an SQL challenge
ndashConsider the depth of available SQL capabilities
ndashExplore the extensible nature of SQL for creative solutions
ndash If one approach encounters a restriction or missing feature look for
another way
ndashShare your creative solutions with other SQL users bull For example go ahead and send your solution to me and I can post it on
sqltips4db2com (giving you credit for it of course)
62 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation 63 Need webcast troubleshooting help Click attachments
DB2 Tech Talk Next Steps Roadmap - SQL Update DB2 105
Read an overview of SQL capabilities in DB2 105
bull What‟s new in DB2 105 with BLU Acceleration article ibmco17DNcBZ
bull Run Oracle Apps on DB2 10 for Linux UNIX and Windows ibmcoJQq4Xq
Familiarize yourself with the references
bull SQL Reference for Cross-Platform Development ibmco1dQQupT
bull DB2 105 Information Center bitlyDB2105Infocenter
bull SQL compatibility what‟s new under product overviews then DB2 for LUW then what‟s new
bull See also SQL support and PLSQL support under DB2 Application Development
Test drive the features
bull DB2 105 trial software Ibmcodb2download
bull IBM Data Studio to create run edit SQL queries and more bitlyIBMDS
Follow Rick at SQLTips4DB2com Blog
bull wwwsqltips4db2com
bull See these posts in particular
bull Distinctly weak type rules with a constrained twist
bull In the PIPE Compiled SQL PL and PLSQL table functions
Reference
Call IBM to schedule a demo or learn more
bull 1 800 966-9875 (US)
bull 1-888-746-7426 (Canada)
bull 1800-425-3333 (India)
bull Or visit httpwwwibmcomplanetwide for contact information worldwide
IBM DB2 105 product page
Ibmcomdb2
IBM DB2 105 Product features
ibmco12c1PJz
IBM Data Studio product page
Ibmcoibmdsinfo
Tech forum on developerWorks
bitlydb2forumluw
Rick Swagerman SQL Tips Blog
wwwsqltips4db2com
Step Three
Step Two
Step One
Step Four
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation 64 Need webcast troubleshooting help Click attachments
Upcoming Tech Talks Donrsquot miss these in-depth DB2 101 feature talks
Dates and topics subject to change and modification
How to register
DB2 Tech Talks web site
Next DB2 Tech Talk DB2 with BLU Acceleration Competitive Advantages
bull September 19 2013 at 1230 PM
bull Chris Eaton Worldwide Technical Specialist
Database amp Workload Optimized Systems
bull Registration bitlytt2013sep
IDUG DB2 Tech Conference Australia and
Barcelona
bull In-person conference sponsored by IDUG
bull September amp October respectively
bull Agenda and registration
wwwidugorg select events
No-charge DB2 105 Technical Immersion Workshop
at IDUG Barcelona
bull October 17 2013
bull At IDUG Conference Barcelona Spain
bull Information and Registration bitlydb2barcelona
Big Data and Analytics Tutorial Series
IBM IOD conference
bull Expand your skills for the next-generation of applications
bull November 3rd and November 7th
bull Information and registration ibmcoIODtutorials
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation 65 Need webcast troubleshooting help Click attachments
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation 66 Need webcast troubleshooting help Click attachments
Questions
Listening in replay
Questions wwwsqltips4db2com
Click submit a question
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment
copy 2013 IBM Corporation 67 Need webcast troubleshooting help Click attachments
Thanks for attending
Please rate the session
Presentation download bitlyttfileaug2013
click Attachments in this webcast environment