Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use XML type to store XML data in SQL Server?

I'm playing around and learning to use Microsoft SQL Server. I want to store XML documents in a table, parts of the XML document won't be modified within the table (i.e. any changes will be done by updating the whole XML document in that cell).

From what I can see, I can store the XML documents in a column of type Xml or in a varchar(MAX).

What are the pros and cons of each?

like image 286
millie Avatar asked Aug 11 '11 16:08

millie


People also ask

Why XML is used in SQL Server?

XML (eXtensible Markup Language) is one of the most common formats used to share information between different platforms. Owing to its simplicity and readability, it has become the de-facto standard for data sharing. In addition, XML is easily extendable.

Why do we use XML in database?

XML Database is used to store huge amount of information in the XML format. As the use of XML is increasing in every field, it is required to have a secured place to store the XML documents. The data stored in the database can be queried using XQuery, serialized, and exported into a desired format.

What is XML data type in SQL Server?

The xml data type is a built-in data type in SQL Server and is somewhat similar to other built-in types such as int and varchar. As with other built-in types, you can use the xml data type as a column type when you create a table as a variable type, a parameter type, a function-return type, or in CAST and CONVERT.

Is XML good for storing data?

XML is a format to store data along with its structure. This feature makes it useful for many things, including transferring data, formatting documents, creating layouts, and more.


1 Answers

XML datatype supports:

  • XML schema validation
  • XML Indexing
  • XML data methods to query and manipulate XML via XPath/XQuery

Besides, using an XML type it will be harder for you to do the typical mistakes junior developers do when handling XML: threat it as a string, mix or ignore encodings like UTF8 and UTF16, ignore namespaces, confuse or ignore processing instructions etc etc.

Please read XML Best Practices for Microsoft SQL Server 2005

like image 98
Remus Rusanu Avatar answered Nov 15 '22 15:11

Remus Rusanu