Download presentation
Presentation is loading. Please wait.
Published byEmmeline Norris Modified over 9 years ago
1
Module 17 Storing XML Data in SQL Server® 2008 R2
2
Module Overview Introduction to XML and XML Schemas Storing XML Data and Schemas in SQL Server Implementing the XML Data Type
3
Lesson 1: Introduction to XML and XML Schemas Discussion: XML Usage in SQL Server Core XML Concepts Fragments vs. Documents XML Namespaces XML Schemas Appropriate Usage of XML Data Storage in SQL Server Demonstration 1A: Introduction to XML and XML Schemas
4
Discussion: XML Usage in SQL Server Traditionally, database administrators have not worked with XML Database developers often want to store XML data in SQL Server Many aspects and components of SQL Server are now XML-based, including those often only used by administrators XML data type and schema collections DDL triggers Extended events Logon triggers Event notifications Service Broker
5
Core XML Concepts Is a plain-text, Unicode-based meta-language Represents both structured and semi-structured data Is not tied to any programming language, OS, or vendor Dare Obasanjo One Microsoft Way Redmond WA 98052 Dare Obasanjo One Microsoft Way Redmond WA 98052 Attribute Element Processing Instruction
6
Fragments vs. Documents Well-formed XML has only one top-level element Documents with multiple top-level elements are fragments DOCUMENT FRAGMENT
7
XML Namespaces <Customer xmlns="urn:AW_NS" xmlns:o="urn:AW_OrderNS" CustomerID="1" CustomerType="S"> <o:Order SalesOrderID="43860" Status="5" OrderDate="2001-08-01T00:00:00"> <Customer xmlns="urn:AW_NS" xmlns:o="urn:AW_OrderNS" CustomerID="1" CustomerType="S"> <o:Order SalesOrderID="43860" Status="5" OrderDate="2001-08-01T00:00:00"> XML namespace is a collection of names that can be used as element or attribute names It is used to avoid conflicts with other names Prefix typically assigned to reference the namespace
8
XML Schemas XML schema describes the structure of XML document XML schema language is also called XML Schema Definition (XSD) Validation constraints SQL Server validates the instance Data type information Schemas provide info about types of attributes and elements An XML schema provides the following:
9
Appropriate Usage of XML Data Storage in SQL Server Reasons to store XML data in SQL Server You wish to share, query, and modify XML in an efficient and transacted way You have both relational and XML data and need to have interoperability You need to build cross-domain applications and need portability of data Your data is sparse or you do not know the structure of your data Order matters within your data You want the server to guarantee the XML is well-formed and optionally validate your data against a schema You want to index your XML data
10
Demonstration 1A: Introduction to XML and XML Schemas In this demonstration, you will see how: XML is structured Documents differ from fragments XML schemas are structured
11
Lesson 2: Storing XML Data and Schemas in SQL Server XML Data Type XML Schema Collections Untyped vs. Typed XML CONTENT vs. DOCUMENT Demonstration 2A: Typed vs. Untyped XML
12
XML Data Type -- usage within table definition CREATE TABLE App.Settings ( SessionID int PRIMARY KEY, WindowSettings xml ); -- usage as local variable DECLARE @Settings xml; -- usage as parameter to stored procedure CREATE PROCEDURE App.SaveSettings (@SettingsToSave xml) AS... -- usage within table definition CREATE TABLE App.Settings ( SessionID int PRIMARY KEY, WindowSettings xml ); -- usage as local variable DECLARE @Settings xml; -- usage as parameter to stored procedure CREATE PROCEDURE App.SaveSettings (@SettingsToSave xml) AS... Is a native data type for XML Lets you store XML documents and fragments Is used for columns, variables, or parameters Exposes methods to query and modify XML
13
XML Schema Collections The XML schema collection: Validates XML instances Types the XML data as it is stored in the database SQL Server provides native storage of XML data with the xml data type You can optionally associate XSD schemas with an xml data type through an XML schema collection SQL Server provides native storage of XML data with the xml data type You can optionally associate XSD schemas with an xml data type through an XML schema collection Stores the imported XML schemas
14
Untyped vs. Typed XML Use untyped XML data type in the following situations: You do not have a schema for your XML data You have schemas, but don’t want server to validate data (there is significant impact on a server performing validation) Use typed XML data type in the following situations: You have schemas and want server to validate XML data You want to take advantage of storage and query optimizations based on type information You want to take advantage of type information during compilation of your queries
15
CONTENT vs. DOCUMENT XML data type stores CONTENT (including fragments) by default Can be required to store DOCUMENT CREATE TABLE App.Settings ( SessionID int PRIMARY KEY, WindowSettings xml (CONTENT App.SettingsSchemaCollection) ); CREATE TABLE App.Settings ( SessionID int PRIMARY KEY, WindowSettings xml (CONTENT App.SettingsSchemaCollection) ); CREATE TABLE App.Settings ( SessionID int PRIMARY KEY, WindowSettings xml (DOCUMENT App.SettingsSchemaCollection) ); CREATE TABLE App.Settings ( SessionID int PRIMARY KEY, WindowSettings xml (DOCUMENT App.SettingsSchemaCollection) ); CONTENT (allows fragments) DOCUMENT (no fragments)
16
Demonstration 2A: Typed vs. Untyped XML In this demonstration, you will see how to: Create an XML SCHEMA COLLECTION Use typed XML in a column of a table Differentiate between CONTENT and DOCUMENT
17
Lesson 3: Implementing XML Indexes What are XML Indexes? Types of XML Indexes Demonstration 3A: Implementing XML Indexes
18
What are XML Indexes? XML data can be slow to access XML indexes can help with query performance Indexes contain details of Nodes Values Paths
19
Types of XML Indexes Primary XML Index Provides a persisted object tree in an internal format that is used to speed access to elements and attributes within the XML Requires a clustered primary key on the table Secondary XML Index Can only be constructed once a primary XML index has been created Three types of secondary indexes can be constructed to help answer specific XQuery queries rapidly: PATH, PROPERTY, VALUE
20
Demonstration 3A: Implementing XML Indexes In this demonstration, you will see how to: Create a primary XML index Create a secondary XML index Query the sys.xml_indexes system view Identify the requirement of a clustered primary key on the table by XML indexes
21
Lab 17: Storing XML Data in SQL Server Exercise 1: Appropriate Usage of XML Data Storage in SQL Server Exercise 2: Investigate the Storage of XML Data in Variables Exercise 3: Investigate the use of XML Schema Collections Challenge Exercise 4: Investigate the Creation of Database Columns Based on XML (Only if time permits) Logon information Estimated time: 45 minutes
22
Lab Scenario A new developer in your organization has discovered that SQL Server can store XML directly. He is keen to use this mechanism extensively. In this lab, you will decide on appropriate usage of XML within the documented application. You also have an upcoming project that will require the use of XML data within SQL Server. No members of your current team have experience working with XML data in SQL Server. You need to learn how to process XML data within SQL Server and you have been provided with some sample queries to assist with this learning.
23
Lab Review What is the purpose of an XML schema? When would you use untyped XML? When would you use typed XML?
24
Module Review and Takeaways Review Questions Best Practices
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.