Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Server 2005: Extending the Type System with XML.

Similar presentations


Presentation on theme: "SQL Server 2005: Extending the Type System with XML."— Presentation transcript:

1 SQL Server 2005: Extending the Type System with XML

2 Who I Am Shawn Wildermuth (swildermuth@adoguy.com) C# MVP INETA Speaker Author of “Pragmatic ADO.NET”; Editor of http://ONDotnet.com This Presentation can be found at: – http://adoguy.com/presentations Shawn Wildermuth (swildermuth@adoguy.com) C# MVP INETA Speaker Author of “Pragmatic ADO.NET”; Editor of http://ONDotnet.com This Presentation can be found at: – http://adoguy.com/presentations

3 Agenda The XML Datatype Using the XML Type Methods What is the Type System Can’t I use CLR Types? Typed XML Extending the Typed System with XML XML Indexes The XML Datatype Using the XML Type Methods What is the Type System Can’t I use CLR Types? Typed XML Extending the Typed System with XML XML Indexes

4 The XML DataType New to SQL Server 2005 – Native support for XML data – Not stored as text blobs – XML treated as first-class type – Has some limitations – Support in-place editing New to SQL Server 2005 – Native support for XML data – Not stored as text blobs – XML treated as first-class type – Has some limitations – Support in-place editing

5 The XML DataType (2) Using the XML datatype – Can be used in procedural code: – Can be gathered from Old Style XML Syntax: – Can be used in Table/View Declarations: Using the XML datatype – Can be used in procedural code: – Can be gathered from Old Style XML Syntax: – Can be used in Table/View Declarations: DECLARE @doc xml SELECT @doc = ' ' SELECT @doc = (SELECT * FROM Person.Contact FOR XML AUTO) CREATE TABLE Team ( TeamID int identity not null, TeamDoc xml DEFAULT ' ' NOT NULL )

6 The XML DataType (3) Using the XML datatype: – Inserting/updating XML with Strings: Using the XML datatype: – Inserting/updating XML with Strings: -- Insert a couple of records INSERT INTO Team (TeamDoc) VALUES (' ');

7 The XML DataType (4) Limitations – No conversion to and from text/ntext – Only strings can cast to XML type – Cannot be used in GROUP BY’s – Cannot be in Distributed or Materialized Views – Cannot used primary or foreign keys – Cannot be uniquely constrained – Only 32 XML Columns per Table – Only 128 Levels of Hierarchy supported Limitations – No conversion to and from text/ntext – Only strings can cast to XML type – Cannot be used in GROUP BY’s – Cannot be in Distributed or Materialized Views – Cannot used primary or foreign keys – Cannot be uniquely constrained – Only 32 XML Columns per Table – Only 128 Levels of Hierarchy supported

8 The XML DataType The XML Type has five methods: – Query: Used to find subresults – Exist: Used to find matches The XML Type has five methods: – Query: Used to find subresults – Exist: Used to find matches SELECT TeamDoc.query('/Team/Players/Pitcher') FROM Team SELECT Count(*) FROM Team WHERE TeamDoc.exist('/Team/Players/Pitcher[@role="Starter"]') = 1

9 The XML DataType Methods (2) – Value: Used to find a value of a result – Nodes: Used to return nodes collections  (We won’t be covering this in the interest of the schedule) – Value: Used to find a value of a result – Nodes: Used to return nodes collections  (We won’t be covering this in the interest of the schedule) SELECT TeamDoc.value('(/Team/Players/Pitcher/@name)[1]', 'nvarchar(max)') as FirstPitcher FROM Team FirstPitcher ----------------- John Smoltz

10 The XML DataType Methods (3) – Modify: Used to do in-place changes  MS Specific XQuery Extensions for Modification –insert –delete – Modify: Used to do in-place changes  MS Specific XQuery Extensions for Modification –insert –delete UPDATE Team SET TeamDoc.modify(' insert as last into (/Team/Players)[1] ') WHERE TeamDoc.exist('/Team[@name="Braves"]') = 1 UPDATE Team SET TeamDoc.modify(' delete /Team/Players[@name="Jaret Wright"] ') WHERE TeamDoc.exist('/Team[@name="Braves"]') = 1

11 The XML DataType Methods (4) – Modify: Used to do in-place changes  replace with (like update)  Can use full XQuery syntax as needed – Modify: Used to do in-place changes  replace with (like update)  Can use full XQuery syntax as needed UPDATE Team SET TeamDoc.modify (' replace value of (/Team/Players/Pitcher[@name="John Smoltz"]/@role)[1] with "Starter" ') UPDATE Team SET TeamDoc.modify (' replace value of (/Team/Players/Pitcher[@name="John Smoltz"]/@role)[1] with ( if (/Team/Players/Pitcher[@name="John Smoltz"]/@role = "Closer") then "Starter" else "Closer") ')

12 SQL Server Type System Type system has been static – varchar, int, bit, datetime, etc. – Could only create types by reduction Type system has been static – varchar, int, bit, datetime, etc. – Could only create types by reduction EXEC sp_addtype N'age', N'tinyint', N'not null' GO CREATE RULE age_range AS @age >= 0 AND @age <=140 GO EXEC sp_bindrule N'age_range', N'age' GO

13 SQL Server Type System (2) SQL Server 2005 Supports two extensions – Managed Types  Limited to 8000 bytes in size  Good for including behavior  Must deal with security issues of managed code  No Good Inter Object Searching Story  No Good Indexing Story SQL Server 2005 Supports two extensions – Managed Types  Limited to 8000 bytes in size  Good for including behavior  Must deal with security issues of managed code  No Good Inter Object Searching Story  No Good Indexing Story

14 SQK Server Type System (3) SQL Server 2005 Supports two extensions – Typed XML  Size not an issue  No behavior can be added  Just data limits security issues  XQuery/XPath for sub-searching  Supports XML Indexing SQL Server 2005 Supports two extensions – Typed XML  Size not an issue  No behavior can be added  Just data limits security issues  XQuery/XPath for sub-searching  Supports XML Indexing

15 Using Typed XML Uses Schema Collections for different Types – Uses new DDL syntax to add XML Type Uses Schema Collections for different Types – Uses new DDL syntax to add XML Type CREATE XML SCHEMA COLLECTION BaseballSchema AS ' '

16 Using Typed XML (2) Once registered, can use as XML type: DECLARE @team xml(BaseballSchema) SET @team = ' ' SELECT @team (1 row(s) affected)

17 Using Typed XML (3) Validation Happens at Assignment DECLARE @team xml(BaseballSchema) -- Won’t work as role isn’t defined SET @team = ' ' XML Validation: Attribute 'role' is not permitted in this context. Location: /*:Team[1]/*:Pitcher[1]/@*:role

18 Using Typed XML (4) Works just as well as columns in Tables CREATE TABLE Team ( TeamID int identity not null, TeamDoc xml(BaseballSchema) ) INSERT INTO Team (TeamDoc) VALUES (' ')

19 Using Typed XML (5) Searching can then include XML searches – Just like non-typed XML, but faster Searching can then include XML searches – Just like non-typed XML, but faster SELECT TeamDoc.query('/Team/Pitcher[@name="John Smoltz"]') FROM Team

20 Managing Typed XML Schemas Schema Collections – Can contain more than one XSD – Include new schema with ADD: Schema Collections – Can contain more than one XSD – Include new schema with ADD: ALTER XML SCHEMA COLLECTION BaseballSchema ADD ' '

21 Managing Typed XML Schemas (2) Both XSD’s can now be used INSERT INTO Team (TeamDoc) VALUES (' ') INSERT INTO Team (TeamDoc) VALUES ('<Score HomeTeam="Braves" AwayTeam="RedSox" HomeScore="5" AwayScore="4" />')

22 Managing Typed XML Schemas (3) Cannot drop schemas if used – ALTER does not let you drop an XSD – Cannot drop entire collection until not in use Cannot drop schemas if used – ALTER does not let you drop an XSD – Cannot drop entire collection until not in use ALTER TABLE Team DROP COLUMN TeamDoc GO DROP XML SCHEMA COLLECTION BaseballSchema GO CREATE XML SCHEMA COLLECTION BaseballSchema AS '...' GO ALTER TABLE Team ADD TeamDoc xml (BaseballSchema)

23 Managing Typed XML Schemas (4) Caveats – May need to workout versioning  Can do this with Convert(xml(sometype), oldtype)  Only works if the new schema is superset  May need to do xslt to do real conversions  Or; support both versions Caveats – May need to workout versioning  Can do this with Convert(xml(sometype), oldtype)  Only works if the new schema is superset  May need to do xslt to do real conversions  Or; support both versions

24 XML Indexing XML Columns can have Indexes – Can have Primary and secondary indexes – Primary improves simple node searches XML Columns can have Indexes – Can have Primary and secondary indexes – Primary improves simple node searches CREATE PRIMARY XML INDEX IXML_Teams ON Team (TeamDoc) SELECT * FROM TEAM WHERE TeamDoc.exist("\Team\Pitcher")

25 XML Indexing (2) Secondary Indexes – Add for specific types of searches  PATH –On the path and value columns of the primary index to make path-specific exist() method calls more efficient  PROPERTY –Builds an index on the PK, path and value columns of the primary index to make value() method calls more efficient Secondary Indexes – Add for specific types of searches  PATH –On the path and value columns of the primary index to make path-specific exist() method calls more efficient  PROPERTY –Builds an index on the PK, path and value columns of the primary index to make value() method calls more efficient TeamDoc.exist('/ /Pitcher[@name = "John Smoltz"]') TeamDoc.value('(/Team/@name)[1]’)

26 XML Indexing (3) Secondary Indexes – Add for specific types of searches  VALUE –Builds an index on the value and path of the primary index to make node based exist() method calls more efficient Secondary Indexes – Add for specific types of searches  VALUE –Builds an index on the value and path of the primary index to make node based exist() method calls more efficient TeamDoc.exist('//Pitcher[@role="Closer"]')

27 XML Indexing (4) Creating Secondary Indexes – Use CREATE XML INDEX syntax  Add USING to specify primary index Creating Secondary Indexes – Use CREATE XML INDEX syntax  Add USING to specify primary index CREATE XML INDEX IXML_Team_Path ON Team (TeamDoc) USING XML INDEX IXML_Teams FOR PATH CREATE XML INDEX IXML_Team_Prop ON Team (TeamDoc) USING XML INDEX IXML_Teams FOR PROPERTY CREATE XML INDEX IXML_Team_Value ON Team (TeamDoc) USING XML INDEX IXML_Teams FOR VALUE

28 Questions?


Download ppt "SQL Server 2005: Extending the Type System with XML."

Similar presentations


Ads by Google