Db2 can store XML as a first-class XML data type using pureXML—not merely stuffing tags into a CLOB. This overview covers what the type is, how internal storage differs from strings, PARSE/SERIALIZE, size limits, and where schema validation fits.
The XML data type defines columns that store XML values. This pureXML type provides the ability to store well-formed XML documents in the database. When you create a table with an XML column, Db2 implicitly creates an XML table space and an XML table to hold the XML data.
| Idea | Meaning |
|---|---|
| XML column | Typed column holding pureXML values |
| XMLPARSE | String document → XML value |
| XMLSERIALIZE | XML value → serialized string form |
| XSR | XML schema repository for validation |
12345CREATE TABLE ORDER_XML ( ORDER_ID INTEGER NOT NULL, ORDER_DOC XML NOT NULL, PRIMARY KEY (ORDER_ID) );
The business key remains relational (ORDER_ID). The document lives in the XML column with XML storage behind the scenes—similar in spirit to how LOBs use auxiliary storage, but specialized for XML.
All XML data is stored in an internal representation. Character data in that form uses UTF-8 in IBM’s description of the internal encoding. Critically: XML values are not a string type and are not directly comparable to ordinary string values.
To get a serialized document (text form of the XML), use XMLSERIALIZE or retrieve into an application variable of XML, string, or binary type. To store a document from text, use XMLPARSE or insert from a string, binary, or XML application type into the XML column.
12345678910111213INSERT INTO ORDER_XML (ORDER_ID, ORDER_DOC) VALUES ( 1001, XMLPARSE(DOCUMENT CAST( '' AS CLOB) ) ); SELECT ORDER_ID, XMLSERIALIZE(ORDER_DOC AS CLOB) AS ORDER_TEXT FROM ORDER_XML WHERE ORDER_ID = 1001; - WIDGET
Exact XMLPARSE options (DOCUMENT vs CONTENT, STRIP WHITESPACE, and so on) belong in deeper XML SQL lessons. Mentally: parse in, serialize out when crossing the string/XML boundary.
The size of an XML value in a Db2 table has no architectural limit in IBM’s wording. However, serialized XML stored in or retrieved from an XML column is limited to 2 GB. Plan application buffers and network transfers accordingly.
Validation against an XML schema—often during INSERT or UPDATE—is supported through the XML schema repository (XSR). If a column requires validated XML, documents must pass schema checks. Unvalidated storage is also possible depending on column definition; choose based on how strictly you trust producers.
Db2 supports XML indexes for efficient access to parts of documents. Like relational indexes, they help some queries and cost maintenance on write—design with EXPLAIN and workload in mind.
| Approach | When to consider |
|---|---|
| XML type | You need XML-aware storage, indexing, and querying |
| CLOB of XML text | You only archive document text without XML features |
| Shred to relational columns | You need relational constraints and joins on elements |
Many shops combine approaches: store the canonical document in XML (or CLOB) and maintain relational summary tables for joins and constraints. Pick based on access patterns, not hype.
Host languages need XML-capable variables or string buffers large enough for serialized documents. Fetching enormous documents into COBOL working storage is rarely wise—stream, locate, or extract nodes with XMLQUERY/XMLTABLE-style facilities in later lessons.
An XML document is a nested set of labeled boxes inside boxes. The XML type is a special shelf built for those boxes so the librarian can open them intelligently. A CLOB shelf just stores a long paper strip of writing. To put paper on the special shelf you parse it into boxes; to take it home as paper you serialize boxes back into writing.
1. The XML data type is used to:
2. How is XML stored internally compared to strings?
3. Serialized XML stored or retrieved from an XML column is limited to about:
4. XMLPARSE is used to:
5. When you create a table with an XML column, Db2 may implicitly create: