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.
The link table approach seems fine, except that you might want a 'relationship type' so that you know WHY they are related.
For example, the relation between Raleigh and North Carolina is not the same as a relation between Raleigh and Durham. Additionally, you may want to know who is the 'parent' in the relationship, in case you were driving conditional drop-downs. (i.e. You select a State, you get to see the cities that are in the state).
Depending on the complexity of your requirements, the simple setup you have right now may not be sufficient. If you simply need to show that two records are related in some way, the link table should be sufficient.
Based on your updated schema this query should work:
where :entity_id is bound to the entity you are querying
I think the structure you have suggested is fine.
To get the related records do something like
Hope that helps.
My advice is that your intial table design is bad. Do not store different types of things in the same table. (First rule of database design, right up there with do not store multiple pieces of information in the same field). This is much harder to query and will cause significant performance problems down the road. Plus it would be a problem entering the data into the realtionship table - how do you know what entities would need to be realted when you do a new entry? It would be much better to design properly relational tables. Entity tables are almost always a bad idea. I see no reason at all from the example to have this type of information in one table. Frankly I'd have a university table and a related address table. It would easy to query and perform far better.
I can think of a few ways.
A single pass with a CASE:
Or two filtered queries UNIONed thus: