Database design / normalization structure needs to contain ANDs, ORs, optional elements and their relationships

后端 未结 5 972
轮回少年
轮回少年 2021-02-06 07:31

I want to store the details of college courses in a (MySql) database but I\'m not sure how to maintain the relationship between modules and selections.

Basically, a cour

5条回答
  •  醉酒成梦
    2021-02-06 08:25

    You can create a recursive table structure here, wherein Options reference their parent options.

    • The "main" options can then be identified by querying this table for all options with "null" parents.

    • The "and-or" relationships can be implemented by a separate "option-set" table, where the primary key is to an "option". The option-set table's with null self-references are the "root" point for defining a course's options. From that point, you will select option-set records with parent = root. This will be the first "level" of options. Some will be mandatory, some won't. To express that, you will have to have a boolean attribute on the option-set table as a flag. Thus each option-set is defined in terms of smaller option-sets. Of course, ultimately, once you get to the bottom, your option-set's will define an actual class at some point.

    I would suggest that this can much more effectively be modelled in JSON or XML, since those data structures support hierarchies in a much more expressive manner.

提交回复
热议问题