Enforcing Referential Integrity with XML Schema Identity Constraints
A schemadatabase, XML, or otherwisespecifies the structure and content of data. For the most part, the XML Schema Recommendation provides mechanisms for validating distinct datum (via simple types) and the structure of data (via complex types). XML Schema also provides for identity constraints, which permit a simplistic but powerful way to assure referential integrity.
The following scenario provides a common ground for discussing referential integrity:
There is a catalog consisting of part numbers, descriptions, prices, etc. Each part number must be unique, and the part number is a key against which other values (listed next) are validated.
Each order has a unique order identifier.
An order contains a distinct set of part numbers validated against the catalog. One slight complication of selecting the intended elements is that part numbers do not have to be unique across orders.
Each order has at most onebut often noshipment identifier.
While there are lots of details covered in this article, there are just three keywords that combine to provide XML Schema referential integrity:
key specifies that the values for a particular element might be a key against which integrity is validated. Every key must be unique, and every key must exist. For example, a part number might be a key in a catalog. That is, every item in the catalog has a part number, and that part number is distinct from every other part number.
unique is exactly like key, except that the key does not have to exist. For example, for all orders, the shipping identifier must be unique. However, not every order has a shipping identifier (the order may be pending shipment).
keyref specifies a reference to an element specified by key or unique. For example, each part number in an order must refer to a part number in the catalog.
Due to the complexity of constructing meaningful paths for the selectors and fields of an identity constraint, the individual element examples in this article do not make much sense by themselves. An XML schema with identity constraints and a corresponding XML instance provide context for the rest of this document.