I am in dilemma, do not know which one is better. (I am using php and mysql btw)
Lets say I am developing a social community website. A user can add as many friends as he wants. In my database, I have a users table which store user’s details include user’s friends. Should I store the user’s friends in:
Strings => john;bryan;sam;paul;…;
or
Integers => 1;21;50;1779;30;…;
String means store their username. Integer means store their user id.
Which one is better?
If string, it would be a very, very large string(imagine the users have 400 friends, and some usernames are extreme long)
If id, how to list friends on user profile page? Getting list of IDs in one long string (separated by semicolon), use explode function to make it into array. Then using loops to display?? Isn’t troublesome? Is there any other way? If yes, please show some sample code..
OR normally, how would u guys to store the ‘friends’ list in the user table?
Don’t store them as a semi-colon delimited anything. Store them as user IDs in a lookup table. Suppose the user is #2. You might have:
Then to display the list of friends, you query this table restricting it by SourceUserID = 2 and join against the user table to get the names. For example:
(Or whatever your parameterised query format is; populate the parameter with the ID of the user whose page you’re displaying.)