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

后端 未结 5 837
轮回少年
轮回少年 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:08

    You can probably do something like this:

    TABLE course_definition (
        ID int,
        num_mandatory_sections int,
        mandatory_hours int,
        num_optional_modules int,
        optional_hours int,
    );
    
    TABLE modules (
        ID int,
        Description varchar(max),
        hours int,
        ....
    );
    
    TABLE course (
        Course_ID int FOREIGN KEY (course_definition.id),
        module_id int FOREIGN KEY (modules.id)
    );
    
    TABLE course_module_relationship (
         Course_ID int FOREIGN KEY (course_definition.id),
         module_ID int foreign key (modules.id),
         Requirement_flag ENUM ("MANDATORY", "OPTIONAL")
    );
    
    TABLE Student_to_course (
         Student_ID int,
         Course_ID int foreign key (course_definition.id)
    );
    
    TABLE Student_to_module (
         Student_ID int,
         Module_ID int FOREIGN KEY (module.id)
    );
    

    If you really need to be able to create group modules aka single module created from the multiple other modules then table module will need to have a flag field:

    group_module boolean
    

    and the following table should be added:

    TABLE module_groupings (
        group_module_ID int foreign key (module.id)
        dependant_module_id int foreign key (module.id)
    );
    

    This is more of pseudo code but you get the idea. The table course and course_module_relationship will have no keys and store your relationships as they can be many to many as I understand the problem. So basically the code that will read process the selections will have to check whether or not this meets the criteria for the course_definition.

    If the Mandatory section to Course is a 1-to-1 relationship you can separate your mandatory section into a separate table but you will have to analyze your data more thoroughly.

提交回复
热议问题