
- DB2 教程
- 主頁
- DB2 - 簡介
- DB2 - 伺服器安裝
- DB2 - 例項
- DB2 - 資料庫
- DB2 - 緩衝池
- DB2 - 表空間
- DB2 - 儲存組
- DB2 - 模式
- DB2 - 資料型別
- DB2 - 表格
- DB2 - 別名
- DB2 - 約束
- DB2 - 索引
- DB2 - 觸發器
- DB2 - 序列
- DB2 - 檢視
- DB2 利用 XML
- DB2 - 備份和恢復
- DB2 - 資料庫安全性
- DB2 - 角色
- DB2 - LDAP
- DB2 有用資源
- DB2 - 疑問解答
- DB2 - 快速指南
- DB2 - 有用資源
- DB2 - 討論
DB2 - 利用 XML
本章節描述瞭如何將 XML 與 DB2 結合使用。
簡介
藉助 PureXML 功能,您可以將格式良好的 XML 文件儲存在資料庫表的列中。這些列具有 XML 資料庫。透過將 XML 資料儲存在 XML 列中,可以以其本機分層形式保留資料。DB2 資料庫伺服器功能可以訪問和管理儲存的 XML 資料。XML 資料以其本機分層形式儲存,可實現對 XML 的高效搜尋、檢索和更新。若要更新 XML 資料中的值,需要使用 XQuery、SQL 或二者的結合。
建立用於儲存 XML 資料的資料庫和表
釋出以下語法來建立資料庫
語法
db2 create database xmldb
預設情況下,資料庫使用 UTF-8(UNICODE)程式碼集。啟用資料庫並連線到資料庫
語法
db2 activate db <db_name> db2 connect to <db_name>
示例
db2 activate db xmldb db2 connect to xmldb
建立格式良好的 XML 檔案,並建立一個數據的列資料型別為“XML”的表。必須使用雙引號將包含 XML 語法的 SQL 查詢傳遞。
語法
db2 “create table <schema>.<table>(col <datatype>, col <xml datatype>)”
示例
db2 "create table shope.books(id bigint not null primary key, book XML)"
將 XML 值插入表中,使用 SQL 語句“INSERT”將格式良好的 XML 文件插入 XML 型別列。
語法
db2 “insert into <table_name> values(value1, value2)”
示例
db2 "insert into shope.books values(1000, '<catalog> <book> <author> Gambardella Matthew</author> <title>XML Developers Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating application with XML</description> </book> </catalog>')"
更新表中的 XML 資料
您可以使用以下語法更新表中的 XML 資料
語法
db2 “update <table_name> set <column>=<value> where <column>=<value>”
示例
db2 "update shope.books set book='<catalog> <book> <author> Gambardella, Matthew</author> <title>XML Developers Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth XML</description> </book> </catalog>' where id=1000"
廣告