after searching up and down, reading all possible articles and tutorials, I grasped the basics of the concept, but still cannot do it, and so as many others as I can see.
Can someone please post 100% practical and dummy proof guide to creation and most basic usage of MySQL many to many relationship, I’m so sure many will benefit from it.
What I have is a table that has a number of items, say I1, I2, I3…
I have another table, with a number of properties, say P1, P2, P3…
For each of the items, each of the properties may hold false or true, for example
I1 has properties P2 and P3
I2 has properties P1, P2 and P3
I3 has properties P1
…
So how to go about creating the relationship? (please give code if possible)
And once created, how to
- insert properties for some item I
- read which properties apply to some existing item I
Thanks in advance
Step 1 – Setup tables:
You should have a table structure like below (*’s are primary keys):
Step 2 – Set foreign key relationships:
Both columns in the ItemProperties table are foreign keys to their corresponding table (ItemId to Item table, PropertyId to Properties table)
Step 3 – Code:
To associate the properties with PropertyIds 35 and 44 to the Item with ID 111 you would run the following:
To select all properties associated with an item you would run the following: