Search
Database

SQL Server 2008 : Explaining XML - XML Schema

Published 3 min readBy Programming4us Editorial Team
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

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

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

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

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

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

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