Identity Constraints

Consider the example shown below. It models a Person which can have an Address, and zero or more Relationships to other Persons. A Relationship does not "contain" a Person. Rather, it has a reference to a Person.

How do we ensure that one entity uniquely refers to another entity? We use XML identity constraints. These constraints are well-described in the relevant XML specifications and we will not elaborate on them here, other than to point out additional restrictions on the use of identity constraints in a Datastore Schema.

<xs:element name="Person">
    <xs:complexType>
        <xs:sequence minOccurs="0">
            <xs:element ref="Address" 
            minOccurs="0" 
            maxOccurs="1" />
            <xs:element ref="Relationship" minOccurs="0" 
            maxOccurs="unbounded"/>                
        </xs:sequence>
        <xs:attribute name="personID" 
            type="D:SVR_KEY"/>            
        <xs:attribute name="firstName" 
            type="D:SVR_STRING" />
        <xs:attribute name="middleInitial" 
            type="D:SVR_STRING" />
        <xs:attribute name="lastName" type="D:SVR_STRING" />
    </xs:complexType>
    <xs:key name="PersonKey"> 
        <xs:selector xpath="./Person"/> 
        <xs:field xpath="@personID"/> 
    </xs:key>
    <xs:keyref name="RelationshipRef" refer="PersonKey"> 
        <xs:selector xpath="./Person/Relationship"/> 
        <xs:field xpath="@personID"/> 
    </xs:keyref>        
</xs:element>
<xs:element name="Address">
    <xs:complexType>
        <xs:attribute name="street1" type="D:SVR_STRING" />
        <xs:attribute name="street2" type="D:SVR_STRING" />
        <xs:attribute name="city" type="D:SVR_STRING" />
        <xs:attribute name="state" type="D:SVR_STRING" />
        <xs:attribute name="zipCode" type="D:SVR_INT32" />
    </xs:complexType>
</xs:element>
<xs:element name="Relationship">
    <xs:complexType>
        <xs:attribute name="type" type="D:SVR_STRING"/>
        <xs:attribute name="personID" type="D:SVR_KEY"/>
    </xs:complexType>
</xs:element>

An attribute referred to in a key or keyref identity constraint must be defined to be of the base domain type SVR_KEY. Conversely, every attribute of type SVR_KEY must be referred to by some key/ or keyref definition. Key attributes have their values automatically populated upon insertion into the Datastore. They are set to a numeric value that is unique across all entities on the Datastore.