Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 98 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
98
Dung lượng
2,62 MB
Nội dung
258 Chapter 8 Managing XML Data the data in the XML structure. XML Schema enables you to declare optional sections inside the schema or generic types that accept any XML fragment. This capability means you can represent not only structured data but also semistructured and unstructured data. Fourth, XML data is searchable. Because of XML’s hierarchical structure, you can apply multiple algorithms to search inside tree structures. XQUERY and XPATH are query languages designed to search XML data. And fifth, XML data is extensible. You can manipulate XML data by inserting, modi- fying, or deleting nodes. This capability means that you can create new XML instances out of existing XML structures. NOTE Data representation types Here are definitions of the three data representation types: ■ Structured data Homogeneous static data structure in which all instances of the data follow the same structure. ■ Semistructured data Heterogeneous data structure that can contain dynamic or optional structures. Instances can look completely different from each other but still conform to the same schema. ■ Unstructured data Heterogeneous data that does not conform to a schema. In XML, data can exist without having a schema to define its structure. Applications that manipulate XML execute a variety of actions on data, such as creat- ing new XML documents, filtering an XML document and extracting relevant nodes based on a filter expression, transforming an XML fragment into another XML struc- ture, and updating or modifying the current data inside the XML structure. The way applications store XML data affects which of these possible actions are at your disposal. SQL Server 2005 enables you to store XML data in two ways: ■ As XML in the database in a text column ■ As XML in the database in an XML data type column MORE INFO Storing XML data as relational data Lesson 5 in this chapter covers storing data as a relational representation and applying composi- tion and shredding techniques to transform relational data into XML and back. C0862271X.fm Page 258 Friday, April 29, 2005 7:38 PM Lesson 1: Working with XML Structures 259 Storing XML in Text Columns You can store XML data in a text column by using the (n)char, (n)varchar, or varbinary data types. For these data types, SQL Server 2005 introduces the MAX argument, which allocates a maximum storage size of 2 GB. The following code example stores XML data in the nvarchar data type: DECLARE @myXML AS nvarchar(max) SET @myXML = '<log><application>Sales</application><description>The connection timed out</description></log>' CAUTION Deprecated data types Microsoft intends to drop support for the text, ntext, and image data types in upcoming SQL Server versions. For this reason, Microsoft recommends that you stop using these data types. The key benefits of storing XML data in SQL Server 2005 text columns are the following: ■ XML provides textual fidelity. All details such as comments and white space are preserved. ■ It does not depend on database capabilities. ■ It reduces the processing workload on the database server because all process- ing of XML data happens in a middle tier. ■ It provides the best performance for document-level insertion and retrieval. Doc- ument-level means that if you want to execute operations at the node level, you are forced to work with the complete XML document because SQL Server is not aware of what is stored in this column. Some limitations of storing XML in SQL Server 2005 text columns are as follows: ■ Coding complexity (and related higher maintenance cost) is added in the mid- dle tier. ■ You can’t manipulate, extract, or modify XML data at the node level. ■ Searching XML data always involves reading the entire document because XML is interpreted as text by the database server. ■ XML validation, well-formedness, and type checking must be executed in the middle tier. C0862271X.fm Page 259 Friday, April 29, 2005 7:38 PM 260 Chapter 8 Managing XML Data MORE INFO Well-formed XML Well-formed XML is an XML document that meets a set of constraints specified by the World Wide Web Consortium (W3C) Recommendation for XML 1.0. For example, well-formed XML must contain a root-level element, and any other nested elements must open and close properly without intermixing. SQL Server 2005 validates some of the well-formedness constraints. Some rules, such as the requirement for a root-level element, are not enforced. For a complete list of well-formedness requirements, read the W3C Recommendation for XML 1.0 at http://www.w3.org/TR/REC-xml. Quick Check 1. What are two benefits of storing XML in a text column in SQL Server 2005? 2. What are two disadvantages of storing XML in a text column in SQL Server 2005? Quick Check Answers 1. Possible answers include the following: XML provides textual fidelity, does not depend on database capabilities, reduces the processing workload on the database server, and provides the best performance for document-level insertion and retrieval. 2. Possible answers include the following: it’s impossible to manipulate, extract, or modify the data at the node level; searching XML data always involves reading the entire document; XML validation must be executed in the middle tier; and there is extra coding complexity in the middle tier. Storing XML in XML Data Type Columns You can use the new XML data type in SQL Server 2005 as you use any other native SQL Server data type: to define columns on tables, to define parameters for functions and stored procedures, and to create variables. As the following code example dem- onstrates, the XML data type column accepts both XML documents and XML frag- ments; this behavior is specified in the SQL/XML ISO-ANSI Standard Part 14. CREATE TABLE UniversalLog(recordID int, description XML) INSERT UniversalLog(recordID, description) VALUES(1, '<log><application>Sales</application><description>The connection timed out.</description></log>') INSERT UniversalLog(recordID, description) VALUES(1, 'database unavailable') C0862271X.fm Page 260 Friday, April 29, 2005 7:38 PM Lesson 1: Working with XML Structures 261 You can also use the XML data type to define parameters and variables, as the follow- ing code example demonstrates: CREATE PROCEDURE AddRecordToLog (@record AS XML) AS procedure body GO DECLARE @logRecord AS XML SET @logRecord = '<log><application>Sales</ application><description>The connection timed out.</description></log>' EXEC AddRecordToLog @logRecord SQL Server automatically converts the data types (n)char, (n)varchar, (n)text, varbi- nary, and image to the XML data type when assigning values to an XML parameter, column, or variable. The benefits of storing XML data by using the XML data type in SQL Server 2005 are as follows: ■ The XML data type is fully integrated with the SQL Server query engine and all other SQL Server services. The same query processor and query optimizer are used for both relational and XML queries. ■ The data is stored and manipulated natively as XML. ■ SQL Server 2005 provides fine-grained support for selecting, inserting, modify- ing, or deleting at the node level. ■ Performance improves for data-retrieval operations because multiple indexing is possible with the XML data type, so SQL Server reads only relevant nodes. ■ Document order and structure are preserved. Limitations of storing XML using the XML data type in SQL Server 2005 include the following: ■ Textual fidelity is not preserved. White space, the XML declaration at the top of the document, comments in the XML, attribute ordering, and other nondata ele- ments are removed from the structure. ■ The maximum allowed node depth is 128 levels. ■ The maximum allowed storage size is 2 GB. C0862271X.fm Page 261 Friday, April 29, 2005 7:38 PM 262 Chapter 8 Managing XML Data Quick Check 1. Which of the following INSERT statements will fail? (Choose all that apply.) A. INSERT UniversalLog(recordID, description) VALUES (1, '<ROOT/>') B. INSERT UniversalLog(recordID, description) VALUES (1, 'ROOT') C. INSERT UniversalLog(recordID, description) VALUES (1, '<ROOT>') D. INSERT UniversalLog(recordID, description) VALUES (1, '<ROOT> <A><b></a></B></ROOT>') Quick Check Answers 1. Will succeed: Represents a single-node XML document. 2. Will succeed: Represents an XML fragment. 3. Will fail: SQL Server validates the well-formedness of the XML document. The <ROOT> node is opened but never closed. 4. Will fail: SQL Server validates the well-formedness of the XML document. The hierarchy constructed by the A and B nodes is not closed properly. Also, XML is case sensitive, so the A node is not the same as the a node. Typing and Validating XML Data with XML Schemas An XML schema describes the structure and constrains the contents of XML docu- ments. Additionally, XML schemas provide type information that describes the nature of the data in elements and attributes. SQL Server 2005 supports untyped XML data and typed XML data. By binding an XML data type variable, column, or parameter to an XML schema, SQL Server gets input that lets it validate the correctness of the XML instance and to strongly type the nodes and contents of the XML instance. If an XML document conforms to what is declared inside an XML schema, the XML document is said to be valid. An invalid XML document does not conform to what is declared inside an XML schema. XML schemas are declared at the database level and deployed to SQL Server. XML schemas are valuable to SQL Server because they provide metadata that defines and constrains XML data types. After creating the XML schema as the following code shows, you can type and validate any XML data type column, variable, or parameter according to the XML schema collection. C0862271X.fm Page 262 Friday, April 29, 2005 7:38 PM Lesson 1: Working with XML Structures 263 Creating an XML Schema in SQL Server 2005 CREATE XML SCHEMA COLLECTION LogRecordSchema AS '<schema xmlns="http://www.w3.org/2001/XMLSchema"> <element name="log"> <complexType> <sequence> <element name="application" type="string"/> <element name="description" type="string"/> </sequence> </complexType> </element> </schema>' In the following code example, SQL Server validates the contents of the @myXML variable by the rules specified in all the XML schemas that compose the LogRecord- Schema schema collection: DECLARE @myXML AS XML(LogRecordSchema) SET @myXML = '<log><date>2005-11-07</date></log>' The assignment in the example fails because the XML instance does not conform to the XML structure declared by the XML schema collection. NOTE Loading an XML schema from a file In most cases, instead of retyping the complete XML schema, it is easier to load it from an XML schema file (extension .xsd). Use the OPENROWSET command in SQL Server 2005 to load the file into a variable of type XML: DECLARE @schema XML SELECT @schema = c FROM OPENROWSET ( BULK 'MyXMLSchema.xsd', SINGLE_BLOB) AS TEMP(c) CREATE XML SCHEMA COLLECTION MySchema AS @schema PRACTICE Creating a New Database In this practice, you will create a new database. In the database, you will create a new XML schema collection, loading it from an .xsd file. Then, you will create a table with columns of XML data type and constrain the XML columns to the XML schema col- lection. Finally, you will load data into the table. This database is the basic database you will use in the other lessons in this chapter. NOTE Code available on the companion CD The practices for this chapter are code intensive. So that you don’t have to type in the code exam- ples in the practices, the Practice Files\Chapter8 folder provides the code needed for all the prac- tices in this chapter. For solutions to the exercises in the Lesson 1 practice, see the Practice Files\Chapter8\Lesson 1\CompleteLesson1.sql file on the CD. C0862271X.fm Page 263 Friday, April 29, 2005 7:38 PM 264 Chapter 8 Managing XML Data Practice 1: Create the TK431Chapter8 Database, UniversalLog Table, and XML Schema In this exercise, you will create the necessary database schema elements to support typed XML data inside a database. 1. Open SQL Server Management Studio (SSMS) and open a connection to SQL Server 2005. 2. Issue a CREATE DATABASE statement to create a new database called TK431Chapter8. CREATE DATABASE TK431Chapter8 GO 3. Copy the Chapter8 folder from the companion CD to the root of the C drive. Then create an XML schema collection called LogRecordSchema. Your code might look like the following: USE TK431Chapter8 GO declare @schema XML SELECT @schema = c FROM OPENROWSET ( BULK 'C:\Chapter8\Lesson 1\logRecordSchema.xsd', SINGLE_BLOB) AS TEMP(c) CREATE XML SCHEMA COLLECTION LogRecordSchema AS @schema 4. Load the XML schema from the .xsd file in the C:\Chapter8 folder. The follow- ing code shows the LogRecordSchema XML schema: <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="logRecord" type="logRecordType" /> <xsd:simpleType name="flagEnum"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="warning" /> <xsd:enumeration value="information" /> <xsd:enumeration value="failure" /> <xsd:enumeration value="custom" /> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="eventEnum"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="appStart"/> <xsd:enumeration value="appClose"/> <xsd:enumeration value="logIn"/> <xsd:enumeration value="logOut"/> </xsd:restriction> </xsd:simpleType> C0862271X.fm Page 264 Friday, April 29, 2005 7:38 PM Lesson 1: Working with XML Structures 265 <xsd:complexType name="logRecordType"> <xsd:choice maxOccurs="unbounded"> <xsd:element name="information" type="informationType"/> <xsd:element name="error" type="errorType"/> <xsd:element name="post" type="postType"/> </xsd:choice> <xsd:attribute name="machine" type="xsd:string" /> <xsd:attribute name="timestamp" type="xsd:dateTime" /> </xsd:complexType> <xsd:complexType name="postType"> <xsd:sequence> <xsd:element name="moreInformation" type="xsd:string" maxOccurs="1" minOccurs="0"/> </xsd:sequence> <xsd:attribute name="eventType" type="eventEnum"/> </xsd:complexType> <xsd:complexType name="informationType"> <xsd:sequence> <xsd:element name="message" type="xsd:string" /> </xsd:sequence> <xsd:attribute name="flag" type="flagEnum" /> </xsd:complexType> <xsd:complexType name="errorType"> <xsd:sequence> <xsd:element name="message" type="xsd:string" /> <xsd:element name="module" type="xsd:string" /> </xsd:sequence> <xsd:attribute name="number" type="xsd:int" /> </xsd:complexType> </xsd:schema> 5. Issue a CREATE TABLE statement to create a new table called UniversalLog that contains the following columns: ❑ ID: INT data type. Set it as an identity column. Do not accept null values. ❑ LogDateTime: DATETIME data type. Default to current date and time. Do not accept null values. ❑ ApplicationName: NVARCHAR (50) data type. Do not accept null values. ❑ LogRecord: XML data type. Accept null values and bind the column to the LogRecordSchema schema collection. Your code should look like this: CREATE TABLE UniversalLog ( ID INT IDENTITY(1,1) NOT NULL, LogDateTime DATETIME NOT NULL CONSTRAINT [DF_UniversalLog_LogDateTime] DEFAULT (GetDate()), ApplicationName NVARCHAR(50) NOT NULL, LogRecord XML(LogRecordSchema) NULL ) C0862271X.fm Page 265 Friday, April 29, 2005 7:38 PM 266 Chapter 8 Managing XML Data NOTE Altering the LogRecord column If you created the table first and then the XML schema collection, you can alter the column in the table to map it to the XML schema by using the following code: ALTER TABLE UniversalLog ALTER COLUMN LogRecord XML (LogRecordSchema) Practice 2: Insert Log Records into the UniversalLog Table In this exercise, you will insert XML data representing log records into the Universal- Log table you created in Practice 1. 1. If necessary, open SSMS and open a connection to SQL Server 2005. 2. Connect to the TK431Chapter8 database you created in Practice 1. 3. Open the LogRecordsXML.sql file in the C:\Chapter8 folder. The file contains the following INSERT statements: INSERT UniversalLog(ApplicationName, LogRecord) VALUES ('SalesApp', '<logRecord machine="server1" timestamp="2000-01-12T12:13:14Z"/>') INSERT UniversalLog(ApplicationName, LogRecord) VALUES ('SalesApp', '<logRecord machine="server1"><information/></logRecord>') INSERT UniversalLog(ID, ApplicationName, LogRecord) VALUES (1, 'SalesApp', '<logRecord machine="server1" timestamp="2000-01-12T12:13:14Z"> <post eventType="appStart"> <moreInformation>All Services starting</moreInformation> </post> </logRecord>') INSERT UniversalLog(ID,ApplicationName, LogRecord) VALUES (2, 'Inventory', '<logRecord machine="server2" timestamp="2000-01-13T12:13:14Z"> <post eventType="appStart"/> <information flag="warning"> <message>Duplicate IP address</message> </information> </logRecord>') INSERT UniversalLog(ID,ApplicationName, LogRecord) VALUES (3, 'HR', '<logRecord machine="server1" timestamp="2000-01-14T12:13:14Z"> <error number="1001"> <message>The user does not have enough permissions to execute query</message> <module>DataAccessLayer</module> </error> </logRecord>') C0862271X.fm Page 266 Friday, April 29, 2005 7:38 PM Lesson 1: Working with XML Structures 267 INSERT UniversalLog(ID,ApplicationName, LogRecord) VALUES (4, 'CustomerService', '<logRecord machine="server2" timestamp="2000-01-15T12:13:14Z"> <post eventType="logOut"/> <information flag="custom"> <message>User must change password on next login</message> </information> </logRecord>') INSERT UniversalLog(ID,ApplicationName, LogRecord) VALUES (5, 'HoursReport', '<logRecord machine="server2" timestamp="2000-01-11T12:13:14Z"> <information flag="failure"> <message>Hard Disk with ID #87230283 is not responding</message> </information> <error number="18763"> <message>Application can not start</message> <module>AppLoader</module> </error> <post eventType="appStart"/> </logRecord>') 4. Execute each of the INSERT code segments in the file in turn by selecting the code and pressing F5 to execute. The first two INSERT statements are meant to return validation errors because the XML data does not conform to the XML schema collection. Pay attention to the messages SQL Server returns. Lesson Summary ■ The XML data-representation format is used to represent semistructured and unstructured data that you cannot represent relationally. ■ SQL Server 2005 provides a new XML data type for native storage of XML doc- uments and fragments in the relational database. ■ XML data can be typed and untyped. Typed XML is constrained by the declara- tions in an XML schema registered in an XML schema collection. Lesson Review The following questions are intended to reinforce key information presented in this lesson. The questions are also available on the companion CD if you prefer to review them in electronic form. NOTE Answers Answers to these questions and explanations of why each answer choice is right or wrong are located in the “Answers” section at the end of this book. C0862271X.fm Page 267 Friday, April 29, 2005 7:38 PM [...]... firstDay="199 9-0 1-0 5T00:00:00" yearsInRole="7">28 268 C0862271X.fm Page 290 Friday, April 29, 2005 7:38 PM 290 Chapter 8 Managing XML Data Sql: Variable and Sql: Column The XQUERY implementation in SQL Server. .. structures, SQL Server supports nested FOR XML queries (explained later in this lesson) MORE INFO Using FOR XML RAW For more information about the settings available to FOR XML RAW, read the topic “Using RAW Mode” in SQL Server 2005 Books Online SQL Server 2005 Books Online is installed as part of SQL Server 2005 Updates for SQL Server 2005 Books Online are available for download at www .microsoft. com/technet/prodtechnol /sql/ 2005/ downloads/books.mspx... 1 1 64 59... Quick Check 1 What is the main difference... Friday, April 29, 2005 7:38 PM Lesson 2: Retrieving XML Data by Using SQL Server Server-Side Technologies 291 when the XQUERY or XPATH expression returned at least one resulting node A 0 is returned when the XQUERY or XPATH expression did not return any resulting node The exist() method is usually used in the WHERE clause of the SELECT statement in Transact -SQL to validate that the expression actually . Mode” in SQL Server 2005 Books Online. SQL Server 2005 Books Online is installed as part of SQL Server 2005. Updates for SQL Server 2005 Books Online are available for download at www .microsoft. com/technet/prodtechnol /sql/ 2005/ downloads/books.mspx. Using. April 29, 2005 7:38 PM Lesson 2: Retrieving XML Data by Using SQL Server Server-Side Technologies 269 Lesson 2: Retrieving XML Data by Using SQL Server Server- Side Technologies SQL Server 2005 offers. Open SQL Server Management Studio (SSMS) and open a connection to SQL Server 2005. 2. Issue a CREATE DATABASE statement to create a new database called TK431Chapter8. CREATE DATABASE TK431Chapter8