Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Data in MS SQL Server Query and Modification

Similar presentations


Presentation on theme: "XML Data in MS SQL Server Query and Modification"— Presentation transcript:

1 XML Data in MS SQL Server Query and Modification
Steven Blundy, Duc Duong, Abhishek Mukherji, Bartlett Shappee CS561

2 Outline Introduction XML Data Type Publishing Structure and storage
Schema, validation Methods Publishing FORXML Queries

3 Approaches for XML integration to DB
Mid-tier Bi-directional XML view Query view using XPath Schema-driven approach for shredding Server-side rowset-to-XML aggregator for XML Publishing - FOR XML Query-driven shredding mechanism- Open XML This talk is an insight into how SQL Server 2005 fits XML functionality into its core relational database management framework. Along with publishing and shredding, support is required for storing XML in native form into the DB and users wish to query into XML structure itself rather than shredding into relational data. To address this need, SQL Server 2005 adds native XML management capabilities along the functionality outlined in the introduction of this tutorial.

4 SQL SERVER 2005 XML Architecture

5 XML Storage - Native Checks Well Formedness XML Documents or Fragments
Validation is optional XML Documents or Fragments

6 XML Storage - DB SQL BLOB Allows for utilization BLOB optimizations
Streaming Parsing Compression Unicode (UTF-16) Strings - UNTYPED Requires Conversion XML Schema - TYPED Encode to match schema Much more efficient Storing the XML data in a BLOB provides the many benfits associated with binary data This includes the optimaztions that coming with streaming the data to and from sources, parsing over the data, and algorithms to compress the data to save space. With in the Binary representation, all un-typed XML data is stored in Unicode Strings, essentially similar to the original xml text data. When an XML schema is associated with the data, we can additional benefits the data is stored in the most efficient way, for reducing size the data can also be optimized for parsing and no longer needs to be pre processed

7 XML Storage - The Numbers
Advantages of Binary Storage 20 to 30% Size Reduction Faster Limitations 2gb of stored binary per instance Hierarchy is limited to 128 Levels These are the simple facts about the basic XML storage: Advantages of Binary Storage 20 to 30% Size Reduction Faster Limitations 2gb of stored binary per instance Hierarchy is limited to 128 Levels

8 Storage - Schema Storage Optimization Uses the XML Infoset
Size Processing Uses the XML Infoset Defined in an XMLSchemaCollection As mentioned before the Schema promotes storage optimization It also adheres to the XML Infoset and is stored in XMLSchemaCollectinos as we shall see

9 Validation - Schema XML Schema Collection Uses the XML Infoset
Stores 1+ XML Schemas Identified by Name Space Not the Same as Constraints (No Business Logic) Metadata Entity Certain Type are Format Constrained i.e. Date must use ISO 8601 format Uses the XML Infoset Multiple Schemas can be stored in an XML Schema Collection being Identified by Name Space. These schemas cannot be used the same way as constraingts on the XML data as they do not provide business logic These collections are a Meta Data entity in that the only store column infromation. Also it is important to not that in SQL Server 2005 certain ambiguous Types such as Date and Time must adhere to specific formats In the case of Date and Time this is the ISO 8601 format The Xml Schema Collection also adheres to and use the XML Infoset

10 Validation - Schema Collection
CREATE XML SCHEMA COLLECTION myCollection AS '<xsd:schema xmlns:xsd=" xmlns=" elementFormDefault="qualified" targetNamespace=" <xsd:element name="bookstore" type="bookstoreType" /> <xsd:complexType name="bookstoreType"> <xsd:sequence maxOccurs="unbounded"> <xsd:element name="book" type="bookType" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="authorName"> <xsd:sequence> <xsd:element name="first-name" type="xsd:string" /> <xsd:element name="last-name" type="xsd:string" /> </xsd:schema>' This just an example, see that it is still entered as one string, and then parsed and handeled by the system

11 Validation - How? At Data Entry Checking Data Updated Schema
Validness checked when typed data is inserted Checking Data Applied using Cast Updated Schema Does not Require re-validation Validation is optional feature which can be done at two points. When a schema is created it can be used to validate current data and also any new data entery. On the modification of a schema, it can be applied to new incoming data and does not re-validate exsisting data.

12 XML DT Methods All evaluate an XQuery Methods
query() - returns XML output exists() - checks if expression results in nodes nodes() - returns XML DT values value() - returns value as SQL DT modify() - modify XML data

13 query() & nodes() methods
Both take XQuery string query() returns list of untyped XML nodes Can be converted to strings For SELECT output nodes() returns list of typed XML nodes All XML DT methods available count(*) works No converting to strings

14 exist() & value() methods
returns true if XQuery returns any nodes value() Takes 2 params: an XQuery & a SQL DT Converts xml value returned by XQuery to specified SQL type

15 Example: value() Title Design Patterns
SELECT data.value(‘(/bibliograph/book/[1]/title)[1]’, ‘NVARCHAR(255)’) AS Title FROM Test Title Design Patterns

16 Example: value() w/ nodes()
SELECT book.value(‘(title)[1]’, ‘NVARCHAR(255)’) AS Title FROM Test CROSS APPLY data.nodes(‘/bibliograph/book’) AS R(book) Title Design Patterns All about XML

17 Example: exist() Title Design Patterns All about XML …
SELECT book.value(‘(title)[1]’, ‘NVARCHAR(255)’) AS Title FROM Test CROSS APPLY data.nodes(‘/bibliograph/book’) AS R(book) WHERE data.exist(‘/bibliograph/book’) = 1 Title Design Patterns All about XML

18 modify() method Uses extended XQuery Used in SQL UPDATEs
insert, delete, and replace keywords Used in SQL UPDATEs

19 Example: modify() UPDATE docs SET xCol.modify(‘ insert
<section num="2"> <title>Background</title> </section> after This example shows the use of the modify() method to insert a new <section> element to the right of the <section> element whose number is 1.

20 Example 2: modify() UPDATE XmlCatalog SET Document.modify ('
declare namespace bk = " replace value of (/bk:bookstore/bk:book with 49.99') The following UPDATE statement replaces the <price> of a book whose ISBN is to $ The XML instance is typed with the XML schema hence the namespace declaration in the XML data modification statement.

21 XML Publishing select CustomerID as "@CustomerID",
City as "address/city", PostalCode as "address/zip", ContactName as "contact/name", Phone as "contact/phone", from Customers for xml path('Customer'), root('Doc')

22 Additional Papers Used
XML Support in Microsoft SQL Server 2005 Shankar Pal, Mark Fussell, and Irwin Dolobowsk XML Best Practices for Microsoft SQL Server 2005 Shankar Pal, Vishesh Parikh, Vasili Zolotov, Leo Giakoumakis, Michael Rys

23 Questions


Download ppt "XML Data in MS SQL Server Query and Modification"

Similar presentations


Ads by Google