简体   繁体   English

SQL DDL:创建递归表(MySQL)

[英]SQL DDL: Creating a recursive table (MySQL)

I am trying to create a recursive entity named Subject. 我正在尝试创建一个名为Subject的递归实体。 A subject can be a prerequisite of another subject, that is why it is recursive. 一个主题可以是另一个主题的先决条件,这就是为什么它是递归的。 Here is what I have so far: 这是我到目前为止的内容:

CREATE TABLE subject(
    subject_code CHAR(7),
    subject_desc VARCHAR(255) NOT NULL,
    no_of_units TINYINT UNSIGNED NOT NULL CHECK (no_of_units > 0 AND no_of_units < 13),
    prerequisite CHAR(7),
    PRIMARY KEY (subject_code),
    FOREIGN KEY (prerequisite) REFERENCES subject(subject_code)
)ENGINE=INNODB;

Is the above the correct way to create a recursive table? 以上是创建递归表的正确方法吗?

Yes. 是。

All you really need is a "Father" column to relate with the parent subject. 您真正需要的是与父主题相关的“父”列。 Your prerequisite column does the trick. 您的前提条件列可以解决问题。

You can check an example here . 您可以在此处查看示例。 The idea is always the same. 这个想法总是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM