HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

How should I design a relationship table for friendship?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
howdesignforshouldfriendshiptablerelationship

Problem

If A is a friend of B, then should I store both values AB and BA, or one is enough? What are the advantages and disadvantages of both methods.

Here is my observation:

  • If I keep both then I have to update both when receive a request from a friend.



  • If I don't keep both, then I found it difficult when having to do multiple JOIN with this table.



Currently, I keep the relationship one way.



So what should I do in this case? Any advice?

Solution

I would store AB and BA. A friendship is really a two-way relationship, each entity is linked to another. Even though intuitively we think of the "friendship" as one link between two people, from a relational point of view it is more like "A has a friend B" and "B has a friend A". Two relationships, two records.

Context

StackExchange Database Administrators Q#10199, answer score: 34

Revisions (0)

No revisions yet.