4) Working With DB2 Data Using SQL and XQuery
4) Working With DB2 Data Using SQL and XQuery
2
IBM DB2 9
3
IBM DB2 9
4
IBM DB2 9
To change data.
- DELETE
5
IBM DB2 9
- FROM TABLE(S)
- WHERE condition
- GROUP BY column
- HAVING condition
- ORDER BY column
6
IBM DB2 9
Examples -
SELECT * FROM staff;
SELECT * FROM staff FETCH FIRST 10 ROWS
ONLY;
SELECT name, salary FROM staff;
SELECT DISTINCT dept, job FROM staff;
SELECT name, salary + comm AS pay
FROM staff;
7
IBM DB2 9
- IN and NOT IN
9
IBM DB2 9
10
IBM DB2 9
13
IBM DB2 9
15
IBM DB2 9
16
IBM DB2 9
17
IBM DB2 9
18
IBM DB2 9
INSERT Statement
The INSERT statement is used to add new rows to a
table or a view.
Examples:
INSERT INTO staff VALUES
(1212,'Cerny',NULL,'Sales',3)
UPDATE Statement
The UPDATE statement is used to change the data in a
table or a view.
For example:
UPDATE staff SET dept = NULL
WHERE ename LIKE ‘A%’
UPDATE staff SET (dept, sal)=(51, 7000)
WHERE id = 750
UPDATE employees SET (dept) = (SELECT
deptname FROM department WHERE deptno
= 1)
20
IBM DB2 9
DELETE Statement
The DELETE statement is used to delete entire rows
of data from a table.
For example:
21
IBM DB2 9
22
IBM DB2 9
Savepoints
A savepoint lets you selectively roll back a subset of actions
that make up a UOW without losing the entire transaction.
do some work;
savepoint A;
do some more work;
savepoint B;
do even more work;
savepoint C;
wrap it up;
roll back to savepoint B;
24
IBM DB2 9
26
IBM DB2 9
What is XML?
XML was designed to structure, store and to send
data/information.
At its core XML is text formatted in the form of tags and text
that follow a well-defined set of rules.
<? xml version=“1.0” ?>
This text may be stored/represented in: <purchaseOrder id=‘12345” secretKey=‘4x%$^’>
<customer id=“A6789”>
<name>John Smith Co</name>
<address>
- A normal file stored on disk <street>1234 W. Main St</street>
<city>Toledo</city>
<state>OH</state>
- A message being sent over HTTP <zip>95141</zip>
</address>
</customer>
<itemList>
- A character string in a programming language <item>
<partNo>A54</partNo>
<quantity>12</quantity>
- A CLOB (character large object) in a database </item>
<item>
<partNo>985</partno>
<quantity>1</quantity>
- Any other way textual data can be used </item>
</itemList>
</purchaseOrder>
27
IBM DB2 9
28
IBM DB2 9
29
IBM DB2 9
30
IBM DB2 9
XML specifications
DTD
describes
supersedes XML
documents
XSD describes
uses
searches searches transforms
XQuery XPath
XSLT
uses
uses
uses
31
IBM DB2 9
32
IBM DB2 9
33
IBM DB2 9
34
IBM DB2 9
XPath Expressions
35
IBM DB2 9
36
IBM DB2 9
37
IBM DB2 9
38
IBM DB2 9
39
IBM DB2 9
40
IBM DB2 9
41
IBM DB2 9
42
IBM DB2 9
43
IBM DB2 9
XML Facilities
XML data type for columns
- create s1.t1 (c1 int, c2 xml)
Language bindings for XML type in programming languages
- cobol, c, java, etc..
XML indexes
- create ix1 on s1.t1(c2) generate keys using pattern ‘/dept/emp/@empno’
An XML schema/DTD repository
Support for XQuery as a primary language as well as:
- Support for SQL within XQuery
- Support for XQuery with SQL
- Support for new SQL/XML functions
Performance, scale, and everything else they expect from a DBMS
44
IBM DB2 9
45
IBM DB2 9
48
IBM DB2 9
XML Functions
XMLQUERY(). function that enables you to execute an XQuery
expression from within an SQL context. It returns an XML
value, which is an XML sequence. This sequence can be empty
or it can contain one or more items.
XMLTABLE(). returns a table from the evaluation of XQuery
expressions; XQuery expressions normally return values as a
sequence, however, XMLTABLE() allows you to execute an
XQuery expression and return values as a table instead.
XMLEXISTS(). If it returns an empty sequence, XMLEXISTS
returns FALSE; otherwise, TRUE is returned. The XMLEXISTS
predicate can be used in the WHERE clauses of UPDATE,
DELETE, and SELECT statements.
49
IBM DB2 9
UPDATE customer
SET custinfo = XMLPARSE (DOCUMENT ‘<name>Jane
Doe</name>’ PRESERVE WHITESPACE)
WHERE empno = 11;
50
IBM DB2 9
51
IBM DB2 9
52
IBM DB2 9
54
IBM DB2 9
55
IBM DB2 9
56
IBM DB2 9
57
IBM DB2 9
58
IBM DB2 9
59
IBM DB2 9
Grazie Hebrew
Italian
Gracias Spanish
Russian
Obrigado
Thank
Portuguese
Arabic
Merci
French
Traditional Chinese
You English
Danke
German
Thai
Simplified Chinese Tamil
Japanese Korean
60