Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primary and foreign keys in xml schema

I am trying to insert primary and foreign keys in XSD schema file below :-

Primary key is StudentID and Foreign Keys are courseID, AddressID, GradeID.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Student">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Title" type="xs:string"/>
      <xs:element name="FirstName" type="xs:string"/>
      <xs:element name="LastName" type="xs:string"/>
      <xs:element name="Dateborn"  type="xs:date"/>
      <xs:element name="Gender"  type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

However above code is not working in my setup please help me in tracing the issue,

like image 421
user2107388 Avatar asked Apr 02 '13 17:04

user2107388


People also ask

What is primary key and foreign key?

A primary key is used to ensure data in the specific column is unique. A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. 2. It uniquely identifies a record in the relational database table.

Which tag is used to define a foreign key in XML schema?

The xpath attribute in the selector tag is used for specifying which section the foreign key is for.

What is difference between primary key and foreign key constraints?

A primary key constraint in the relational database acts as a unique identifier for every row in the table. In contrast, a foreign key constraint establishes a relationship between two different tables to uniquely identify a row of the same table or another table.

What is a key in XML?

The key element specifies an attribute or element value as a key (unique, non-nullable, and always present) within the containing element in an instance document.


1 Answers

In general you would need to put more details in a question... So this should be enough to understand what else you might need to define, and how. I'll tackle what's needed to understand how to define a primary key/foreign key by illustrating an assumed student/address relationship.

First you need to define a context where these constraints hold true. In my modified XSD, I call it the "World".

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="World">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Student" maxOccurs="unbounded"/>   
                <xs:element ref="Address" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>          
        </xs:complexType>
        <xs:key name="PKStudents">
            <xs:selector xpath="Student/StudentID"/>
            <xs:field xpath="."/>
        </xs:key>
        <xs:key name="PKAddresses">
            <xs:selector xpath="Address/AddressID"/>
            <xs:field xpath="."/>
        </xs:key>
        <xs:keyref name="FKStudentToAddress" refer="PKAddresses">
            <xs:selector xpath="Student/AddressID"/>
            <xs:field xpath="."/>
        </xs:keyref>
    </xs:element>
    <xs:element name="Student">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Title" type="xs:string"/>
                <xs:element name="FirstName" type="xs:string"/>
                <xs:element name="LastName" type="xs:string"/>
                <xs:element name="Dateborn" type="xs:date"/>
                <xs:element name="Gender" type="xs:string"/>
                <xs:element name="StudentID" type="xs:string"/>
                <xs:element name="AddressID" type="xs:string" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Address">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="AddressID" type="xs:string"/>
                <xs:element name="Street" type="xs:string"/>
                <xs:element name="City" type="xs:string"/>
                <xs:element name="Province" type="xs:string" minOccurs="0"/>
                <xs:element name="Country" type="xs:date" minOccurs="0"/>
                <xs:element name="PostalCode" type="xs:string" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Then an XML like this would pass or fail, depending on what you do with the values in the StudentID and AddressID fields.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<World xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Student>
        <Title>Title1</Title>
        <FirstName>FirstName1</FirstName>
        <LastName>LastName1</LastName>
        <Dateborn>1900-01-01</Dateborn>
        <Gender>Gender1</Gender>
        <StudentID>StudentID1</StudentID>
        <AddressID>AddressID1</AddressID>
    </Student>
    <Student>
        <Title>Title1</Title>
        <FirstName>FirstName1</FirstName>
        <LastName>LastName1</LastName>
        <Dateborn>1900-01-01</Dateborn>
        <Gender>Gender1</Gender>
        <StudentID>StudentID2</StudentID>
        <AddressID>AddressID1</AddressID>
    </Student>
    <Address>
        <AddressID>AddressID1</AddressID>
        <Street>Street1</Street>
        <City>City1</City>
        <Province>Province1</Province>
        <Country>1900-01-01</Country>
        <PostalCode>PostalCode1</PostalCode>
    </Address>
    <Address>
        <AddressID>AddressID2</AddressID>
        <Street>Street1</Street>
        <City>City1</City>
        <Province>Province1</Province>
        <Country>1900-01-01</Country>
        <PostalCode>PostalCode1</PostalCode>
    </Address>
</World>

To finish your solution, you would need to define the Course and Grade "entities" in your "world", define the xs:key for each, similar to Student/*Address*, then add CourseID and GradeID attributes to the entities that need them, and finally, define the keyref, as above, for Entity to Grade and Entity to Course.

like image 167
Petru Gardea Avatar answered Oct 19 '22 16:10

Petru Gardea