如何设置外键?

发布于 2024-12-01 16:45:22 字数 2887 浏览 1 评论 0原文

我正在做一个使用 MDB2 实现的项目。

我想在 xml 模式文件上设置外键。 只有 3 个表:学生、课程和注册,其中注册作为桥梁实体。

这是我的注册 xml 文件。

<database>
<name>mdb2_schema_demo</name>
<charset>utf8</charset>
<description>A database to demo MDB2 schema sync</description>

<table>
    <name>enrollment</name>
    <declaration>
        <field>
            <name>id</name>
            <type>integer</type>
            <notnull>1</notnull>
            <autoincrement>1</autoincrement>
            <unsigned>1</unsigned>
            <length>8</length>
            <description>A unique running number to identify enrollment</description>
        </field>

        <field>
            <name>student_id</name>
            <type>integer</type>
            <notnull>0</notnull>
            <unsigned>1</unsigned>
            <length>8</length>
            <description>Foreign key refer to student</description>
        </field>

        <field>
            <name>course_id</name>
            <type>integer</type>
            <notnull>0</notnull>
            <unsigned>1</unsigned>
            <length>8</length>
            <description>Foreign key refer to course</description>
        </field>

        <index>
            <name>pk_enrollment</name>
            <unique>1</unique>
            <primary>1</primary>

            <field>
                <name>id</name>
                <sorting>ascending</sorting>
            </field>
        </index>

        <foreign>
            <name>fk_student_enrollment</name>
            <field>student_id</field>
            <references>
                <table>student</table>
                <field>id</field>
            </references>
            <ondelete>restrict</ondelete>
            <onupdate>cascade</onupdate>
        </foreign>
        <foreign>
            <name>fk_course_enrollment</name>
            <field>course_id</field>
            <references>
                <table>course</table>
                <field>id</field>
            </references>
            <ondelete>restrict</ondelete>
            <onupdate>cascade</onupdate>
        </foreign>
    </declaration>
</table>

这是错误。

消息:MDB2_Schema 错误:模式验证错误

解析器错误:表“enrollment”的外键 fk_student_enrollment”引用的表“student”不存在 - 无错误 - 字节:2244;行:70;列:12

I am doing a project which is using MDB2 implementation.

I wanna to set a foreign key on xml schema file.
There are only 3 tables: student, course, and enrollment where enrollment is serve as a bridge entity.

Here is my xml file for enrollment.

<database>
<name>mdb2_schema_demo</name>
<charset>utf8</charset>
<description>A database to demo MDB2 schema sync</description>

<table>
    <name>enrollment</name>
    <declaration>
        <field>
            <name>id</name>
            <type>integer</type>
            <notnull>1</notnull>
            <autoincrement>1</autoincrement>
            <unsigned>1</unsigned>
            <length>8</length>
            <description>A unique running number to identify enrollment</description>
        </field>

        <field>
            <name>student_id</name>
            <type>integer</type>
            <notnull>0</notnull>
            <unsigned>1</unsigned>
            <length>8</length>
            <description>Foreign key refer to student</description>
        </field>

        <field>
            <name>course_id</name>
            <type>integer</type>
            <notnull>0</notnull>
            <unsigned>1</unsigned>
            <length>8</length>
            <description>Foreign key refer to course</description>
        </field>

        <index>
            <name>pk_enrollment</name>
            <unique>1</unique>
            <primary>1</primary>

            <field>
                <name>id</name>
                <sorting>ascending</sorting>
            </field>
        </index>

        <foreign>
            <name>fk_student_enrollment</name>
            <field>student_id</field>
            <references>
                <table>student</table>
                <field>id</field>
            </references>
            <ondelete>restrict</ondelete>
            <onupdate>cascade</onupdate>
        </foreign>
        <foreign>
            <name>fk_course_enrollment</name>
            <field>course_id</field>
            <references>
                <table>course</table>
                <field>id</field>
            </references>
            <ondelete>restrict</ondelete>
            <onupdate>cascade</onupdate>
        </foreign>
    </declaration>
</table>

Here is the error.

message: MDB2_Schema Error: schema validation error

Parser error: referenced table "student" of foreign key fk_student_enrollment" of table "enrollment" does not exist - No error - Byte: 2244; Line: 70; Col: 12

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文