Jump to content


Photo

how to structure database to handle user's friends


  • Please log in to reply
3 replies to this topic

#1 boonamera

boonamera
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 20 July 2006 - 11:13 PM

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.



#2 Kurt

Kurt
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationNew York

Posted 20 July 2006 - 11:55 PM

Can you post more info? I hardly know what you mean  ???

#3 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 21 July 2006 - 04:31 PM

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.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 21 July 2006 - 07:48 PM

That's definitely the right approach... such a lookup is the best solution.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users