Db2 简明教程
DB2 - with XML
本章介绍在 DB2 中使用 XML。
This chapter describes use of XML with DB2.
Introduction
有了 PureXML 功能,你可以在数据库表的列中存储格式良好的 XML 文档。这些列具有 XML 数据库。将 XML 数据存储在 XML 列中,可以使其以本机分层形式保持数据。DB2 数据库服务器功能可以访问和管理已存储的 XML 数据。将 XML 数据存储在其本机分层形式中能够高效地搜索、检索和更新 XML。若要更新 XML 数据中的值,你需要使用 XQuery、SQL 或两者的组合。
PureXML feature allows you to store well-formed XML documents in columns of database tables. Those columns have XML database. Data is kept in its native hierarchical form by storing XML data in XML column. The stored XML data can be accessed and managed by DB2 database server functionality. The storage of XML data in its native hierarchical form enables efficient search, retrieval, and update of XML. To update a value in XML data, you need to use XQuery, SQL or combination of both.
Creating a database and table for storing XML data
通过发布以下语法创建一个数据库:
Create a database by issuing the following syntax:
Syntax:
Syntax:
db2 create database xmldb
默认情况下,数据库使用 UTF-8 (UNICODE) 编码集。激活数据库并连接到它:
By default, databases use UTF-8 (UNICODE) code set. Activate the database and connect to it:
Syntax:
Syntax:
db2 activate db <db_name>
db2 connect to <db_name>
Example:
Example:
db2 activate db xmldb
db2 connect to xmldb
创建一个格式良好的 XML 文件,并创建一个数据类型为“XML”的表。必须将包含在双引号中的 SQL 查询传递到 XML 语法。
Create a well-formed XML file and create a table with data type of the column as ‘XML’. It is mandatory to pass the SQL query containing XML syntax within double quotation marks.
Syntax:
Syntax:
db2 “create table <schema>.<table>(col <datatype>,
col <xml datatype>)”
Example:
Example:
db2 "create table shope.books(id bigint not null
primary key, book XML)"
使用 SQL 语句“INSERT”将 XML 值插入到表中,格式良好的 XML 文档被插入到 XML 类型列中。
Insert xml values into table, well-formed XML documents are inserted into XML type column using SQL statement ‘INSERT’.
Syntax:
Syntax:
db2 “insert into <table_name> values(value1, value2)”
Example:
Example:
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>')"
Updating XML data in a table
您可以使用以下语法更新表中的 XML 数据:
You can update XML data in a table by using the following syntax:
Syntax:
Syntax:
db2 “update <table_name> set <column>=<value> where
<column>=<value>”
Example:
Example:
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"