SQL Server XML schema: is there any way to add unique constraint on Xml?

678 Views Asked by At

The following create xml schema collection code returns the error of

Msg 9336, Level 16, State 1, Line 34
The XML Schema syntax 'unique' is not supported.
create xml schema collection cs.T as N'  
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  <xsd:element name="Column">
    <xsd:unique name="uniqueKey">
      <xsd:selector xpath="@ColumnKey"/>
      <xsd:field xpath="."/>
    </xsd:unique>
  </xsd:element>
</xsd:schema>';

Is there any way to set unique constraint by other ways?

1

There are 1 best solutions below

1
Andrew On

That is correct: <xsd:unique> is unsupported. From the documentation:

Currently, SQL Server does not support these XSD-based constraints for enforcing uniqueness or establishing keys and key references. XML schemas that contain these elements cannot be registered.

As the documentation states, the same issue applies to <xsd:key> and <xsd:keyref> as well.