I’m currently trying to design the database for a simple social site. I’m using MySQL for my database. Currently, I only have a users table. Basically, I want users to be able to add friends and be able to list their friends. I’m thinking of creating a friends table, something like:
friends table
user_id
friend_id
And whenever someone adds a friend, it will create an entry. I’m not exactly sure if I’m on the right direction. Another question, is there a way to retrieve all 2nd degree friends?(friends of friends) Any help would be appreciated. Thanks a lot in advance!
Yes, you’re on the right path.
You can retrieve 2nd degree friends by joining the friends table to itself
One thing to consider is commutativity – if A is a friend of B, is B a friend of A ?