MasterMastic
MasterMastic

Reputation: 21286

Associate user with multiple other users

I'm connecting my C++ program to a MySQL server, and I'm having a bit of a hard time.

Each user has a contacts/friends list which (of course) holds other users. I'm not sure how to actually associate a user with other users in the database. what datatype could work for such a task?

The only thing I could think of is maybe to have a text field with numbers separated by spaces and parse it each time the user connects. but for some reason it sounds awfully wrong to me, so I'm not sure.

What could you recommend? Thank you.

Upvotes: 2

Views: 88

Answers (1)

Mark Byers
Mark Byers

Reputation: 838376

The only thing I could think of is maybe to have a text field with numbers separated by spaces and parse it each time the user connects. but for some reason it sounds awfully wrong to me

That sounds like a bad idea to me too.

You want a many-to-many relationship:

contact
user    friend
1       2
1       3
1       5
2       4
3       1

Note that in a contact list you may want to store additional attributes for each contact list entry. For example the user may wish to change the name that is displayed in their own contact list. For example, your table might look like this if user 1 wants to see user 3's name to be displayed as "foo".

contact
user    friend   displayName
1       2        NULL
1       3        'foo'
1       5        NULL
2       4        NULL
3       1        NULL

Here NULL means that the user has not specified a preferred display name for their contact, so the contact's name should be shown.

Upvotes: 1

Related Questions