Jump to content

Archived

This topic is now archived and is closed to further replies.

boonamera

how to structure database to handle user's friends

Recommended Posts

What would be the best way to structure a database so that users can add friends.

Right now i have a simple user table which includes
userid
username
password
emai

thanks

edit:
ok more info:
let's say the following user
1
boonamera

wants to add as a friend
2
booya

how would you record this relationship?
I'm asking what field would you put in the friends table if that's how you suggest doing it.

Share this post


Link to post
Share on other sites
Can you post more info? I hardly know what you mean  ???

Share this post


Link to post
Share on other sites
What you are describing is a "many to many" relationship, i.e. each member can have a number of other members as friends, each of whom can also have many friends, etc. So you need a new table to break this up into a "one to many" relationship.

So I would suggest a new table which records:

userid
friendid

so that userid corresponds to the userid in your user table, and friendid also corresponds to the userid in the user table, of one of his friends. That way you could add the same userid many times, each with a different friendid, that way each member can have as many different friends as they like and you have not wasted memory with fields for those members who do not have friends.

Share this post


Link to post
Share on other sites
That's definitely the right approach... such a lookup is the best solution.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.