Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
© 2015 IBM Corporation
Overview of DB2 11 SQL Enhancements
Christopher J. Crone – IBM Distinguished Engineer, DB2 for z/OS
Agenda
TheSQLenhancementsaddedinDB211willbepresentedanddiscussed.Thetopicwillcoverwhytheenhancementsweremadeandhowtotakeadvantageofthesenewfeatures.
§ OverviewofDB2FamilySQLFunctionality§ Introduction toDB211Enhancements§ Summary
2
DB2 SQL
3
z
luw
common
DB2 10 for z/OS and DB2 10.1 Linux, Unix & Windows(not exhaustive, some features may be missing)
Multi-row INSERT, FETCH & multi-row cursor UPDATE, dynamic scrollable cursors, GET DIAGNOSTICS, join across encoding schemes, IS NOT DISTINCT FROM, VARBINARY, FETCH CONTINUE, SELECT from MERGE, routine versions, time zone support
Inner and outer joins, table expressions, subqueries, GROUP BY, complex correlation, global temporary tables, CASE, 100+ built-in functions including SQL/XML, limited fetch, insensitive scrollable cursors, UNION everywhere, MIN/MAX single index, self-referencing updates with subqueries, sort avoidance for ORDER BY, row expressions, CALL from trigger, statement isolation, range partitioning, 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, UPDATE or DELETE, INSTEAD OF trigger, SQL PL in routines, file reference variables, XML, FETCH FIRST & ORDER BY in subselect/fullselect, EXCEPT, INTERSECT, MERGE, BIGINT, caseless comparisons, not logged tables, text search functions, spatial, data compression, DECFLOAT, optimistic locking, ROLE, TRUNCATE, index & XML compression, created temps, inline LOB, administrative privileges, implicit cast, datetime enhancements, currently committed, moving sum & average, index include columns, row and column access controls, time travel query, trusted contexts Updateable UNION in views, GROUPING SETS, ROLLUP, CUBE, more built-in functions, SET CURRENT ISOLATION, MDC, XQuery, XML enhancements, global variables, temp table compression, user-defined array types
DB2 SQL
4
z
luw
common
DB2 11 for z/OS and DB2 10.5 Linux, Unix & Windows(not exhaustive, some features may be missing)
Multi-row INSERT, FETCH & multi-row cursor UPDATE, dynamic scrollable cursors, GET DIAGNOSTICS, join across encoding schemes, IS NOT DISTINCT FROM, VARBINARY, FETCH CONTINUE, SELECT FROM MERGE, routine versions, time zone support, transparent archive query, accelerated tablesInner and outer joins, table expressions, subqueries, GROUP BY, complex correlation, global temporary tables, CASE, 100+ built-in functions including SQL/XML, limited fetch, insensitive scrollable cursors, UNION everywhere, MIN/MAX single index, self-referencing updates with subqueries, sort avoidance for ORDER BY, row expressions, CALL from trigger, statement isolation, range partitioning, 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, UPDATE or DELETE, INSTEAD OF trigger, SQL PL in routines, file reference variables, XML, FETCH FIRST & ORDER BY in subselect/fullselect, EXCEPT, INTERSECT, MERGE, BIGINT, caseless comparisons, not logged tables, text search functions, spatial, data compression, DECFLOAT, optimistic locking, ROLE, TRUNCATE, index & XML compression, created temps, inline LOB, administrative privileges, implicit cast, datetime enhancements, currently committed, moving sum & average, index include columns, row and column access controls, time travel query, trusted contexts, global variables, GROUPING SETS, ROLLUP, CUBE, DROP COLUMN, user-defined array types, XQuery
Updateable UNION in views, more built-in functions, SET CURRENT ISOLATION, MDC, XML enhancements, additional user-defined types (row and cursor), MODULEs, column organized tables (BLU Acceleration), parameter defaults
INTRODUCTION TO DB2 11 ENHANCEMENTS
SQL PL ARRAY DATA TYPE
7
Review of SQL Procedures• What is an SQL Stored Procedure?
•AstoredprocedurethatcontainsonlySQLstatements• MayuseSQLcontrol statements towritethelogicpartoftheprogram(e.g.WHILE,IF,GOTO,REPEAT,etc.)
• SQLProcedural LanguageorSQLPL
• Two types of SQL Stored Procedures•ExternalSQLProcedures(fromV5on)- GeneratedCprogramwhichrunsinaWLMenvironment
•Native SQLProcedures(fromDB29on)- TheSQLprocedure logicrunsintheDBM1addressspace
8
Native SQL Procedure Processingz/OSAppl pgm
CALL SP1
DB2DBM1
DDF
Appl pgm
CALL SP1The image part with relationship ID rId3 was not found in
SQL PL native logicSQLSQL
SP1DB2
directory
EDM pool
SQL PL native logicSQLSQL
SP1
*native SQL procedures do not run IN the WLM address space but are still running UNDER the WLM
9
Array Data Type Overview• DB2 for z/OS introduces support for Arrays
inside SQL Routines (UDFs or Procedures). •PriortoDB211,usersmighthaveutilizedthefollowingmechanismstopassdatato/fromRoutines:
• DECLAREDGLOBALTEMPORARYTABLE,• ConcatenatedStrings,• Longlistsofparameters,or…
•TheArrayDataTypeconsistsofanorderedsetofelementshavingthesamedatatype(length,precision,scale,CCSIDasappropriate)
• TwoTypesofArraysareSupported:• OrdinaryArrays
•Haveamaximumcardinality(2Billion)•ThedatatypeoftheindexvalueisINTEGER
• AssociativeArrays•Donot haveadefinedupperboundonthenumberofelements•ThedatatypeoftheindexvaluescanbeanINTEGERorcharacterstring(notaLOB)
10
Creating an Array Type• CREATE Ordinary and Associative arrays via CREATE TYPE
statement
-- ordinary array with integer value & integer indexCREATE TYPE OrdIntArray AS INTEGER ARRAY[100];-- associative array with integer value & integer indexCREATE TYPE AssocIntArray AS INTEGER ARRAY[INT];-- associative array with integer value & varchar indexCREATE TYPE AssocIntArrayVarIdx AS INTEGER ARRAY[VARCHAR(13)];
If data-type2 is specified, then array is an
Associative Array
All types except XML
11
Creating a Native SQL Routine with Array ArgumentsCREATE FUNCTION (SQL scalar)
Arrayelementsmayonlybesentinasanargumenttoafunction(whole)Arraysmaybepassed inasanargumentoraresultofafunction
CREATE PROCEDURE (SQL – native)ArrayelementsmayonlybesentinasanINparm toaprocedure(whole)Arraysmaybepassed inasIN,OUT,orINOUTparms foraprocedure
-- create a UDF that accepts and returns an ordinary array CREATE FUNCTION UDF1 (P1 OrdIntArray, P2 INTEGER)RETURNS OrdIntArrayBEGIN…
END;
-- create a procedure that accepts and returns an associative arrayCREATE PROCEDURE PROC2 (IN InP1 INTEGER,
IN InP2 AssocIntArrayVarIdx,INOUT InOutP1 AssocIntArrayVarIdx, OUT OutP1 AssocIntArrayVarIdx)
BEGIN…
END;-- ordinary array with integer value & integer indexCREATE TYPE OrdIntArray AS INTEGER ARRAY[100];-- associative array with integer value & varchar indexCREATE TYPE AssocIntArrayVarIdx AS INTEGER ARRAY[VARCHAR(13)];
12
Declaring Array VariablesDECLARE Ordinary and Associative arrays via SQL-variable-declaration
-- create a UDF that accepts and returns an ordinary array CREATE FUNCTION UDF1 (P1 OrdIntArray, P2 INTEGER)RETURNS OrdIntArrayBEGIN
-- declare two ordinary array with same array typeDECLARE myOrdIntArray OrdIntArray;DECLARE my2ndOrdIntArray OrdIntArray;…
END;
-- ordinary array with integer value & integer indexCREATE TYPE OrdIntArray AS INTEGER ARRAY[100];-- associative array with integer value & varchar indexCREATE TYPE AssocIntArrayVarIdx AS INTEGER ARRAY[VARCHAR(13)];
-- create a procedure that accepts and returns an associative arrayCREATE PROCEDURE PROC2 (IN InP1 INTEGER,
IN InP2 AssocIntArrayVarIdx,INOUT InOutP1 AssocIntArrayVarIdx, OUT OutP1 AssocIntArrayVarIdx)
BEGIN-- declare two associative arrays with the same array typeDECLARE myAssocIntArray AssocIntArrayVarIdx;DECLARE myAssocIntArray AssocIntArrayVarIdx;…
END;
-- ordinary array with integer value & integer indexCREATE TYPE OrdIntArray AS INTEGER ARRAY[100];-- associative array with integer value & varchar indexCREATE TYPE AssocIntArrayVarIdx AS INTEGER ARRAY[VARCHAR(13)];
13
Assigning values to ArraysConstruct arrays with array-constructor and assign values with SET assignment-statement
-- create a UDF that accepts and returns an ordinary array CREATE FUNCTION UDF1 (P1 OrdIntArray, P2 INTEGER)RETURNS OrdIntArrayBEGIN
-- declare two ordinary array with same array typeDECLARE myOrdIntArray OrdIntArray;DECLARE my2ndOrdIntArray OrdIntArray;-- set an ordinary array with 3 values using an array-constructorSET myOrdIntArray = ARRAY[10,20,30];
-- set a 2nd ordinary array’s array element, using array-element-specificationSET my2ndOrdIntArray[1] = myOrdIntArray[3];…
END;
my2ndOrdIntArrayIndex Value
1 30
myOrdIntArrayIndex Value1 102 203 30
14
Build an Array from Table DataARRAY_AGG Aggregate FunctionThe ARRAY_AGG function returns an array where each value of the input set is assigned to an element of the array.
CREATE TYPE OrdVchArray AS VARCHAR(12) ARRAY[100];
DECLARE myOrdVchArray OrdVchArray;
SET myOrdVchArray = (SELECT ARRAY_AGG(PHONE) FROM EMPLOYEE WHERE ID IS NOT NULLORDER BY ID, PHONE);
EMPLOYEEID PHONE
111222 408-555-1111333444 408-555-3331222333 408-555-2222111222 408-555-1112333444 408-555-3332
myOrdVchArrayIndex Value
1 408-555-11112 408-555-11123 408-555-22224 408-555-33315 408-555-3332
15
Treating an Array Like a TableTreat an array like a table i.e. fetch data from the array (using UNNEST collection-derived-table) just like you would from a table
from-clausecollection-derived-table
Acollection-derived-tablecanbeusedtoconverttheelements ofoneormorearraysintocolumnvalues inseparaterowsofanintermediate resulttable
Associative Array ExampleSELECT T.State, T.PopulationFROM UNNEST(myAssocIntArray) AS T(State,Population);
myAssocIntArrayIndex Value
California 2000000Oregon 140000
State PopulationCalifornia 2000000Oregon 140000
16
Deleting Elements From An Array§ ARRAY_DELETE Scalar Function§ The ARRAY_DELETE function deletes elements from an array.§ The result of the function has the same data type as array-expression.
myAssocIntArrayIndex Value
California 2000000New Hampshire 18000New York 1250000
-- create a procedure that accepts and returns an associative arrayCREATE PROCEDURE PROC2 (IN InP1 INTEGER,
IN InP2 AssocIntArrayVarIdx,INOUT InOutP1 AssocIntArrayVarIdx, OUT OutP1 AssocIntArrayVarIdx)
BEGIN-- declare two associative arrays with the same array typeDECLARE myAssocIntArray AssocIntArrayVarIdx;-- set an associative array with 5 valuesSET myAssocIntArray['California'] = 2000000;SET myAssocIntArray['Oregon'] = 140000;SET myAssocIntArray[New York'] = 1250000;SET myAssocIntArray[‘New Hampshire'] = 18000;
-- remove the state of ‘Oregon’ from the arraySET myAssocIntArray = ARRAY_DELETE(myAssocIntArray,’Oregon’);-- remove all states that begin with ‘New’ from the arraySET myAssocIntArray = ARRAY_DELETE(myAssocIntArray, ’New Hampshire’, ‘New York’);…
END;
myAssocIntArrayIndex Value
California 2000000New Hampshire 18000New York 1250000Oregon 140000
myAssocIntArrayIndex Value
California 2000000
17
Finding an Array Element IndexName DescriptionARRAY_FIRST returns the minimum array index value of the arrayARRAY_LAST returns the maximum array index value of the arrayARRAY_NEXT returns the next larger array index value for an array relative to
the specified array index argumentARRAY_PRIOR returns the next smaller array index value for an array relative to
the specified array index argument-- create a procedure that accepts and returns an associative arrayCREATE PROCEDURE PROC2 (IN InP1 INTEGER,
IN InP2 AssocIntArrayVarIdx,INOUT InOutP1 AssocIntArrayVarIdx, OUT OutP1 AssocIntArrayVarIdx)
BEGIN-- declare local varchar variableDECLARE myVarchar, my2ndVarchar VARCHAR(20);-- declare two associative arrays with the same array typeDECLARE myAssocIntArray AssocIntArrayVarIdx;-- set an associative array with 2 valuesSET myAssocIntArray['California'] = 2000000;SET myAssocIntArray['Oregon'] = 140000;
-- sets a local variable to the index that follows ‘California’SET myVarchar = ARRAY_NEXT(myAssocIntArray,’California’);…
END;
myVarchar = ‘Oregon’
AUTONOMOUS TRANSACTIONS
19
Autonomous Transactions• An Autonomous Transactions is:
• AnativeSQLprocedure thatrunsinitsownunitofwork• MayperformSQL,COMMITs,andROLLBACKit’sownSQL
• Nouncommittedchangesfromit’scallerareseen• Locksarenotsharedbetween thecallerandtheautonomousprocedure• Uponcompletionoftheautonomousprocedure,aCOMMITisdriven fortheautonomousprocedurewhenSQLCODE>=0
• Thecaller’sworkisuntouched
• Useful foreventorauditlogs
z/OSAppl pgm
DB2DBM1
AutoTran_STP:BEGINUPDATE T2 …
END;
INSERT INTO T1 …
CALL AutoTran_STP;
ROLLBACK;
T1 modifications unseen
T2 committed but not T1
AutoTran_STP:BEGINUPDATE T2 …
END;
20
Creating & displaying autonomous transactions• Specify AUTONOMOUS keyword on CREATE PROCEDURE
or ALTER PROCEDURE statement’s option-list
• Display autonomous transaction state with -DISPLAY THREAD(*) command
16.32.57 DB1G DISPLAY THREAD(*)16.32.57 STC00090 DSNV401I DB1G DISPLAY THREAD REPORT FOLLOWS -16.32.57 STC00090 DSNV402I DB1G ACTIVE THREADS -NAME ST A REQ ID AUTHID PLAN ASID TOKENBATCH SP * 1 APPLX USER001 PL01AP01 0025 30
AT * 641 APPLX USER001 PL01AP01 002A 13
GLOBAL VARIABLES
22
Global Variables Overview• Benefits
•AllowsuserstoshareinformationacrossSQLstatements•Allowscapabilitytosetonce,useeverywhere•ProvidesadditionalsecurityviaDB2GRANTandREVOKE
• Characteristics•Similartospecialregisters•Theirdefinitions areglobalandsharedacrossdifferentconnections
•Theircontents areonlysharedwithinthesameconnection• Eachconnectionmaintainsitsowninstantiation
•TheircontentsareNOTaffectedbyCOMMITnorROLLBACK•UseonlytheUNICODEencodingscheme
23
Writing and Reading Global Variables• WRITE to via:
•SET,SELECT INTO,VALUES INTO,OUTorinOUT parms• READ from via:
•Anywhere anexpressioncanbespecified•Exceptions
• checkconstraints,MQTs,viewswithWITH CHECKOPTION,keys(indexonexpressions, XMLindexes, spatialindexes),arrays
•Valuesarelockedinassoonasthefollowing:• Viewdefinition• SQLscalarUDFortableUDFbody• Triggeraction• Rowpermissiondefinition• Columnmaskdefinition• Auto-bindingapackage
24
Using and Referencing a Global VariableCreateaglobalvariablevianewCREATEVARIABLEstatement
Settingandreferencingaglobalvariable-- create a global variableCREATE VARIABLE Charge_Rate DECIMAL(4,2) DEFAULT 0.00;
-- create a procedure that determines charge rate CREATE PROCEDURE Loan_Charge_Rate (IN ID CHAR(5))BEGIN.. SELECT SCORE INTO Cust_Score FROM CUSTOMER WHERE ACCOUNT = ID;IF Cust_Score = ‘Good’ THEN SET Charge_Rate = 1.0;ELSE SET Charge_Rate = 3.0;
END;
-- calling applicationmyApp: PROCEDURE(Buyer, Amount);…CALL Loan_Charge_Rate(Buyer);UPDATE CUSTOMER SET BALANCE = BALANCE + (Amount * Charge_Rate);
END myAPP;
GROUPING SETS
Enhanced subselect group-by-clause
group-by-clause
subselect
Syntax: grouping-sets and super-groupsgrouping-sets
super-groups
28
New Aggregations – Grouping Sets§ Grouping sets allow multiple groups to be specified§ Querying and reporting are made easier and faster by producing a single
result from essentially performing a UNION ALL of two or more groups of rows
§ group-by-clause has been enhanced:
• grouping-sets• Allowsmultiplegroupingclauses tobespecifiedinasinglestatement• Allowsthegroupstobecomputedwithasinglepassoverthedata• Canbeusedtodeterminesubtotalsandgrandtotals
• i.e.foragivengroup,calculate it’ssubtotalaswellasthegrandtotalforallgroups
• super-groups• Pre-defined grouping-sets
• ROLLUP• CUBE
29
Predefined Grouping Sets - ROLLUP§ ROLLUP
• Createssubtotalsthat‘rollup’fromthemostdetailed leveltoagrandtotal• Nelements translatetoN+1groupingsets:
GROUP BY ROLLUP (C1,C2,C3) GROUP BY GROUPING SETS ((C1,C2,C3), (C1,C2), (C1), ());
– The order specified is significant to the result:
equivalent
GROUP BY ROLLUP (C1,C2)GROUP BY GROUPING SETS ((C1,C2),
(C1), ());
equivalent
GROUP BY ROLLUP (C2,C1)GROUP BY GROUPING SETS ((C2,C1),
(C2), ());
equivalent
versus
30
Predefined Grouping Sets - CUBECUBE
• Createssubtotalsforallpermutations• Nelements translateto2n groupingsets:
• UnlikeROLLUP,theorderspecified isNOTsignificant totheresult• i.e.useORDERBYtoguaranteetheorder
grand-total• theoverallaggregationofprevioussubtotals• BothCUBEandROLLUPreturnthisrowasthelastrow
GROUP BY CUBE (C1,C2,C3)GROUP BY GROUPING SETS (
(C1,C2,C3), (C1,C2), (C1,C3), (C2,C3), (C1), (C2), (C3), ());
equivalent
8 groups3 elements
grand-total
31
GROUPING Aggregate Function
§ Used to indicate whether or not the row was returned as a result of a grouping set
• i.e.theGROUPINGaggregatefunctiontellsyoutheresultofthegroupingsetwasNULLversusthesourcerowofthegroupingsetwasNULL
• 1• theNULLvaluewasgeneratedbyasuper-group
• 0• theNULLvalueisfromthesourcerow
32
GROUP BY GROUPING SETS - ExampleExample: Create a result set from the SALES table based on person and date. SELECT WEEK(SALES_DATE) as WEEK, DAYOFWEEK(SALES_DATE) AS DAY,
SALES_PERSON, SUM(SALES) AS SOLDFROM SALESWHERE SALES_DATE > '1999-12-31’GROUP BY GROUPING SETS
(WEEK(SALES_DATE), DAYOFWEEK(SALES_DATE), SALES_PERSON)
33
GROUP BY ROLLUP§ An extension to the GROUP BY clause
Produces a result set that contains “sub-total” rows. The sequence of the columns is significant.
GROUP BY ROLLUP (a, b, c) is equal to
GROUP BY (a, b, c) + GROUP BY (a, b) + GROUP BY (a) + grand-total
Example
SELECT WEEK(SALES_DATE) AS WEEK, DAYOFWEEK(SALES_DATE) AS DAY,SALES_PERSON, SUM(SALES) AS SOLD
FROM SALESWHERE SALES_DATE > '1999-12-31'GROUP BY ROLLUP
(WEEK(SALES_DATE), DAYOFWEEK(SALES_DATE), SALES_PERSON)ORDER BY WEEK(SALES_DATE), DAYOFWEEK(SALES_DATE),
SALES_PERSON
34
GROUP BY ROLLUP results
35
GROUP BY CUBE§ An extension to the GROUP BY clause
Produces a result set that contains ROLLUP aggregation plus cross-tabulation rows.The sequence of the columns is not significant.
GROUP BY CUBE (a, b, c) is equal to
GROUP BY (a, b, c) + GROUP BY (a, b) + GROUP BY (a,c) + GROUP BY (b,c) + GROUP BY (a) + GROUP BY (b) + GROUP BY (c) + grand-total
Example
SELECT WEEK(SALES_DATE) AS WEEK, DAYOFWEEK(SALES_DATE) AS DAY,SALES_PERSON, SUM(SALES) AS SOLD
FROM SALESWHERE SALES_DATE > '1999-12-31'GROUP BY CUBE
(WEEK(SALES_DATE), DAYOFWEEK(SALES_DATE), SALES_PERSON)ORDER BY WEEK(SALES_DATE), DAYOFWEEK(SALES_DATE), SALES_PERSON
36
GROUP BY CUBE results
GROUP BY CUBE results contd.
TEMPORAL ENHANCEMENTS
39
Review - Temporal Based Analysis§ System-maintained temporal tables
• DB2generatedhistory• ASOFquery
§ User-maintained temporal tables
• Userprovidetimeperiod• Automaticbusinesstimekeyenforcement.• Queryoveranycurrent,anyprior,futurepoint/period inbusinesstime.
• Newtimerangeupdate/delete statements supportautomaticrowsplitting,exploitedbythemergestatements.
§ Bi-temporal, combination of the above twoSee http://www.redbooks.ibm.com/abstracts/sg247892.html?Open Chapter 7 Application Enablement for more information
Period Specification with Views (including Symmetric)
• Period specification with view reference – querieso STT: system-period temporal table; STT_HIST: history table of STTo ATT: application-period temporal table
o BTT: bitemporal table; BTT_HIST: history table of BTTo RT: non-temporal regular table
CREATE VIEW VW AS SELECT … FROM RT, ATT, STT, BTT;
SELECT … FROM VW FOR BUSINESS_TIME AS OF bus_value FOR SYSTEM_TIME AS OF sys_value;
SELECT … FROM(SELECT … FROM RT,
ATT FOR BUSINESS_TIME AS OF bus_value,STT FOR SYSTEM_TIME AS OF sys_value,BTT FOR BUSINESS_TIME AS OF bus_value
FOR SYSTEM_TIME AS OF sys_value) VW;
Introduction of Temporal Special Registers• Special Registers
o CURRENT TEMPORAL SYSTEM_TIME: TIMESTAMP(12), nullableo CURRENT TEMPORAL BUSINESS_TIME: TIMESTAMP(12), nullable
• SET Temporal Registers*** The value of the special register is sent to remote side for implicit connect ***
• References of Temporal RegistersSELECT CURRENT TEMPORAL SYSTEM_TIME FROM SYSIBM.SYSDUMMY1;
SELECT CURRENT TEMPORAL BUSINESS_TIME FROM SYSIBM.SYSDUMMY1;
SET CURRENT TEMPORAL SYSTEM_TIME = TIMESTAMP('2008-01-01') + 5 DAYS ;SET CURRENT TEMPORAL SYSTEM_TIME = CURRENT TIMESTAMP – 1 YEAR ;SET CURRENT TEMPORAL SYSTEM_TIME = NULL ;
SET CURRENT TEMPORAL BUSINESS_TIME = TIMESTAMP('2008-01-01') + 5 DAYS ;SET CURRENT TEMPORAL BUSINESS_TIME = CURRENT TIMESTAMP – 1 YEAR ;SET CURRENT TEMPORAL BUSINESS_TIME = NULL ;
Auditing and Auditing with Temporal ExampleIn this example, user_id (SESSION_USER) and op_code (I/U/D) are audited. DB2 can audit a subset of special registers and session variables. The auditing is supported on regular table. When combined with temporal, it provides complete history for auditing.
CREATE bank_account_stt (account_no INT NOT NULL,balance INT, user_id VARCHAR(128) GENERATED ALWAYS AS (SESSION_USER), op_code VARCHAR(1) GENERATED ALWAYS AS (DATA CHANGE OPERATION),sys_start TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS ROW BEGIN, sys_end TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS ROW END, trans_id TIMESTAMP(12) GENERATED ALWAYS AS TRANSACTION START ID,...PERIOD SYSTEM_TIME(sys_start, sys_end));
CREATE bank_account_hist (account_no INT NOT NULL,balance INT, user_id VARCHAR(128), op_code VARCHAR(1),sys_start TIMESTAMP(12) NOT NULL, sys_end TIMESTAMP(12) NOT NULL, trans_id TIMESTAMP(12),...);
ALTER TABLE bank_account_stt ADD VERSIONING USE HISTORY TABLE bank_account_hist ON DELETE ADD EXTRA ROW;
Auditing ExampleAttime2,Claireinsertsarowintobank_account_stt table(accountno”13452’,balance$2000);
accountbalanceUserop-codesys_start sys_end---------- ---------- -------- ----------- ------------ -----------
basetable13452$2000ClaireI2MaxHistorytable(empty)
Attime6,Steveupdatestherowincreasing$500balance(account_no "13452",balance$2500):accountbalanceUserop-codesys_start sys_end---------- ---------- -------- ----------- ------------ -----------
Basetable:13452$2500SteveU6MaxHistorytable:13452$2000ClaireI26
Attime15,Rickdeletestherowofaccount_no "13452"(account_no "13452",balance$2500,removedfromcurrenttable):
accountbalanceUserop-codesys_start sys_end---------- ---------- -------- ----------- ------------ -----------
Basetable(empty)Historytable:13452$2000ClaireI26
13452$2500SteveU61513452$2500RickD1515
Basedonhistorytable,therowforaccount13452withbalance$2000wasinsertedbyClaireattime2,thebalancewasupdatedto$2500bySteveattime6,andwasdeletedbyRickattime15.
TRANSPARENT ARCHIVING
Transparent Archive Query
45
■ Applications can query current + archive with no SQL changes■ By default, data is retrieved from base table only, as usual■ Set a new global variable when archive data is desired■ DB2 automatically converts SQL to UNION ALL via dynamic plan
switching technique (high performance)■ Archiving process is user-controlled■ Move_To_Archive global variable allows DELETEs to be
automatically archivedSee http://www.redbooks.ibm.com/abstracts/sg248180.html?OpenChapter 7 for more information on Transparent Archiving
High performance, availability storage
Current data Archive
data
Cheaper storage
SUMMARY
46
Summary§ DB211buildsonSQLfunctionprovidedinpreviousreleaseswiththe
followingfocusareas• NativeSQLRoutines
• DB210addedNativeSQLUDFs• DB211addedtheARRAYdatatype foruseinSQLRoutines• DB211addedAutonomousSQLProcedures
• Analyticprocessing• DB29andDB210AddedOLAPSpecificationSupport• DB211AddedCube,Rollup,andGroupingSets
• Security• DB210added,andDB211enhanced,ColumnMasking
• SQLFamilyandVendorcompatibility extensions• Extensionsneededtorunpopularapplicationsrequestedbycustomersandvendors
• TemporalandArchiveTables• Supportcommonusepatterns
• APPLCOMPAT• Separatessystemmigrationfromapplicationimpact
47
Resources§ Requirements
•https://www.ibm.com/developerworks/rfe/§ DB2forz/OSInformationCenter
•http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp
§ DB211forz/OSTechnicalOverviewRedbook• http://publib-b.boulder.ibm.com/abstracts/sg248180.html?Open
48
© 2015 IBM Corporation
Thank YouOverview of DB2 11 SQL EnhancementsChristopher J. [email protected]