다음을 통해 공유


키 XSD(XML 스키마) 제약 조건을 DataSet 제약 조건에 매핑

스키마에서 키 요소를 사용하여 요소 또는 특성에 제약 조건을 지정할 수 있습니다. 키 제약 조건이 지정된 요소 또는 특성은 스키마 인스턴스에 고유 값이 있어야 하며 null 값을 가질 수 없습니다.

키 제약 조건은 키 제약 조건이 정의된 열에 null 값을 가질 수 없다는 점을 제외하고 고유 제약 조건과 유사합니다.

다음 표에서는 요소에서 지정할 수 있는 msdata 특성을 간략하게 설명합니다.

속성 이름 설명
msdata:ConstraintName 이 특성을 지정하면 해당 값이 제약 조건 이름으로 사용됩니다. 그렇지 않으면 이름 특성은 제약 조건 이름의 값을 제공합니다.
msdata:PrimaryKey 있는 경우 PrimaryKey="true"IsPrimaryKey 제약 조건 속성은 true로 설정되므로 기본 키로 설정됩니다. 기본 키는 null 값을 가질 수 없으므로 AllowDBNull 열 속성은 false로 설정됩니다.

키 제약 조건이 지정된 스키마를 변환할 때 매핑 프로세스는 제약 조건의 각 열에 대해 AllowDBNull 열 속성이 false 로 설정된 테이블에 고유한 제약 조건을 만듭니다. 요소에 지정하지 않은 경우 고유 제약 조건의 IsPrimaryKey 속성도 msdata:PrimaryKey="true"로 설정됩니다. 이는 스키마 PrimaryKey="true"에 있는 고유 제약 조건과 동일합니다.

다음 스키마 예제에서 요소는 CustomerID 요소에 대한 키 제약 조건을 지정합니다.

<xs:schema id="cod"  
            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:string" 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="MyDataSet" msdata:IsDataSet="true">  
  <xs:complexType>  
    <xs:choice maxOccurs="unbounded">  
      <xs:element ref="Customers" />  
    </xs:choice>  
  </xs:complexType>  
   <xs:key  msdata:PrimaryKey="true"  
       msdata:ConstraintName="KeyCustID"  
          name="KeyConstCustomerID" >  
     <xs:selector xpath=".//Customers" />  
     <xs:field xpath="CustomerID" />  
    </xs:key>  
 </xs:element>  
</xs:schema>

요소는 Customers 요소의 CustomerID 자식 요소 값에 고유한 값이 있어야 하며 null 값을 가질 수 없음을 지정합니다. XSD(XML 스키마 정의 언어) 스키마를 번역할 때 매핑 프로세스는 다음 테이블을 만듭니다.

Customers(CustomerID, CompanyName, Phone)  

또한 XML 스키마 매핑은 다음과 같이 CustomerID 열에 DataSet를 만듭니다. (간단히 하기 위해 관련 속성만 표시됩니다.)

      DataSetName: MyDataSet  
TableName: customers  
  ColumnName: CustomerID  
      AllowDBNull: False  
      Unique: True  
  ConstraintName: KeyCustID  
      Table: customers  
      Columns: CustomerID
      IsPrimaryKey: True  

생성된 DataSet에서 UniqueConstraintIsPrimaryKey 속성은 스키마가 키 요소에 지정 msdata:PrimaryKey="true" 되기 때문에 true로 설정됩니다.

DataSet에서 UniqueConstraintConstraintName 속성 값은 스키마의 요소에 지정된 msdata:ConstraintName 특성의 값입니다.

참고하십시오