1 / 24

Module 17 Storing XML Data in SQL Server ® 2008 R2

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

lindley
Télécharger la présentation

Module 17 Storing XML Data in SQL Server ® 2008 R2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 typeand 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 <?xmlversion="1.0"encoding="iso-8859-1" ?> <?xml-stylesheethref="orders.xsl"?> <orderid="ord123456"> <customerid="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 Processing Instruction Element

  6. Fragments vs. Documents DOCUMENT FRAGMENT • Well-formed XML has only one top-level element • Documents with multiple top-level elements are fragments <?xmlversion="1.0"encoding="iso-8859-1" ?> <orders> <orderid="ord123456"> <customerid="cust0921" /> </order> <orderid="ord123457"> <customerid="cust0925" /> </order> </orders> <orderid="ord123456"> <customerid="cust0921" /> </order> <orderid="ord123457"> <customerid="cust0925" /> </order>

  7. XML Namespaces • 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 <Customerxmlns="urn:AW_NS"xmlns:o="urn:AW_OrderNS" CustomerID="1"CustomerType="S"> <o:OrderSalesOrderID="43860"Status="5" OrderDate="2001-08-01T00:00:00"> <o:OrderDetailProductID="761"Quantity="2"/> <o:OrderDetailProductID="770"Quantity="1"/> </o:Order> </Customer>

  8. XML Schemas • XML schema describes the structure of XML document • XML schema language is also called XML Schema Definition (XSD) An XML schema provides the following: • Validation constraints • SQL Server validates the instance • Data type information • Schemas provide info about types of attributes and elements

  9. Appropriate Usage of XML Data Storage in SQL Server

  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 • 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 -- usage within table definition CREATETABLEApp.Settings (SessionIDintPRIMARYKEY, WindowSettingsxml ); -- usage as local variable DECLARE @Settings xml; -- usage as parameter to stored procedure CREATEPROCEDUREApp.SaveSettings (@SettingsToSavexml)AS...

  13. XML Schema Collections • 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 The XML schema collection: • Stores the imported XML schemas • Validates XML instances • Types the XML data as it is stored in the database

  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 CONTENT (allows fragments) DOCUMENT (no fragments) • XML data type stores CONTENT (including fragments) by default • Can be required to store DOCUMENT CREATETABLEApp.Settings (SessionIDintPRIMARYKEY, WindowSettingsxml (CONTENT App.SettingsSchemaCollection) ); CREATETABLEApp.Settings (SessionIDintPRIMARYKEY, WindowSettingsxml (DOCUMENT App.SettingsSchemaCollection) );

  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

  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: 45minutes

  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

More Related