17.06.2013 Views

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

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

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

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 16: A Brief XML Primer<br />

2. We have allowed NULLs but could have just as easily chosen NOT NULL as a constraint. Note,<br />

however, that the NOT NULL would be enforced on whether the row had any data for that column,<br />

not whether that data was valid.<br />

3. Our XML is considered “non-typed XML.” That is, since we have not associated any schema<br />

with it, <strong>SQL</strong> <strong>Server</strong> doesn’t really know anything about how this XML is supposed to behave to<br />

be considered “valid.”<br />

The first of these is implied in any column that is defined with the data type XML rather than just plain<br />

text. We will see much more about this in our next XML data type section.<br />

The second goes with any data type in <strong>SQL</strong> <strong>Server</strong> — we can specify whether we allow NULL data or not<br />

for that column.<br />

So, the real meat in terms of changes we can make at definition time has to do with whether we specify<br />

our XML column as being typed or non-typed XML. The non-typed definition we used in the preceding<br />

example means that <strong>SQL</strong> <strong>Server</strong> knows very little about any XML stored in the column and, therefore,<br />

can do little to police its validity. If we set the column up as being typed XML, then we are providing<br />

much more definition about what is considered “valid” for any XML that goes in our column.<br />

The AdventureWorks<strong>2008</strong> database already has schema collections that match the validation we want to<br />

place on our two XML columns, so let’s look at how we would change our CREATE statement to adjust to<br />

typed XML:<br />

CREATE TABLE Production.ProductModel<br />

(<br />

ProductModelID int IDENTITY(1,1) PRIMARY KEY NOT NULL,<br />

Name dbo.Name NOT NULL,<br />

CatalogDescription xml<br />

(CONTENT [Production].[ProductDescriptionSchemaCollection]) NULL,<br />

Instructions xml<br />

(CONTENT [Production].[ManuInstructionsSchemaCollection]) NULL,<br />

rowguid uniqueidentifier ROWGUIDCOL NOT NULL,<br />

ModifiedDate datetime NOT NULL<br />

CONSTRAINT DF_ProductModel_ModifiedDate DEFAULT (GETDATE())<br />

);<br />

This represents the way it is defined in the actual AdventureWorks<strong>2008</strong> sample. In order to insert a<br />

record into the Production.ProductModel table, you must either leave the CatalogDescription and<br />

Instructions fields blank or supply XML that is valid when tested against their respective schemas.<br />

XML Schema Collections<br />

488<br />

XML schema collections are really nothing more than named persistence of one or more schema documents<br />

into the database. The name amounts to a handle to your set of schemas. By referring to that collection,<br />

you are indicating that the XML typed column or variable must be valid when matched against<br />

all of the schemas in that collection.<br />

We can view existing schema collections. To do this, we utilize the built-in XML_SCHEMA_NAMESPACE()<br />

function. The syntax looks like this:<br />

XML_SCHEMA_NAMESPACE( , , [] )

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

Saved successfully!

Ooh no, something went wrong!