MySQL – How to Design a Relationship Table for Friendship

MySQLrelational-theory

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.

enter image description here

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

Best Answer

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.