Here’s a Item table that stores basic item information like name, price, color etc.
Now, I need to keep track of materials used in an item like say, cotton, gems, rhinestone, leather etc. One required feature is to identify the items that is made of one or more partiuclar materials. For example, given the materials cotton and rhinestone, I should be able to output a list of all the items that is made of cotton and rhinestone (among other possible materials).
So, what’s the best way to design the tables for this particular requirement taking performance into account?
You should have a table for item and one for material, and then use a junction table to define which items contain which materials and vice-versa.. Something like this would be best:
Item
Material
Item_Material
Then, if you want to get all the items with cotton in them, you’d write a query like:
Or for all materials making up an item: