Using Annotated XSD Schemas for Creating XML Views tutorial
Using sql:relationship to Specify Relationships

## Preliminaries

In the annotated XSD schema, the sql:relationship annotation is used to nest the schema elements hierarchically, on the basis of primary key and foreign key relationships among the underlying tables to which the elements map. In specifying the sql:relationship annotation, you must identify: The parent table (Customers) and the child table (Orders). The necessary join condition. (CustomerID in Orders is a child key that refers to the CustomerID parent key in the Customers table.) This information is used in generating the proper hierarchy. (For each parent element, the related child elements appear as subelements.) To provide the table names and the necessary join information, the following attributes are specified on the sql:relationship annotation:

- 'name' specifies the unique name of the relationship;
- 'parent' specifies the parent relation (table). This is an optional attribute; if the attribute is not specified, the parent table name is obtained from information in the child hierarchy in the document. If the schema specifies two parent-child hierarchies that use the same <sql:relationship> but different parent elements, you do not specify the parent attribute in <sql:relationship>. This information is obtained from the hierarchy in the schema.
- 'parent-key' specifies the parent key of the parent. If the parent key is composed of multiple columns, values are specified with a space between them. There is a positional mapping between the values that are specified for the multicolumn key and for the corresponding child key.
- 'child' specifies the child relation (table).
- 'child-key' specifies the child key in the child referring to parent-key in parent. If the child key is composed of multiple attributes (columns), the child-key values are specified with a space between them. There is a positional mapping between the values that are specified for the multicolumn key and for the corresponding parent key.

These attributes are valid only with the <sql:relationship> element.

## Example. Specifying the sql:relationship annotation on an element.

The following annotated XSD schema includes 'Customer' and 'Order' elements. The 'Order' element is a subelement of the 'Customer' element. In the schema, the sql:relationship annotation is specified on the 'Order' subelement. The relationship itself is defined in the 'appinfo' element. The 'relationship' element identifies CustomerID in the Orders table as a foreign key that refers to the CustomerID primary key in the Customers table. Therefore, orders that belong to a customer appear as a subelement of that 'Customer' element.

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
    <xsd:annotation>
      <xsd:appinfo>
        <sql:relationship name="CustOrders"
              parent="Demo.demo.Customers"
              parent-key="CustomerID"
              child="Demo.demo.Orders"
              child-key="CustomerID" />
      </xsd:appinfo>
    </xsd:annotation>

      <xsd:element name="Customer" sql:relation="Demo.demo.Customers" type="CustomerType" />
       <xsd:complexType name="CustomerType" >
         <xsd:sequence>
            <xsd:element name="Order"
                         sql:relation="Demo.demo.Orders"
                        sql:relationship="CustOrders" >
               <xsd:complexType>
                  <xsd:attribute name="OrderID" type="xsd:integer" />
                  <xsd:attribute name="CustomerID" type="xsd:string" />
               </xsd:complexType>
            </xsd:element>
         </xsd:sequence>
            <xsd:attribute name="CustomerID"   type="xsd:string" />
            <xsd:attribute name="ContactName"  type="xsd:string" />
        </xsd:complexType>
    </xsd:schema>

Let the schema is written to the file 'CustOr_constant.xsd', then after loading this file by `xml_load_mapping_schema_decl` function, the first example will produce a result for the XPath query:

    XPATH [__view 'Customer_Order'] /Customer[@CustomerID="QUEEN"];

the second example will produce a result for the XQuery query:

    select xquery_eval('<doc>{for $r in xmlview("Customer_Order")/*[@CustomerID="QUEEN"] return $r}</doc>', xtree_doc('<q/>'))