I have a database that has a table of Ingredients I and a table of Recipes R. The two tables have a many-to-many relationship, as one recipes uses many ingredients and one ingredient is used in many recipes. I have a third cross-reference table that uses the cross-reference validation pattern to enforce my many-to-many relationship, and is done using string foreign keys (instead of integers).
Assuming I have a collection of ingredients C outside of my database, how can I query Recipe table R for every recipe that can be made using ONLY the list of ingredients supplied in C?
Other things to consider
1) Speed will (of course) be a concern eventually, but correctness is what I’m stuck on at the moment.
2) The collection of ingredients C might be very large (~100 ingredients).
Any answers or even just pointers in the right direction would be greatly appreciated.
Thanks,
Alec
One way is to write:
That is: start with
C. Select all recipe–ingredient cross-references where the ingredient is not inC. This gives you the set of all recipes that cannot be made using only ingredients inC. Then, select all recipes that aren’t in that set.