Using SQL to query XML data in DB2 9 _DB2

Source: Internet
Author: User
Tags db2 xpath how to use sql xquery
The DB2 tutorial you're looking at is using SQL to query the XML data in DB2 9.

Although DB2 's hybrid architecture is quite different from previous versions, it is not difficult to take advantage of its new XML capabilities. If you are already familiar with SQL, you will soon be able to translate this skill into the processing of local XML data stored in DB2. You'll know how to do that with this article.

The XML features in DB2 Viper (that is, DB2 9) include new storage management, new indexing techniques, and support for query languages. In this article, learn how to use SQL or XML-extended SQL (sql/xml) to query DB2 XML columns for data. The next article will discuss the new support for the emerging industry standard XQuery in DB2, and explore when XQuery is most useful.

You might be surprised that DB2 also supports bilingual queries-that is, queries that combine expressions from SQL and XQuery. What language (or two languages) should be used depends on the needs of the application, but also on the skills you possess. In fact, combining elements from both query languages into one query is not as difficult as you might think. This can also provide powerful capabilities for searching and integrating traditional SQL and XML data.

Sample Database

The query in this article accesses the sample database that was created in the "DB2 Viper QuickStart" (developerworks,2006 year April). Here we briefly review the definition of "items" and "clients" tables in the sample database:

Listing 1. Definition of a table


CREATE TABLE items (











Comments XML

)

CREATE TABLE Clients (







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. Subsequent query examples will refer to one of the XML documents or to certain elements in the two documents.

Figure 1. Sample XML document stored in the Comments column of the items table



Figure 2. Sample XML document stored in the "Clients" table "contactinfo" column



Query environment

All the queries in this article are sent interactively, and you can issue queries through the DB2 command line processor or the DB2 command Editor in DB2 control Center. The screen images and instructions in this article are primarily based on the latter. (DB2 Viper also comes with an Eclipse based Developer Workbench that can help programmers graphically construct queries.) However, this article does not discuss application development issues or Developer Workbench. )

To use the DB2 Command Editor, you need to start control Center and select Tools > Command Editor. The window shown in Figure 3 will pop up. Enter the query in the upper panel, click the green arrow in the upper-left corner to run the query, and then view the output in the panel below or the Query Results tab.

Figure 3. DB2 Command Editor, which can be started from DB2 control Center



Pure SQL Query

Even if you have limited knowledge of SQL, you can still easily query XML data. For example, the following query selects all of the contents of the "clients" table, including the XML information stored in the "ContactInfo" column:

Listing 2. A simple SELECT statement



SELECT * FROM Clients



You can, of course, write more selective SQL queries that include relational projections and restriction operations. The following query retrieves the ID, name, and contact information for all customers with a "Gold" status. Note that the "ContactInfo" column contains XML data, while the other two columns do not contain XML data:

Listing 3. Simple SELECT statement with projections and restrictions





From clients

where status = ' Gold '



As you might expect, you can create a view based on such a query, as the following "Goldview" illustrates:

Listing 4. Create a view that contains XML columns







From clients

where status = ' Gold '



Unfortunately, a lot of things can't be solved by using SQL. You can retrieve an entire XML document through a pure SQL statement (as you have just shown), but you cannot specify an xml-based query predicate, nor can you retrieve a portion of an XML document or a specific element value in an XML document. In other words, using pure SQL does not project, restrict, connect, aggregate, or sort operations on fragments in an XML document. For example, you cannot retrieve the e-mail address of a Gold customer alone or the name of a customer residing in a region with a postal code of "95116". To express these types of queries, you need to use both query languages with XML-extended SQL (Sql/xml), XQuery, or combination.

The next section explores several basic features of Sql/xml. In the next article, we'll learn how to write XQuery and how to use XQuery in conjunction with SQL.

sql/xml Query

As the name suggests, Sql/xml is designed to build a bridge between SQL and XML. It is first and foremost a part of the SQL Standard and has evolved to include the specification of embedding XQuery or XPath expressions in SQL statements. XPath is a language used to navigate an XML document in order to discover elements or attributes. XQuery includes support for XPath.

It is important to note that XQuery (and XPath) expressions are case sensitive. For example, XQuery referencing XML element "Zip" does not apply to XML elements named "Zip" or "zip". It is sometimes difficult for SQL programmers to remember the case sensitivity, because the SQL query syntax allows the use of "zip", "zip", and "zip" to refer to the same column name.

DB2 Viper provides more than 15 sql/xml functions that enable you to search for specific data in an XML document, transform traditional data into XML, transform XML data into relational data, and perform other useful tasks. This article does not discuss all aspects of sql/xml, but only a few 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.