module 17 storing xml data in sql server ® 2008 r2
DESCRIPTION
Module 17 Storing XML Data in SQL Server ® 2008 R2. Module Overview. Introduction to XML and XML Schemas Storing XML Data and Schemas in SQL Server Implementing the XML Data Type. Lesson 1: Introduction to XML and XML Schemas. Discussion: XML Usage in SQL Server Core XML Concepts - PowerPoint PPT PresentationTRANSCRIPT
Module 17Storing XML Data in
SQL Server® 2008 R2
Module Overview• Introduction to XML and XML Schemas• Storing XML Data and Schemas in SQL Server• Implementing the XML Data Type
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
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
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
<?xml version="1.0" encoding="iso-8859-1" ?><?xml-stylesheet href="orders.xsl"?>
<order id="ord123456"> <customer id="cust0921"> <first-name>Dare</first-name> <last-name>Obasanjo</last-name> <address> <street>One Microsoft Way</street> <city>Redmond</city> <state>WA</state> <zip>98052</zip> </address> </customer></order>
Attribute
Element
Processing Instruction
Fragments vs. Documents
• Well-formed XML has only one top-level element
• Documents with multiple top-level elements are fragments
<order id="ord123456"> <customer id="cust0921" /></order><order id="ord123457"> <customer id="cust0925" /></order>
<?xml version="1.0" encoding="iso-8859-1" ?><orders> <order id="ord123456"> <customer id="cust0921" /> </order> <order id="ord123457"> <customer id="cust0925" /> </order></orders>
DOCUMENT
FRAGMENT
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"> <o:OrderDetail ProductID="761" Quantity="2"/> <o:OrderDetail ProductID="770" Quantity="1"/> </o:Order></Customer>
• XML namespace is a collection of names that can be usedas element or attribute names
• It is used to avoid conflicts with other names
• Prefix typically assigned to reference the namespace
XML Schemas
• XML schema describes the structure of XML document
• XML schema language is also called XML Schema Definition (XSD)
• Validation constraintsSQL Server validates the instance
• Data type informationSchemas provide info about types of attributes and elements
An XML schema provides the following:
Appropriate Usage of XML Data Storage in SQL Server
Reasons to store XML data in SQL ServerYou wish to share, query, and modify XML in an efficient and transacted wayYou have both relational and XML data and need to have interoperabilityYou need to build cross-domain applications and need portability of dataYour data is sparse or you do not know the structure of your dataOrder matters within your dataYou want the server to guarantee the XML is well-formed and optionally validate your data against a schemaYou want to index your XML data
Demonstration 1A: Introduction to XML and XML SchemasIn this demonstration, you will see how:• XML is structured• Documents differ from fragments• XML schemas are structured
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
XML Data Type
-- usage within table definitionCREATE TABLE App.Settings( SessionID int PRIMARY KEY, WindowSettings xml);-- usage as local variableDECLARE @Settings xml;-- usage as parameter to stored procedureCREATE 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
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
• Stores the imported XML schemas
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
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 (DOCUMENT App.SettingsSchemaCollection));
CONTENT(allows fragments)
DOCUMENT(no fragments)
Demonstration 2A: Typed vs. Untyped XMLIn 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
Lesson 3: Implementing XML Indexes• What are XML Indexes?• Types of XML Indexes• Demonstration 3A: Implementing XML Indexes
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
Types of XML Indexes
Primary XML IndexProvides a persisted object tree in an internal format that is used to speed access to elements and attributes within the XMLRequires a clustered primary key on the table
Secondary XML IndexCan only be constructed once a primary XML index has been createdThree types of secondary indexes can be constructed to help answer specific XQuery queries rapidly:PATH, PROPERTY, VALUE
Demonstration 3A: Implementing XML IndexesIn 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
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
Virtual machine 623XB-MIA-SQLUser name AdventureWorks\AdministratorPassword Pa$$w0rd
Lab ScenarioA 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.
Lab Review• What is the purpose of an XML schema?• When would you use untyped XML?• When would you use typed XML?
Module Review and Takeaways• Review Questions• Best Practices