XQUERY for

wwwdb.inf.tu.dresden.de

XQUERY for

© Prof. Dr.-Ing. Wolfgang

Lehner |

12

XML, Identity Column and Sequences in

DB2


XML in DB2

XML in DB2 before Version 9

• as CLOB

• with XML-Extender: decomposition of XML documents on relational tables

XML in DB2 from Version 9 on

• data type XML for well defined XML documents

• http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.xml.doc/doc/c0023366.html

• internal storage format

• hierarchical storage

• queries via XQuery, SQL

or a combination

thereof

• XML indexes

C

O

M

P

I

L

E

R

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 2


XML Schema Repository

XML Schema

• information on the structure of the XML document

• nesting, frequency and attributes of elements as well as data type and value range

• can consist of multiple XML schema documents

XML Schema Repository (XSR) in DB2

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.xml.doc/doc/c0022711.html

• information for the validation and processing of XML documents

• catalog view SYSCAT.XSROBJECTS

• register the XML schema

• REGISTER XMLSCHEMA FROM AS


• possibly add further documents to the XML schema

• ADD XMLSCHEMA DOCUMENT TO ADD

FROM

• validate XML schema, complete registration

• COMPLETE XMLSCHEMA

• remove XML schema

• DROP XSROBJECT

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 3


Tables with XML Columns

XML columns

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0023655.html

• declared during CREATE TABLE as data type XML

• a table can contain one or more XML columns

• no length is defined

• similar to LOB columns, the XML columns also contain only a descriptor for the

column; the data is stored separately

• no XML Schema on the level of columns, but only per instance

Examples

• table with two XML columns:

• CREATE TABLE Customer (Cid BIGINT NOT NULL PRIMARY KEY,

Info XML, History XML)

• table with CHECK constraint

• CREATE TABLE TableValid (id BIGINT,

xmlcol XML,

CONSTRAINT valid_check CHECK (xmlcol IS VALIDATED))

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 4


Requirements and Restrictions

Requirements to the database

• Unicode database (UTF-8)

• no partitioning

Restrictions for XML columns

• not usable as keys (e.g., primary, foreign or unique key,…)

• not in relational indexes

• no default values or generated values

• restriction to 2 GB per value of type XML, so that there is an effective limit of 2 GB

per XML document

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 5


Insertion of XML Data

Insertion of XML data

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.xml.doc/doc/c0023466.html

• validation is optional

• implicit or explicit parsing of the data

• examples

• implicit parsing

INSERT INTO xmltab (xmlcol) VALUES ('...')

• explicit parsing

INSERT INTO xmltab (xmlcol) VALUES (XMLPARSE (DOCUMENT

'...' PRESERVE WHITESPACE))

• WHITESPACE – processing of invisible characters

between XML tags, DEFAULT: STRIP

• validation against registered schema mySchema

INSERT INTO xmltab (xmlcol) VALUES ( XMLVALIDATE (XMLPARSE

(DOCUMENT '...') ACCORDING TO XMLSCHEMA ID

myschema))

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 6


Modification and Deletion of XML Data

Modification of XML data

• Prior to V9.5: no modification on XML documents is allowed replace full

XML document

• UPDATE xmltab SET xmlcol='...'

• Since V9.5: modification of parts of the XML document possible with the

help of the XQuery Update Facility standard (extension of XQuery)

Deletion of XML data

• set column value to NULL

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 7


XML Export in DB2

Export of XML data

• XML data is stored in seperate file

• Replacement of XML data in export file with XML Data Specifier (XDS)

• XML-specific options for EXPORT

• XML TO - export path for seperate XML data, Default "."

• MODIFIED BY

XMLCHAR - Zeichensatz der XML-Daten in Zeichensatz der Datenbank

XMLINSEPFILES – every XML column of one tuple in separate file,

Default: all XML data in one file + offset und length in XDS

• Further Options: XMLFILE, XMLSAVESCHEMA

Example

• EXPORT TO OF DEL MODIFIED BY XMLINSEPFILES

XMLSAVESCHEMA

1000,"silver",""

1001,"gold",""

1002,"gold",""

1003,"gold",""

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 8


XML Import in DB2

Import von XML-Daten

• Validation optional

• In V9.1: only IMPORT possible, since V9.5 also LOAD

• XML-specific options for IMPORT / (LOAD)

• Path to (separated) XML data files: XML FROM , Default "."

• Data type: MODIFIED BY XMLCHAR

• Further options: XMLPARSE, XMLVALIDATE

Beispiel (see also IMPORT exercise)

• IMPORT FROM OF DEL XMLVALIDATE USING XDS


• IMPORT FROM OF DEL XMLVALIDATE USING SCHEMA



© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 9


XQuery

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 10


XQuery

XQuery

• XML query language standardized by the W3C

• Separate query language in DB2

• embedding of and into SQL possible

XQuery vs. SQL

Data Model

Results

Case sensitivity relevant on

object level

XQuery

hierarchical

ordered

sequence of XML data

(Xquery data model)

yes

SQL

flat, set-oriented

set of different SQL data

types

no

NULL values no yes

1 Datenaustausch, TPCH

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 11


XQuery Data Model

Xquery data model (XDM)

• instance of the XDM: sequence

• sequence: ordered set of zero or multiple elements

• element: atomic value, node, complete XML document

• atomic value: integrated type, defined by the XML schema

(xs:string, xs:integer, xs:decimal, xs:date, …)

• node: document node, element node, attribute node, text node…

Features

• no differentiation between a set with one element and the element itself

• no nested sequences

Examples

• (42, "text ", )

• 13

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 12


XPath

XPath

• part of XQuery

• navigation through the XML document

• defines an XDM sequence

Examples

• all e-mail addresses of all clients

• /Client/email

• first e-mail address of the client with ID 123

• /Client[@id="123"]/email[1]

• all e-mail addresses of all clients from California

• /Client/Address[state="CA"]/../email

• /Client[Address/state="CA"]/email

• all cities below the direct child node of all clients

• /Client/*/city

• all e-mail addresses in the full XML document

• //email


...


...

...



© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 13


FLOWR Expression

FLOWR

• complex queries to XML documents

for: iteration over input sequence, variable binding for each run

• elements of the sequence bound to a variable

• let: variable declaration and value assignment

• full value (or a sequence in some cases) assigend to the variable

• no iteration!

• order by: sorting

• where: filter

• return: result construction

Examples

for $i in (1,2,3) return {$i}

1, 2, 3

• let $i := (1,2,3) return {$i}

1 2 3

for $i in (3,2,1) where $i>1 order by $i return {$i}

2, 3

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 14


XML Queries in DB2

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 15


Example Scenario

Table person:

firstname lastname age address

Kathy Smith 32 …

Larry Menard 45 …

XML documents with address information


25 East Creek

Markham

905-555-7258

kathy.smith@whatever.com


© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 16


XQuery as Individual Query Language

XQuery as separate query language

• marked by the keyword XQUERY

• input data for XQuery via functions:

db2-fn:xmlcolumn or db2-fn:sqlquery

• Attention: table and column names in CAPITAL letters!

• function returns XML sequence

• can contain all XML values including atomic values and nodes

• not necessarily a sequence of correctly formatted documents

• may contain, e.g., a single atomic value (e.g., 36)

• http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.xml.doc/doc/xqrgetdata.html

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 17


Column Access from XQuery

db2-fn:xmlcolumn

• Returns a sequence from a column in the DB2 database connected at the respective

point in time

• Attention: table and column names in CAPITAL letters!

• http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnxmc.html

Examples

• phone numbers of all persons from Markham

XQUERY

for $addr in db2-fn:xmlcolumn('PERSON.ADDRESS')/address

where $addr/city = "Markham"

return $addr/phone

• or short:

XQUERY db2-fn:xmlcolumn('PERSON.ADRESS')/address[city =

"Markham"]/phone

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 18


SQL in XQuery

db2-fn:sqlquery

• Returns a sequence that is the result of an SQL-Fullselect command in the DB2

database connected at the respective point in time

• SQL query must return data of datatype XML

• single quotation marks have to be escaped

• http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnsqq.html

Examples

• phone number of Kathy Smith

XQUERY let $phone := db2-fn:sqlquery('

SELECT address

FROM person

WHERE firstname = ''Kathy'' AND lastname = ''Smith''

')/address/phone/text()

return {$phone}

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 19


XQuery in SQL

XQuery in SQL

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.xml.doc/doc/c0051454.html

• projection of XML data in SQL query result with XMLQUERY

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0022193.html

• generation of a relational table from XML data with XMLTABLE

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0022195.html

• restriction of the result set with XMLEXISTS

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0022228.html

Examples

• name and phone number for all persons age 40 and above for whom an e-mail

address has been provided

• SELECT firstname, lastname,

XMLQUERY('$doc/address/phone' PASSING address AS "doc")

AS phone

FROM person WHERE age > 40 AND

XMLEXISTS('$doc/address/email' PASSING address AS "doc")

• PASSING – columns, expressions or literals are passed on, separated by commas

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 20


XML Functions in SQL

XML functions (excerpt)

• XMLPARSE()

• character data, binary data XDM

• XMLSERIALIZE()

• XDM character data, binary data

• XMLTABLE()

• creation of relational tables with the help of XQuery

• XMLVALIDATE()

• validation of structure, content and data types of an XML document

• only XML schema (no DTD)

• XMLCAST()

• data type conversion between XML and SQL data types

• XMLEXISTS()

• check whether or not the Xquery expression returns a non-empty sequence

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 21


XML Indexes

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 23


XML Indexes in DB2

XML indexes

• on components of the XML document (not on full columns)

• identification of the part to be indexed via XML pattern (restricted Xpath

expression)

• possible on elements and attributes

• also support // and *

• declaration of an SQL data type for the indexed values required (DATE,

TIMESTAMP, VARCHAR or DOUBLE)

• only individual XML columns can be indexed (no multi-column indexes)

• multiple indexes for one XML column possible

Examples

• CREATE INDEX p_xml_idx ON person (address) GENERATE KEY

USING XMLPATTERN '/address/@country' AS SQL VARCHAR(20)

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000919.html

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 24


XML Indexes in DB2 (2)

Conditions for using indexes

• index on complex elements possible but has effects on the query

example:


Kathy

Smith


CREATE INDEX ... USING

XMLPATTERN '//name'

AS SQL VARCHAR(50)

for use of the index in a query, the predicate must be [name="KathySmith"]

• if index on text node query has to be to text node

• matching of the predicate type and the data type of the created index

relevant for DOUBLE (CREATE INDEX … AS SQL DOUBLE))

• /element[element = "value"] no index use

• /element[element = value ] index use

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 25


Identity Columns & Sequences

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 26


Identity Columns in DB2

Automatic generation of attribute values

• GENERATED keyword in CREATE TABLE Statement

• Is bound to one table column

• Column options

• GENERATED ALWAYS AS IDENTITY

• GENERATED ALWAYS AS ()

• GENERATED BY DEFAULT AS IDENTITY

• IDENTITY – continuous numbering

• () – arbitrary function, references to columns possible

• Example: … PAY INT GENERATED ALWAYS AS (SALARY+BONUS)…

• Recalculation of column PAY, if column SALARY or BONUS changes

• GENERATED BY DEFAULT

• Only generated value, if no value is specified in INSERT statement

• Generated value not unique in every case (see options)

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 27


Identity Columns in DB2 (2)

IDENTITY-Options

• START WITH: Start-value

• INCREMENT BY: Interval between succeeding values (default: 1)

• MINVALUE: stop value for decreasing sequence

• MAXVALUE: stop value for increasing sequence

• CYCLE: Restart of sequence on reaching minimal/maximal value multiple same

values possible

• CACHE

• Caching of values in memory for performance reasons, instead of catching every

value from system catalog (default: 20)

• Will get lost on system crashes gaps possible

• ORDER: Force values in order of queries

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 28


Identity Columns in DB2 (3)

Example

CREATE TABLE books(

id INT GENERATED ALWAYS AS IDENTITY(START WITH 100,

INCREMENT BY 10),

title VARCHAR(128)

)

• INSERT INTO sampletab VALUES ('DB2 DBA V9');

INSERT INTO sampletab VALUES ('DB2 Developer V9');

• Result:

ID

TITLE

------ ----------------

100 DB2 DBA V9

110 DB2 Developer V9

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 29


Sequences

Sequence

• database object for generating continuous values

• transactionally secured

• not bound to a specific table column (different to identity columns)

CREATE SEQUENCE

[START WITH ]

[INCREMENT BY ]

[NO MINVALUE | MINVALUE ]

[NO MAXVALUE | MAXVALUE ]

[NO CYCLE | CYCLE]

[NO CACHE | CACHE ]

[NO ORDER | ORDER]

• options: see Identity Column

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 30


Sequences (2)

How To Use

• NEXTVAL FOR : returns next value

• PREVVAL FOR : returns current value

Example

CREATE SEQUENCE ORG_SEQ

START WITH 1

INCREMENT BY 1

NO MAXVALUE

NO CYCLE

CACHE 24

VALUES(NEXTVAL FOR ORG_SEQ);

1. Execution 2. Execution

1

--

1

--

1 2

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 31


Conclusion

XQuery data model and XMLSchema

• Sequences, elements, nodes

• Validation of XML data

DML and Exchange of XML data

• Specific considerations with the handling of XML data within DB2

XQuery

• Standalone or embeded in DB2 queries

XML-Indexes

• Query optimization with indexes on XML documents

Some changes from DB2 V9.1 DB2 V9.5

• XQuery Update Facility in V9.5

• LOAD for XML data in V9.5

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 32


DB2 Certification

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 33


DB2 9 Fundamentals Certification (730)

When?

• Wednesday, 1 February 2012

• 11:00 - 15:00

Where?

• Room INF/E042

Requirements

• Exercise points > 70 %

• ID Card (e.g. driver licence or passport)

• Prometric ID

Preliminary actions

• Registration on

https://ibt.prometric.com/ibm

• Test provider: "IBM Information Management Certification"

• See also: http://wwwdb.inf.tudresden.de/files/WS0910/TIS/certification_guideline.pdf

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 34


Testing Interface

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 35


Test 730:

DB2 9 Family Fundamentals

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 36


Test Details

Test

• 64 questions in 6 parts

• 90 minutes

• 59% or better passed

Website

• http://www-03.ibm.com/certify/tests/obj730.shtml

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 37


Section 1 (14%)

Section 1 – Planning

• Knowledge of DB2 products (client, server, etc.)

• Knowledge of the features or functions available in DB2 tools (just tools that come

with product - distributed +space - i.e., control center, configuration advisor,

configuration assistant, command line processor)

• Knowledge database workloads (OLTP vs. warehousing)

• Knowledge of non-relational data concepts (extenders)

• Knowledge of XML data implications (non-shredding)

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 38


Section 2 (11%)

Section 2 - Security

• Knowledge of restricting data access

• Knowledge of different privileges and authorities

• Knowledge of encryption options (data and network)

• Given a DDL SQL statement, knowledge to identify results (grant/revoke/connect

statements)

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 39


Section 3 (17%)

Section 3 - Working with Databases and Database Objects

• Ability to identify and connect to DB2 servers and databases

• Ability to identify DB2 objects

• Knowledge of basic characteristics and properties of DB2 objects

• Given a DDL SQL statement, knowledge to identify results (ability to create objects)

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 40


Section 4 (23.5%)

Section 4 - Working with DB2 Data using SQL

• Given a DML SQL statement, knowledge to identify results

• Ability to use SQL to SELECT data from tables

• Ability to use SQL to SORT or GROUP data

• Ability to use SQL to UPDATE, DELETE, or INSERT data

• Knowledge of transactions (i.e., commit/rollback and transaction boundaries)

• Ability to call a procedure or invoke a user defined function

• Given an XQuery statement, knowledge to identify results

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 41


Section 5 (23,5%)

Section 5 - Working with DB2 Tables, Views and Indexes

• Ability to demonstrate usage of DB2 data types

• Given a situation, ability to create table

• Knowledge to identify when referential integrity should be used

• Knowledge to identify methods of data constraint

• Knowledge to identify characteristics of a table, view or index

• Knowledge to identify when triggers should be used

• Knowledge of schemas

• Knowledge of data type options for storing XML data

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 42


Section 6 (11%)

Section 6 - Data Concurrency

• Knowledge to identify factors that influence locking

• Ability to list objects on which locks can be obtained

• Knowledge to identify characteristics of DB2 locks

• Given a situation, knowledge to identify the isolation levels that should be used

© Prof. Dr.-Ing. Wolfgang Lehner | 12 XML and XQuery | 43

More magazines by this user
Similar magazines