Jump to content

Best way to store "friends" in a database?


ttocskcaj

Recommended Posts

I'm working on  a Social Network from scratch. I'm stuck on how to store friend relationships between users?

On one topic here, I found an idea.

Have a table with 3 columns; id,user_1,user_2

each relationship would have a new row in the database.

Would this be the best way to do this? I can imagine the table would get quite big.

If each user has 100 friends and there's 10000 users that's 1000000 rows. etc

Link to comment
Share on other sites

I can see more advantages to having a row id than not having one.  It's auto incremented, what can it hurt?

 

The compound user_1 and user_2 is unique why add more unique values to something that's already unique? Adding the extra ID shows a lack of DB knowledge as you can easily reference a compound key from another table:

 

CREATE TABLE compound (
  user ..
  friend ..
  ..
  FOREIGN KEY (user, friend) REFERENCES friends (user, friend)
);

 

Don't add an ID just for the sake of having an ID in each table. On a side note ID's are not by definition INT UNSIGNED NOT NULL AUTO_INCREMENT they could aswell be a VARCHAR, just so you know ;)

Link to comment
Share on other sites

  • 2 weeks later...

I can see more advantages to having a row id than not having one.  It's auto incremented, what can it hurt?

 

The compound user_1 and user_2 is unique why add more unique values to something that's already unique? Adding the extra ID shows a lack of DB knowledge as you can easily reference a compound key from another table:

 

 

 

Good insights! I always add a primary key, thinking that's just the way it's done. Obviously not so. I gain another gram of knowledge!

Link to comment
Share on other sites

I can see more advantages to having a row id than not having one.  It's auto incremented, what can it hurt?

 

The compound user_1 and user_2 is unique why add more unique values to something that's already unique? Adding the extra ID shows a lack of DB knowledge as you can easily reference a compound key from another table:

 

 

 

Good insights! I always add a primary key, thinking that's just the way it's done. Obviously not so. I gain another gram of knowledge!

 

Glad I could help ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.