I have a table called Modules with columns (M_Id, Name, Version, Descr). I have another table detailing dependencies for each modules, i.e. a module may be dependent upon another. For this I use a table called Mod_depends which has the composite key (M_Id_1, Depends).
Example entries:
Module
1 | Chameleon | 2.9.2 | HTML/XML template engine
2 | Unittest2 | 1.0.0 | unittest module
3 | OrderedDict | 1.0.1 | Ordered dictionary
Mod_depends
1 | 2
1 | 3
I’m looking for a MySQL query that, for a given id, can return entries from the Modules table representing the dependencies. In the example above, supplying Id = 1 would return:
2 | Unittest2 | 1.0.0 | unittest module
3 | OrderedDict | 1.0.1 | Ordered dictionary
Does this require a join or some subquery magic or both?
Is there a way I can add constraints such that additions to the Mod_depends table is only possible when an entry exists in then Modules table?
Cheers
Cheers
For the simplest case (first-order dependencies only) it’s something like this:
… but it’d be a really non-trivial task to find more deep dependencies, as it’ll become a query for tree-like structure. So you can either use this discussion for ideas, or reorganize your DB from Adjacency List to Closure Table. Perhaps you’d want to check this wonderful answer as well. )