Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using XML in SQL Server 2005 NameTitleCompany. XML Overview Business Opportunity The majority of all data transmitted electronically between organizations.

Similar presentations


Presentation on theme: "Using XML in SQL Server 2005 NameTitleCompany. XML Overview Business Opportunity The majority of all data transmitted electronically between organizations."— Presentation transcript:

1 Using XML in SQL Server 2005 NameTitleCompany

2 XML Overview Business Opportunity The majority of all data transmitted electronically between organizations is now in XML. (source this besides ebXML) By storing XML natively, SQL Server 2005 helps eliminate the friction of data transfer between organizations. With the improved XML support in VS 2005, building e-business applications is radically simplified.

3 XML in the Database Overview XML has become widely adopted XML is ubiquitous, extensible, and a platform independent transport format Supports structured and semi-structured data Limited support in SQL Server 2000 First class support in SQL Server 2005

4 XML in the Database XML Data Type SQL Server 2005 adds native XML storage with XML data type Stores both well-formed and validated XML Implementation Markup documents Tree-structured data where re-assembly costs should be minimal Semi-structured data

5 XML in the Database XML Data Type An alternative to storing XML on disk Supports column, variable, or parameter Can represent Version 1.0 documents or fragments Methods on XML data type query(), value(), exist(), modify(), and nodes()

6 XML in the Database XML Data Type Table structure storing XML as CLOB CREATE TABLE [JobCandidate]( [JobCandidateID] [int] IDENTITY(1,1) NOT NULL, [JobCandidateID] [int] IDENTITY(1,1) NOT NULL, [EmployeeID] [int] NULL, [EmployeeID] [int] NULL, [Resume] [ntext] NULL, [Resume] [ntext] NULL, [ModifiedDate] [datetime] NOT NULL DEFAULT GetDate()) [ModifiedDate] [datetime] NOT NULL DEFAULT GetDate()) CLOB = Character Large Object TEXT, NTEXT, VARCHAR, NVARCHAR, etc.

7 XML in the Database XML Data Type Table structure storing XML as XML! CREATE TABLE [JobCandidate]( [JobCandidateID] [int] IDENTITY(1,1) NOT NULL, [JobCandidateID] [int] IDENTITY(1,1) NOT NULL, [EmployeeID] [int] NULL, [EmployeeID] [int] NULL, [Resume] [xml] NULL, [Resume] [xml] NULL, [ModifiedDate] [datetime] NOT NULL DEFAULT GetDate()) [ModifiedDate] [datetime] NOT NULL DEFAULT GetDate())

8 XML in the Database XML Data Type XML columns can have CHECK Constraints like a relational column CREATE TABLE [JobCandidate]( [JobCandidateID] [int] IDENTITY(1,1) NOT NULL, [JobCandidateID] [int] IDENTITY(1,1) NOT NULL, [EmployeeID] [int] NULL, [EmployeeID] [int] NULL, [Resume] [xml] NULL [Resume] [xml] NULL CHECK (Resume.exist('//school/college') = 1), CHECK (Resume.exist('//school/college') = 1), [ModifiedDate] [datetime] NOT NULL DEFAULT GetDate()) [ModifiedDate] [datetime] NOT NULL DEFAULT GetDate())

9 XML Data Type

10 XML in the Database XML Schema Support Validation and typing provided by XSD W3C recommendation Mechanism for type definitions Validation constraints Typed Data Guarantees the shape of the data Allows storage and query optimizations

11 XML in the Database XML Schema Support Associating a Schema CREATE XML SCHEMA COLLECTION ResumeSchema AS... CREATE TABLE [JobCandidate]( [JobCandidateID] [int] IDENTITY(1,1) NOT NULL, [JobCandidateID] [int] IDENTITY(1,1) NOT NULL, [EmployeeID] [int] NULL, [EmployeeID] [int] NULL, [Resume] [xml] ([ResumeSchema]) NULL, [Resume] [xml] ([ResumeSchema]) NULL, [ModifiedDate] [datetime] NOT NULL DEFAULT GetDate()) [ModifiedDate] [datetime] NOT NULL DEFAULT GetDate())

12 XML in the Database XML Schema Collections Creating an XML Schema Collection CREATE XML SCHEMA COLLECTION ResumeSchema AS '<xs:schema targetNameSpace='http://schemas.ms.com/emp" …/>' targetNameSpace='http://schemas.ms.com/emp" …/>' Constraining XML documents CREATE TABLE Employee (..., Resume XML(ResumeSchema)) Retrieving XML Schema Collections SELECT Name FROM AdventureWorks.sys.xml_schema_collections

13 Associating a Schema

14 XML in the Database XML Indexes Create XML index on XML column CREATE PRIMARY XML INDEX IDX1 ON JobCandidate (Resume) Creates a secondary, B-tree index on Path, Property, or Value Speeds up queries Results can be served directly from index Uses SQL Server's cost based optimizer Full-Text Indexing/Searching supports XML

15 XML in the Database XML Indexes PATH Index When querying by path and value /ItemList/Item[@ProductID="1"] PROPERTY Index When retrieving node values by path (/ItemList/Item@ProductID)[1] VALUE Index When querying imprecisely, like for existence //Item[@ProductID="1"]

16 XML in the Database XML Indexes Requirements A clustered PK must exist in the table If XML index exists, the PK may not be altered Only one primary XML index per column Many secondary XML indexes per column PATH, PROPERTY, and VALUE All index names must be unique per table

17 XML in the Database XQuery Query XML documents and data Based on W3C working draft FLWOR - For, Let, Where, Order by, Return XQuery methods.query() – returns XML data type instance.value() – returns a single (scalar) value.exist() – returns 1 if expression returns item(s).nodes() – returns a row per selected node

18 XML in the Database XQuery XQuery.query() example // Search for an ID of 123 and return section // heading of section 3 and later // heading of section 3 and later SELECT ID, xmlColumn.query('for $s in /doc[@id = 123]//sec[@num >= 3] /doc[@id = 123]//sec[@num >= 3] return {data($s/heading)} ') FROM xmlTable return {data($s/heading)} ') FROM xmlTable

19 XML in the Database XQuery XQuery.value() example // Extracts an XQuery value into the SQL // value and type space // value and type space SELECT xmlColumn.value( '(/doc[@id = 123]//sec[@num >= 3]/@num)[1]', '(/doc[@id = 123]//sec[@num >= 3]/@num)[1]', 'int') 'int') FROM xmlTable FROM xmlTable

20 XML in the Database XQuery XQuery.exist() example // Return a 1 if the XQuery expression returns // at least one item, 0 otherwise // at least one item, 0 otherwise SELECT * FROM xmlTable WHERE xmlColumn.exist('/a/b[@id=42]') = 1 WHERE xmlColumn.exist('/a/b[@id=42]') = 1

21 XQuery

22 XML in the Database XML Data Manipulation Language Inefficient to UPDATE the entire column XML sub-tree modification using.modify() Three actions: Insert, Delete, or Replace UPDATE xmlTable SET xmlColumn.modify('insert Background Background after (/doc/section[@num=1])[1]')

23 XML/DML

24 XML in the Database FOR XML Improvements Support for XSD schemas Support for NULL values Support for XML data type Nested FOR XML Can be assigned to an XML data type FOR XML PATH

25 FOR XML Improvements

26 XML in the Database Summary XML Data Type XML Schema Support XML Indexes XQuery XML Data Manipulation Language FOR XML Improvements

27 Resources On the Web World Wide Web Consortium http://www.w3.org W3 Schools http://www.w3schools.com

28 © 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.


Download ppt "Using XML in SQL Server 2005 NameTitleCompany. XML Overview Business Opportunity The majority of all data transmitted electronically between organizations."

Similar presentations


Ads by Google