DATABASE

SQL Server 2008 : Explaining XML - XML Schema

2/15/2011 7:47:50 PM
XML schemas provide a means to define the structure and content of an XML document. SQL Server supports a subset of the XML Schema and uses three DDL statements to manage XML schema collections: CREATE XML SCHEMA COLLECTION, ALTER XML SCHEMA COLLECTION, and DROP XML SCHEMA COLLECTION. XML schema collections are XML schemas that are imported into a SQL Server database and are defined using the XSD (XML Schema Definition) language.

We will first explore the CREATE XML SCHEMA COLLECTION DDL statement. The CREATE XML SCHEMA COLLECTION DDL uses the syntax shown in Figure 1. Refer to Table 1 for arguments and descriptions.

Figure 1. CREATE XML SCHEMA COLLECTION Syntax
CREATE XML SCHEMA COLLECTION [ <relational_schema>. ]sql_identifier AS
Expression

Table 1. CREATE XML SCHEMA COLLECTION Arguments
ArgumentsDescription
Relational_schemaIdentifies the relational schema name. If not specified, default relational schema is assumed.
sql_identifierIs the SQL identifier for the XML schema collection.
ExpressionIs a string constant or scalar variable. Is varchar, varbinary, nvarchar, or XML type.

Let’s examine the CREATE XML SCHEMA COLLECTION. In Figure 2, we created a sample XML schema collection using the CREATE XML SCHEMA COLLECTION DDL statement.

Figure 2. Sample CREATE XML SCHEMA COLLECTION
CREATE XML SCHEMA COLLECTION customerSchema AS
'<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="root" type="string"/>
</schema>'

After the schema collection is created, you can then assign it to an XML variable or a column with the schema declarations it contains. You do this by referencing the schema collection in the CREATE TABLE statement shown in Figure 3.

Figure 3. CREATE TABLE statement defining the XML SCHEMA COLLECTION
CREATE TABLE [dbo].[XML_Table](
[pk] [int] IDENTITY(1,1) NOT NULL,
[customerName] [varchar](255) NULL,
[customerPhone] [varchar](255) NULL,
[customerAddress] [xml](customerSchema) NULL,
CONSTRAINT [PK_XML_Table] PRIMARY KEY CLUSTERED
(
[pk] ASC
))

The ALTER XML SCHEMA COLLECTION allows you to add new schema components to an existing XML schema collection. The ALTER XML SCHEMA COLLECTION DDL uses the following syntax as shown in Figure 4. Table 2 lists all the arguments and descriptions for the ALTER XML SCHEMA COLLECTION.

Figure 4. ALTER XML SCHEMA COLLECTION Syntax
ALTER XML SCHEMA COLLECTION [ relational_schema. ]sql_identifier ADD
'Schema Component'

Table 2. ALTER XML SCHEMA COLLECTION Arguments
ArgumentDescription
relational_schemaIdentifies the relational schema name. If not specified, the default relational schema is assumed.
sql_identifierIs the SQL identifier for the XML schema collection.
‘Schema Component’Is the schema component to insert.

In Figure 5, we will add a new schema component to the customerSchema.

Figure 5. Sample Usage of ALTER XML SCHEMA COLLECTION
ALTER XML SCHEMA COLLECTION customerSchema ADD
'<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="customer" type="string"/>
</schema>'

The DROP XML SCHEMA COLLECTION removes an XML Schema from the database. You cannot drop an XML schema while it is in use. So if the XML Schema is associated with an XML-type parameter or column, the table column has to be altered without specifying the schema. If you try to drop the schema, while in use, you will receive an error. In the following example, Figure 6, we receive an error when we try to drop the XML Schema, customerSchema, on table XML_Table.

Figure 6. Sample Usage of DROP XML SCHEMA
DROP XML SCHEMA COLLECTION customerSchema
GO

Msg 6328, Level 16, State 1, Line 1
Specified collection 'customerSchema' cannot be dropped because it is used by
object 'dbo.XML_Table'.


To drop an XML schema collection, we will alter the table and remove the schema on the XML column and then drop the schema, as shown in Figure 7. Refer to Table 3 for a list of arguments and descriptions for the DROP XML SCHEMA COLLECTION.

Table 3. DROP XML SCHEMA COLLECTION Arguments
ArgumentDescription
relational_schemaIdentifies the relational schema name. If not specified, the default relational schema is assumed.
sql_identifierIs the name of the XML schema collection to drop.

To properly drop the XML Schema, customerSchema, we need to use the ALTER TABLE statement and then execute a DROP XML SCHEMA COLLECTION. We will use syntax shown in Figure 7 to accomplish this task.

Figure 7. The ALTER TABLE Statement
ALTER TABLE XML_Table ALTER COLUMN customerAddress XML
GO
DROP XML SCHEMA COLLECTION customerSchema
Other  
 
Video
Top 10
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
The latest Audi TT : New angles for TT
Era of million-dollar luxury cars
Game Review : Hearthstone - Blackrock Mountain
Game Review : Battlefield Hardline
Google Chromecast
Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
Michael Kors Designs Stylish Tech Products for Women
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
Popular Tags
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone