Share via


Map unique XML Schema (XSD) Constraints to DataSet Constraints

In an XML Schema definition language (XSD) schema, the unique element specifies the uniqueness constraint on an element or attribute. In the process of translating an XML Schema into a relational schema, the unique constraint specified on an element or attribute in the XML Schema is mapped to a unique constraint in the DataTable in the corresponding DataSet that is generated.

The following table outlines the msdata attributes that you can specify in the unique element.

Attribute name

Description

msdata:ConstraintName

If this attribute is specified, its value is used as the constraint name. Otherwise, the name attribute provides the value of the constraint name.

msdata:PrimaryKey

If PrimaryKey="true" is present in the unique element, a unique constraint is created with the IsPrimaryKey property set to true.

The following example shows an XML Schema that uses the unique element to specify a uniqueness constraint.

<xs:schema id="SampleDataSet" 
            xmlns:xs="http://www.w3.org/2001/XMLSchema" 
            xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Customers">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="CustomerID" type="xs:integer" 
           minOccurs="0"/>
        <xs:element name="CompanyName" type="xs:string" 
           minOccurs="0"/>
       <xs:element name="Phone" type="xs:string" />
     </xs:sequence>
   </xs:complexType>
 </xs:element>

 <xs:element name="SampleDataSet" msdata:IsDataSet="true">
  <xs:complexType>
    <xs:choice maxOccurs="unbounded">
      <xs:element ref="Customers" />
    </xs:choice>
  </xs:complexType>
   <xs:unique    
msdata:ConstraintName="UCustID"    
name="UniqueCustIDConstr" >      
<xs:selector xpath=".//Customers" />      
<xs:field xpath="CustomerID" />    
</xs:unique>
</xs:element>
</xs:schema>

The unique element in the schema specifies that for all Customers elements in a document instance, the value of the CustomerID child element must be unique. In building the DataSet, the mapping process reads this schema and generates the following table:

Customers (CustomerID, CompanyName, Phone)

The mapping process also creates a unique constraint on the CustomerID column, as shown in the following DataSet. (For simplicity, only relevant properties are shown.)

DataSetName: MyDataSet
TableName: Customers
  ColumnName: CustomerID
      AllowDBNull: True
      Unique: True
  ConstraintName: UcustID
      Type: UniqueConstraint
      Table: Customers
      Columns: CustomerID 
      IsPrimaryKey: False

In the DataSet that is generated, the IsPrimaryKey property is set to False for the unique constraint. The unique property on the column indicates that the CustomerID column values must be unique (but they can be a null reference, as specified by the AllowDBNull property of the column).

If you modify the schema and set the optional msdata:PrimaryKey attribute value to True, the unique constraint is created on the table. The AllowDBNull column property is set to False, and the IsPrimaryKey property of the constraint set to True, thus making the CustomerID column a primary key column.

You can specify a unique constraint on a combination of elements or attributes in the XML Schema. The following example demonstrates how to specify that a combination of CustomerID and CompanyName values must be unique for all Customers in any instance, by adding another xs:field element in the schema.

   <xs:unique   
            msdata:ConstraintName="SomeName"  
            name="UniqueCustIDConstr" > 
     <xs:selector xpath=".//Customers" /> 
     <xs:field xpath="CustomerID" /> 
     <xs:field xpath="CompanyName" /> 
   </xs:unique>

This is the constraint that is created in the resulting DataSet.

ConstraintName: SomeName
  Table: Customers
  Columns: CustomerID CompanyName 
  IsPrimaryKey: False

See Also

Concepts

Generating DataSet Relations from XML Schema (XSD)

Other Resources

Mapping XML Schema (XSD) Constraints to DataSet Constraints

ADO.NET Managed Providers and DataSet Developer Center