I have a SQL table like so:
Update: I'm changing the example table as the existing hierarchical nature of the original data (State, Cities, Schools) is overshadowing the fact that a simple relationship is needed between the items.
entities
id name
1 Apple
2 Orange
3 Banana
4 Carrot
5 Mushroom
I want to define two-way relationships between these entities so a user viewing one entity can see a list of all related entities.
The relationships are defined by an end user.
What is the best way to represent these relationships in the database and subsequently query and update them?
One way as I see it...
My instinct says a relationship table like so:
entity_entity
entity_id_a entity_id_b
1 2
5 1
4 1
5 4
1 3
That being the case, given a supplied entity_id of 4, how would one get all related records, which would be 1 and 5?
Likewise a query of entity_id = 1 should return 2, 3, 4, and 5.
Thanks for your time and let me know if I can clarify the question at all.