which one is best way to store multiple values for a single field? Comma separated or Separate Table which one is best way to store multiple values for a single field? Comma separated or Separate Table database database

which one is best way to store multiple values for a single field? Comma separated or Separate Table


You need just two tables - one to list your users, and one to list who each user is following.

The resulting tables would be like your second proposal, except that the followers table is unnecessary because all of the required data is already in the following table - it's just keyed from the second column instead of the first. That table will need an index on both columns.

The following table whould have one row per relationship per direction. If the users are following each other, you would put two entries in the following table.

CREATE TABLE following (    userid ... NOT NULL,    following_id ... NOT NULL);CREATE INDEX idx_user ON following(userid);CREATE INDEX idx_following on following(following_id);CREATE UNIQUE INDEX idx_both ON following (userid, following_id); // prevents duplicates

To find the IDs that a particular user is following:

SELECT following_id FROM following WHERE userid = ?

or to find that user's followers:

SELECT userid FROM following WHERE following_id = ?

Use appropriate JOIN clauses if required to expand those queries to return the users' names.


None of the above. One row per follower. Normalize your data and using it will be easy. Make it an abstract mess like you're proposing and you're life will get tougher and tougher as your application grows.