How to Model Course Prerequisites for a University in MySQL

database-designMySQL

I am trying to model a courses structure and dependencies of a university.

For example, in the Computer Engineering department you need to do Data Structures before doing Artificial Intelligence. But you also need to finish Introduction to Logic OR Mathematical Logic and Proofs.

I was thinking about using a table for the courses (Id, Course Number, Course Name, Hours, Professor) and another table for the dependencies.

There lies the problem: How to model this dependencies/prerequisites? The only -viable- solution that I came up with was making this as a string, written in plain English; but that clearly doesn't seem to be a decent solution and I also believe that it violates the atomicity principle.

Notice that the prerequisites of Artificial Intelligence are of the form IA <- DS && (IL || MLP), but other courses will have a different structure like Calculus 2, that requires analytical geometry and calculus 1 done before: C2 <- AG && C1.

I don't know if this is relevant or not, but I will use MySQL.

Best Answer

You have some Courses:

COURSES
-------
course_id
course_number
course_name

You need to combine one or more of these Courses with a logical operator:

COMBINATIONS
------------
combination_id
logical_operator (AND, OR or NOT)

COMBINATION_COURSES
-------------------
combination_id
course_id

You may also need to combine some of these combinations (so changing the COMBINATION_COURSES table):

COMBINATION_COURSES
-------------------
combination_id
course_id
sub_combination_id

where one and only one of course_id and sub_combination_id is not null.

A Course may have one of the combinations as a prerequisite (so changing the COMBINATIONS table):

COURSES
-------
course_id
course_number
course_name
prerequisite_combination_id

EDIT

So, given your original examples your tables would be populated:

COURSES
=======
course_id course_name                    prerequisite_combination_id
--------- ------------------------------ ---------------------------
        1 Artificial Intelligence                                 12
        2 Data Structures
        3 Introduction to Logic
        4 Mathematical Logic and Proofs 
        5 Calculus 1
        6 Calculus 2                                              13
        7 Analytical Geometry

COMBINATIONS
============
combination_id logical_operator
-------------- ----------------
            11 OR
            12 AND
            13 AND

COMBINATION_COURSES
===================
combination_id course_id sub_combination_id
-------------- --------- ------------------
            11         3
            11         4
            12         2
            12                           11
            13         7
            13         5

Course 1 - Artificial Intelligence has a prerequisite of the Combination 12. Combination 12 is Course 2 - Data Structures AND Combination 11. Combination 11 is Course 3 - Introduction to Logic OR Course 4 - Mathematical Logic and Proofs.

Hence, the prerequisite of Course 1 - Artificial Intelligence is Course 2 - Data Structures AND (Course 3 - Introduction to Logic OR Course 4 - Mathematical Logic and Proofs).

This structure should allow you to store any prerequisite that is the any logical combination of any number of courses.