DB2 9 作為業界首個混合數據服務器,在關系引擎基礎上引入優化 XML 存儲引擎,原生支持 XML 文檔存儲、索引、事務、安全與 XQuery 查詢。XML 特點:自描述(標記與數據共存)、層次化(體現實體關系)、序列化(順序敏感),與關系模型(集合、扁平、靜態模式)互補。
-- CLP
db2 create db aromadb using codeset utf-8 territory us
-- Control Center: 勾選 "Enable database for XML",Code set 選 UTF-8
注意:XML 僅支持 UTF-8 單分區數據庫。
SALES.COMMENTS 列存儲客戶反饋 XML:評論 ID、客戶信息、反饋類型/內容、評分、商店響應。示例片段:
<Comments>
<comment>
<comment_ID>5301</comment_ID>
<customer_info><fname>Scott</fname><lname>Phillips</lname><email>Scott_Phillips@hotmail.com</email></customer_info>
<feedback><type>opinion</type><content>Gold Tips was highly enjoyable!</content></feedback>
<store_rating><score>5</score><out_of>5</out_of></store_rating>
<store_response><required>no</required></store_response>
</comment>
...
</Comments>
FLWOR 表達式:類似 SQL SELECT-FROM-WHERE,迭代序列並可選返回計算值。子句關鍵字:for(迭代綁定)、let(變量賦值)、where(過濾)、order by(排序)、return(結果構造)。
路徑表達式:斜槓分隔步驟導航層次,每步可帶謂詞過濾。如 $comments/comment/feedback[type="suggestion"]/content。
執行 XQuery 須在語句首加 xquery 關鍵字(僅頂層語言需加,嵌入 SQL 時不需)。數據源函數 db2-fn:XMLcolumn('SCHEMA.TABLE.COLUMN') 返回列中 XML 文檔序列(名稱需大寫匹配目錄)。
-- FLWOR
xquery for $y in db2-fn:XMLcolumn('AROMA.SALES.COMMENTS') return $y
-- 路徑
xquery db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')
xquery db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')/Comments/comment/feedback/content
text()xquery db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')/Comments/comment/feedback/content/text()
-- FLWOR where
xquery for $y in db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')/Comments/comment/feedback
where $y/type = "suggestion"
return $y/content/text()
-- 路徑謂詞
xquery db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')/Comments/comment/feedback[type="suggestion"]/content/text()
邏輯操作符 or/and 同 SQL。
xquery
<ul> {
for $y in db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')/Comments/comment/store_response
where $y/completed = "no"
return <li>{$y/action/text()}</li>
} </ul>
花括號 {...} 指示計算表達式而非字面量;構造器直接生成目標 XML/HTML。
if-then-elsexquery for $y in db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')/Comments/comment
where $y/store_response/completed = "no"
return
if ($y/feedback/type = 'suggestion')
then <suggestion>{$y/comment_ID, $y/customer_info, $y/store_response/action}</suggestion>
else <question>{$y/comment_ID, $y/customer_info, $y/store_response/action}</question>
大小寫敏感:元素名須精確匹配(如 comment_ID)。
order byxquery for $y in db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')/Comments/comment
where $y/feedback/type = "opinion"
order by $y/store_rating/score
return <comment><rating>{$y/store_rating/score/text()}</rating><opinion>{$y/feedback/content/text()}</opinion></comment>
let + distinct-values + count()xquery for $t in distinct-values(db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')/Comments/comment/feedback/type)
let $tc := db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')/Comments/comment[feedback/type = $t]
return <comments><type>{$t}</type><count>{count($tc)}</count></comments>
結果:opinion 1220、question 364、suggestion 251。
avg() 等xquery avg(db2-fn:XMLcolumn('AROMA.SALES.COMMENTS')/Comments/comment/store_rating/score)
-- 返回 4.113896...
-- 同類:sum、max、min、count
db2-fn:sqlqueryxquery for $y in
db2-fn:sqlquery(
'SELECT Comments FROM aroma.sales a, aroma.store b
WHERE city = ''New York'' AND a.storekey = b.storekey'
)/Comments/comment/feedback/content/text()
return $y
SQL 字符串內單引號雙寫轉義(''New York'');db2-fn:sqlquery 執行 SQL 返回 XML 序列供 XQuery 後續處理。
XQuery 與 SQL 非互斥,可組合發揮最大威力:SQL 關聯篩選行,XQuery 深度解析行內 XML。pureXML 讓 DB2 成為處理混合工作負載的理想平台。
