Dumies collection

Page 335

Chapter 17: Operating on XML Data with SQL Here’s an example of how you might use the XML type: CREATE TABLE CLIENT ( ClientName Address1 Address2 City State PostalCode Phone Fax ContactPerson Comments

CHARACTER (30) CHARACTER (30), CHARACTER (30), CHARACTER (25), CHARACTER (2), CHARACTER (10), CHARACTER (13), CHARACTER (13), CHARACTER (30), XML(SEQUENCE) ) ;

NOT NULL,

This SQL statement will store an XML document in the Comments column of the CLIENT table. The resulting document might look something like the following: <Comments> <Comment> <CommentNo>1</CommentNo> <MessageText>Is VetLab equipped to analyze penguin blood?</MessageText> <ResponseRequested>Yes</ResponseRequested> </Comment> <Comment> <CommentNo>2</CommentNo> <MessageText>Thanks for the fast turnaround on the leopard seal sputum sample.</MessageText> <ResponseRequested>No</ResponseRequested> </Comment> </Comments>

When not to use the XML type Just because SQL:2003 allows you to use the XML type doesn’t mean that you always should. In fact, on many occasions, it doesn’t make sense to use the XML type. Most data in relational databases today is better off in its current format than it is in XML format. Here are a couple of examples of when not to use the XML type: ⻬ When the data breaks down naturally into a relational structure with tables, rows, and columns. ⻬ When you will need to update pieces of the document, rather than deal with the document as a whole.

315


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.