foreign-keys - xml 模式中的主键和外键

标签 foreign-keys xsd primary-key

我正在尝试在下面的 XSD 架构文件中插入主键和外键:-

主键是 StudentID,外键是 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>

但是上面的代码在我的设置中不起作用,请帮助我跟踪问题,

最佳答案

一般来说,您需要在问题中添加更多细节......所以这应该足以了解您可能还需要定义什么以及如何定义。我将通过说明假定的学生/地址关系来解决理解如何定义主键/外键所需的内容。

首先,您需要定义这些约束成立的上下文。在我修改后的 XSD 中,我称它为“世界”。

<?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>

然后像这样的 XML 会通过或失败,具体取决于您对 StudentID 和 AddressID 字段中的值所做的操作。
<?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>

要完成您的解决方案,您需要定义 类(class) 等级 在您的“世界”中的“实体”,定义 xs:key对于每个,类似于 学生 /*Address*,然后给需要的实体添加CourseID和GradeID属性,最后定义keyref,如上,为实体等级 实体类(class) .

关于foreign-keys - xml 模式中的主键和外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15770948/

相关文章:

c++ - XSD : value not in enumeration

java - 向 Web 服务 Opengts 发送 XML 请求以从 Android 应用程序登录时出错

MySQL:删除主键会删除唯一约束和/或索引吗?

oracle - 使用 Oracle Sql Developer 生成 DDL 以包含外键

mysql - 具有外键约束的删除顺序,

基于条件的 SQL 查询

grails - 如何使用grails(create/list.gsp)从自动生成的 View 中插入/查看主键(电子邮件)?

mysql - 如何在mysql中插入外键

c# - 如何将 XSD.exe 与 attributeGroup ref 一起使用

sql - 为什么一个SQL表有多个主键?