Sample Header Ad - 728x90

How to represent arbitrary logical expressions in a many to many relationship?

1 vote
0 answers
60 views
I have a Course entity in my database and I want to represent its prerequisites. - A Course can have many prerequisites. - A Course can be a prerequisite for many other Courses - Clearly, this would be represented as a Many to Many relationship between the Course entity and itself. - But I also need to represent more complicated prerequisites that involve logical expressions. For example course1 might have the following prerequisites: course2 AND (course3 OR course4). I need to keep track of the expression and the order of the operands as well. One approach I thought about is to treat the expression as a product of sums. I would have a PrerequisiteGroup entity which represents a group of courses ORed together and then a Prerequisite many-to-many relationship to represent a group of PrerequisiteGroups ANDed together for every course. I feel this design might be too complicated when it comes to querying but I think it can represent any logical expression that only has AND and OR operators. Please let me know if there are better designs. Also, let me know if a relational database isn't the best to represent this, and if a graph-based database would be simpler.
Asked by devordem (11 rep)
Feb 25, 2023, 10:54 AM
Last activity: Jul 1, 2024, 12:31 PM