I’m looking forward to a logic to store friends list in a database. I’mthinking of adding things in an array with userids
example: user a’s array in friends table would contain userid arrays of friends like 1,2,4,6,77,44 etc
I want to know whether this will be an efficient way of doing this. If not, what logic should be ideally implemented for large community?
You likely need a separate many-to-many join table. To achieve this, if both the user and their friends reside in the same user table. The table could look like this:
user_id – id of user the friend lookup is being done for, is foreign key to user_id field in user table
friend_id – id of friend associated with user, also is a foreign key to user_id field in user table
You would have a compound primary key across both fields, ensuring that each user_id to friend_id combination is unique.
This would be sample CREATE TABLE statement:
And sample data may look like this:
Then say you wanted to do a lookup of all the user table data for the friends associated with a particular user (i.e. the logged in user). You could query that data like this: