Uniqueness Constraints

Figure 673. Introducing nicknames Slide presentation Create comment in forum
<contact nickname="Blue"> ◀───────┐
  <firstName>Eve</firstName> ...  
</contact>                         <!-- Error: Nicknames must be unique-->
                                  
<contact>  <!-- O.k.: Nickname    
               is optional-->     
  <firstName>Petra</firstName> ...
</contact>                        
                                  
<contact nickname="Blue"> ◀───────┘
  <firstName>Joe</firstName>...
</contact>

Figure 674. Nickname semantics Slide presentation Create comment in forum
  • Optional nickname value.

  • Unique among all <contact> elements if present.

Relational databases: UNIQUE constraint.

CREATE TABLE Contact (
   ...
   nickname CHAR(10) NULL UNIQUE
...
)

Figure 675. <xs:unique> Slide presentation Create comment in forum
<xs:element name="organizer">
...<xs:element ref="contact" .../>
       ...
...<xs:unique name="nicknameUnique"> 
     <xs:selector       xpath = "contact"/>
     <xs:field xpath="@nickname"/>   
  </xs:unique>             └───┐      
</xs:element> ...    ┌─────────┼─────┘
                              
<xs:element name="contact">     
...     <xs:attribute name="nickname" .../>
  ...
</xs:element>
<contact nickname="Blue"> ◀────┐
  <firstName>Eve </firstName>  
  <lastName>Dexter</lastName>  
</contact>                     
                               
<!-- Invalid: Duplicate unique │
   value [Blue] ... -->        │
                               
<contact nickname="Blue"> ◀────┘
  <firstName>Joe</firstName>
  <lastName>Simpson</lastName>
</contact>

As usual: <organizer> will have <contact> children.

Defining a uniqueness constraint. In contrast to SQL the constraint itself requires an identifying name.

Assured uniqueness among the set of all <contact> children of <organizer>.

The attribute being chosen to become unique.

Notice the @ distinguishing the attribute <contact nickname="Blue"> from a (possible) child element:

<contact>
   <nickname>Blue</nickname>
...
</contact>

The actual nickname attribute definition.


Figure 676. Introducing id values Slide presentation Create comment in forum
<contact id="123">
  <firstName>Joe</firstName>...</contact>

<contact> <!-- Error: Missing @id-->
  <firstName>Joe</firstName>...</contact>

<contact id="345">
  <firstName>Sue</firstName>...</contact>

<contact id="123"> <!-- Error: Duplicate @id value-->
  <firstName>Sue</firstName>...</contact>

Figure 677. id semantics Slide presentation Create comment in forum
  • Required value.

  • Unique among all <contact> elements.

Relational databases: PRIMARY KEY or UNIQUE + NOT NULL constraint.

CREATE TABLE Contact (
   ...
   id INT PRIMARY KEY
...
)

Figure 678. <xs:key> vs. <xs:unique> Slide presentation Create comment in forum
<xs:element name="organizer">
... <xs:element ref="contact".../>
    ...
    <xs:key name="primaryKey">
      <xs:selector  xpath="contact"/>
      <xs:field  xpath="@id"/> 
    </xs:key>         ┌───┼────┘
</xs:element>            └───┐
<xs:element name="contact">   
...<xs:attribute       name="id"
    type="xs:int" use="optional" />
...
<!-- Invalid: Element "organizer"
has no value for the
key "primaryKey". -->
<contact birthDate="1995-11-29"
  category="businessPartner"
  nickname="Blue">
  <firstName>Eve </firstName>
  <lastName>Dexter</lastName>
</contact>

Note: Although the id attribute is being declared optional the <xs:key> rather than a weaker <xs:unique> constraint still enforces its presence.


Figure 679. Enhancing error message understanding Slide presentation Create comment in forum
<xs:element name="organizer">
... <xs:element ref="contact".../>
    ...
    <xs:key name="primaryKey">
      <xs:selector xpath="contact"/>
      <xs:field xpath="@id"/>
    </xs:key>
</xs:element>
<xs:element name="contact">
...<xs:attribute name="id"
    type="xs:int" use="required" />
...
<!-- Invalid: Element "organizer"
has no value for the
key "primaryKey". -->
<contact birthDate="1995-11-29"
  category="businessPartner"
  nickname="Blue">
  <firstName>Eve </firstName>
  <lastName>Dexter</lastName>
</contact>