Use SQL to query XML data in DB2 9

Source: Internet
Author: User
Tags table definition how to use sql xquery

The DB2 tutorial is: use SQL to query XML data in DB2 9.

Although the hybrid architecture of DB2 is quite different from that of earlier versions, it is not difficult to use its new XML features. If you are familiar with SQL, you can quickly convert this skill to processing local XML data stored in DB2. This article will show you how to achieve this.

XML features in DB2 viper (DB2 9) include new storage management, new indexing technology, and support for query languages. This article describes how to use SQL or SQL (SQL/XML) with XML extensions to query data in the DB2 XML column. NextArticleWe will discuss the newly introduced support for emerging industry-standard XQuery in DB2 and explore when it is most useful.

You may be surprised that DB2 also supports bilingual queries-queries that combine expressions from SQL and XQuery. Which language should be used (or the combination of the two languages) depends on the ApplicationProgramDepends on your skills. In fact, it is not as difficult as you think to combine the elements in the two query languages into a query. This can also provide powerful capabilities for searching and integrating traditional SQL and XML data.

Sample Database

The query in this article will access the sample database created in "DB2 Viper Quick Start" (developerworks, November April 2006. Here we will briefly review the definitions of the "items" and "clients" tables in the sample database:

Listing 1. Table Definition



Create Table items (

Id int primary key not null,

BrandName varchar (30 ),

Itemname varchar (30 ),

SKU int,

SRP decimal (7,2 ),

Comments XML

)

Create Table clients (

Id int primary key not null,

Name varchar (50 ),

Status varchar (10 ),

Contactinfo XML

)

Figure 1 shows the sample XML data in the "items. Comments" column, and Figure 2 shows the sample XML data in the "Clients. contactinfo" column. The following query example references an XML document or some specific elements in the two documents.

Figure 1. Example XML document stored in the "items" table "Comments" column

Figure 2. Example XML document stored in the "clients" table "contactinfo" column

Query Environment

All queries in this article are sent interactively. You can use the DB2 command line processor or the DB2 command editor in the DB2 control center to issue queries. The screen images and descriptions in this article are mainly based on the latter method. (DB2 Viper also comes with an eclipse-based developer workbench that helps programmers construct queries graphically. However, this article does not discuss application development issues or developer workbench .)

To use DB2 command editor, you need to start control center and select Tools> command editor. The window shown in Figure 3 is displayed. Enter query in the preceding panel, click the green arrow in the upper left corner to run the query, and view the output in the following panel or the query results tab.

Figure 3. DB2 command Editor, which can be started from DB2 Control Center

Pure SQL query

Even if you know little about SQL, you can easily query XML data. For example, the following query Selects all the content in the "clients" table, including the XML Information stored in the "contactinfo" column:

Listing 2. Simple SELECT statement

 


Select * from clients

Of course, you can also write more selective SQL queries to include relational projection and restricted operations. The following query retrieves the IDs, names, and contact information of all customers in the "gold" status. Note that the "contactinfo" column contains XML data, while the other two columns do not include XML data:

Listing 3. Simple select statements with projection and restrictions

 


Select ID, name, contactinfo

From clients

Where status = 'gold'

As you expected, you can create a view based on this query. The following "goldview" describes this:

Listing 4. Creating a view containing XML Columns



Create view goldview

Select ID, name, contactinfo

From clients

Where status = 'gold'

Unfortunately, many things cannot be solved simply by using SQL. You can use a pure SQL statement to retrieve the entire XML document (this has been proved just now), but you cannot specify an XML-based query predicate, you cannot retrieve a part of an XML document or a specific element value in an XML document. In other words, you cannot use SQL statements to project, restrict, join, aggregate, or sort fragments in an XML document. For example, you cannot retrieve the email address of a gold customer or the name of a customer living in a zip code "95116. To express these types of queries, you need to use SQL (SQL/XML) with XML extensions, XQuery, or both query languages.

The next section describes the basic features of SQL/XML. In the next article, we will learn how to write an XQuery and how to combine it with SQL.

SQL/XML query

As the name suggests, SQL/XML is designed to build a bridge between SQL and XML. It is first part of the SQL standard and has evolved to include the specification for embedding an XQuery or XPath expression into an SQL statement. XPath is a language used to navigate XML documents to discover elements or attributes. XQuery includes support for xpath.

Make sure that the XQuery (and XPath) expressions are case sensitive. For example, the XQuery that references the XML element "Zip" does not apply to XML elements named "Zip" or "Zip. Sometimes it is hard for SQL programmers to remember the case sensitivity, because the SQL query syntax allows the use of "Zip", "Zip", and "Zip" to reference the same column name.

DB2 Viper provides over 15 SQL/XML functions to search for specific data in the XML document, convert traditional data to XML, and convert XML data to relational data, and other useful tasks. This article does not discuss all aspects of SQL/XML, but just talks about several common query challenges.

[1] [2] [3] [4] [5] [6] Next

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.