0

This question already has an answer here:

I am making a website that has multiplayer games like chess, poker, etc.

Users will be able to add friends. I was thinking about how could I make this relationship in the database, and I thought of two methods:

1 - Using a row for every friendship e.g

id   |   friend1   |   friend2

2 - Using only a row per user, adding all friends in a string separated by comma (in a text field maybe?) e.g

|id   |   user    |   friends                |
----------------------------------------------
|1    |    rellfy |   user1,user2,user3,user4|

What would be the best method to use? Using a string only, and parsing it later through the application, or querying for friends directly?

marked as duplicate by Michael Green, Community Mar 26 '16 at 11:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

First approach is the SQL approach. You need to make modification though. The relation that you are describing is called many to many - each player can have each other player as friend. To achieve this you need a separate table which connects player with his friends (you just need 2 IDs):

|Player_ID  | Friend_ID|
------------------------
|  1        |  2       |
|  1        |  5       |

This is the better approach if you need make a lot of queries on Player and list of his friends (this will give speed of queries).

If you just need list of names, the other approach is also possible.

So you have to think and decide which is better approach for your needs.

  • Thanks for the answer! I think I'm going to use the first one, since I would not need to change the row of the user who accepts a friend invite, for example. And I also feel like it's the right way to do it. – user90568 Mar 26 '16 at 9:30