Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 16: A Brief XML Primer 2. We have allowed NULLs but could have just as easily chosen NOT NULL as a constraint. Note, however, that the NOT NULL would be enforced on whether the row had any data for that column, not whether that data was valid. 3. Our XML is considered “non-typed XML.” That is, since we have not associated any schema with it, SQL Server doesn’t really know anything about how this XML is supposed to behave to be considered “valid.” The first of these is implied in any column that is defined with the data type XML rather than just plain text. We will see much more about this in our next XML data type section. The second goes with any data type in SQL Server — we can specify whether we allow NULL data or not for that column. So, the real meat in terms of changes we can make at definition time has to do with whether we specify our XML column as being typed or non-typed XML. The non-typed definition we used in the preceding example means that SQL Server knows very little about any XML stored in the column and, therefore, can do little to police its validity. If we set the column up as being typed XML, then we are providing much more definition about what is considered “valid” for any XML that goes in our column. The AdventureWorks2008 database already has schema collections that match the validation we want to place on our two XML columns, so let’s look at how we would change our CREATE statement to adjust to typed XML: CREATE TABLE Production.ProductModel ( ProductModelID int IDENTITY(1,1) PRIMARY KEY NOT NULL, Name dbo.Name NOT NULL, CatalogDescription xml (CONTENT [Production].[ProductDescriptionSchemaCollection]) NULL, Instructions xml (CONTENT [Production].[ManuInstructionsSchemaCollection]) NULL, rowguid uniqueidentifier ROWGUIDCOL NOT NULL, ModifiedDate datetime NOT NULL CONSTRAINT DF_ProductModel_ModifiedDate DEFAULT (GETDATE()) ); This represents the way it is defined in the actual AdventureWorks2008 sample. In order to insert a record into the Production.ProductModel table, you must either leave the CatalogDescription and Instructions fields blank or supply XML that is valid when tested against their respective schemas. XML Schema Collections 488 XML schema collections are really nothing more than named persistence of one or more schema documents into the database. The name amounts to a handle to your set of schemas. By referring to that collection, you are indicating that the XML typed column or variable must be valid when matched against all of the schemas in that collection. We can view existing schema collections. To do this, we utilize the built-in XML_SCHEMA_NAMESPACE() function. The syntax looks like this: XML_SCHEMA_NAMESPACE( , , [] )

This is just a little confusing, so let’s touch on these parameters just a bit: Parameter Description So, to use this for the Production.ManuInstructionsSchemaCollection schema collection, we would make a query like this: SELECT XML_SCHEMA_NAMESPACE(‘Production’,’ManuInstructionsSchemaCollection’); This spews forth a ton of unformatted XML: Chapter 16: A Brief XML Primer SQL Server schema This is your relational database schema (not to be confused with the XML schema). For example, for the table Production .ProductModel, Production is the relational schema. For Sales.SalesOrderHeader, Sales is the relational schema. xml schema collection The name used when the XML schema collection was created. In your CREATE table example previously, you referred to the ProductDescriptionSchemaCollection and ManuInstructionSSchemaCollection XML schema collections. namespace Optional name for a specific namespace within the XML schema collection. Remember that XML schema collections can contain multiple schema documents — this would return anything that fell within the specified namespace. 489

This is just a little confusing, so let’s touch on these parameters just a bit:<br />

Parameter Description<br />

So, to use this for the Production.ManuInstructionsSchemaCollection schema collection, we<br />

would make a query like this:<br />

SELECT XML_SCHEMA_NAMESPACE(‘Production’,’ManuInstructionsSchemaCollection’);<br />

This spews forth a ton of unformatted XML:<br />

Chapter 16: A Brief XML Primer<br />

<strong>SQL</strong> <strong>Server</strong> schema This is your relational database schema (not to be confused with<br />

the XML schema). For example, for the table Production<br />

.ProductModel, Production is the relational schema. For<br />

Sales.SalesOrderHeader, Sales is the relational schema.<br />

xml schema collection The name used when the XML schema collection was created.<br />

In your CREATE table example previously, you referred to the<br />

ProductDescriptionSchemaCollection and<br />

ManuInstructionSSchemaCollection XML schema collections.<br />

namespace Optional name for a specific namespace within the XML schema<br />

collection. Remember that XML schema collections can contain<br />

multiple schema documents — this would return anything that fell<br />

within the specified namespace.<br />

<br />

489

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!