xml data in ms sql server query and modification steven blundy, duc duong, abhishek mukherji,...

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

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

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

XML Data in MS SQL Server Query and Modification

Steven Blundy, Duc Duong, Abhishek Mukherji, Bartlett Shappee

CS561

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

Outline

Introduction XML Data Type

Structure and storageSchema, validationMethods

PublishingFORXML Queries

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

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

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

SQL SERVER 2005 XML Architecture

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

XML Storage - Native

Checks Well FormednessValidation is optional

XML Documents or Fragments

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

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

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

XML Storage - The Numbers

Advantages of Binary Storage20 to 30% Size ReductionFaster

Limitations2gb of stored binary per instanceHierarchy is limited to 128 Levels

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

Storage - Schema

Storage OptimizationSizeProcessing

Uses the XML Infoset Defined in an XMLSchemaCollection

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

Validation - Schema

XML Schema CollectionStores 1+ XML Schemas

Identified by Name Space Not the Same as Constraints (No Business Logic)

Metadata EntityCertain Type are Format Constrained

i.e. Date must use ISO 8601 format

Uses the XML Infoset

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

Validation - Schema Collection

CREATE XML SCHEMA COLLECTION myCollection AS '<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://myBooks" elementFormDefault="qualified" targetNamespace="http://myBooks"> <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:sequence> </xsd:complexType></xsd:schema>'

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

Validation - How?

At Data EntryValidness checked when typed data is

inserted Checking Data

Applied using Cast Updated Schema

Does not Require re-validation

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

XML DT Methods

All evaluate an XQuery Methods

query() - returns XML outputexists() - checks if expression results in nodesnodes() - returns XML DT valuesvalue() - returns value as SQL DTmodify() - modify XML data

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

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

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

exist() & value() methods

exist() returns true if XQuery returns any nodes

value() Takes 2 params: an XQuery & a SQL DTConverts xml value returned by XQuery to

specified SQL type

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

Example: value()

SELECT data.value(‘(/bibliograph/book/[1]/title)[1]’,‘NVARCHAR(255)’) AS Title

FROM Test

Title

Design Patterns

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

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

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

Example: exist()

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

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

modify() method

Uses extended XQuery insert, delete, and replace keywords

Used in SQL UPDATEs

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

Example: modify()

UPDATE docs SET xCol.modify(‘

insert

<section num="2">

<title>Background</title>

</section>

after (/doc//section[@num=1])[1]')

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

Example 2: modify()

UPDATE XmlCatalog

SET Document.modify ('

declare namespace bk = "http://myBooks";

replace value of (/bk:bookstore/bk:book [@ISBN="1-861003-11-0"]/bk:price)[1] with 49.99')

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

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')

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

Additional Papers Used

XML Support in Microsoft SQL Server 2005 Shankar Pal, Mark Fussell, and Irwin Dolobowsk http://msdn2.microsoft.com/en-us/library/ms345117.aspx

XML Best Practices for Microsoft SQL Server 2005 Shankar Pal, Vishesh Parikh, Vasili Zolotov, Leo Giakoumakis,

Michael Rys http://msdn2.microsoft.com/en-us/library/ms345115(d=printer).a

spx

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

Questions