If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > MySQL - users and friends

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-03-07, 08:41
snarez snarez is offline
Registered User
 
Join Date: May 2007
Posts: 2
MySQL - users and friends

I'm totaly lost; I read a lot of tutorials and forums, and I'm still not sure if my design is OK, so I decided to ask here. I'm sure it's not big deal for somebody with more expirience than me (approx 0 ).

What troubles me is this: I have users, and users can have friends. Any user can have any number of friends, but you first must agree to be somebody's friend. Users are neatly placed in a table and have their UserID (primary key) + usual stuff (email, SHA1 of password etc). So how do I define user's friends and requests for friendships? My idea was that each user gets a table, with name UserID_friends and 2 fields: friend ID (FK) and status, where status codes determine their relationship (eg. 1 - "I requested friendship", 2 - "he is my friend", 4 - "he requested friendship", 8 - "I am his friend"; using bitwise OR I can get any combination of these). Name of that table is stored in users table under field "frends_table".

Is this OK? If not, please give me advice, or point me to an article, I want this database to be as fast as possible, as I believe it might get a lot traffic if I'm lucky enough that my application gets enough traffic.

Thanks a lot for your help.
Reply With Quote
  #2 (permalink)  
Old 05-03-07, 09:49
msmeland msmeland is offline
Registered User
 
Join Date: Mar 2004
Location: Between Chicago and Milwaukee
Posts: 187
Each user shouldn't have his/her own friends table because the number of tables would multiply very quickly that way. You could make one table called friends and then have PK (AutoNumber) Owner (UserID) Friend (UserID) and then a status code(the 1,2,4 or 8 etc.), and a datestamp. That way, if you decide to add a status code, or change a status code you can do a global change on one table instead of having to write the code for each individual table that was user coded. You are going to also want to add an ignore table with the same kind of coding, so that it shuts certain users out of all the specific UserID content. 'Net people are funny, and sometimes need the ability to shut out stalkers and irritations.

If your app takes off, you are not going to want to have so many tables to clean up. Keeping it as simple as possible with good database engineering is a must.

If you want to take a look at a good MySQL driven 'user type' system that has good database structure, I'd take a look at simplemachines.org. Their forum software (open source) is really well designed.
__________________
"An adventure is only an inconvenience rightly understood; an inconvenience is only an adventure wrongly considered." ~G.K. Chesterton
Reply With Quote
  #3 (permalink)  
Old 05-03-07, 10:26
snarez snarez is offline
Registered User
 
Join Date: May 2007
Posts: 2
Thank you for your answer and the link. I can't believe I didn't think of it myself..

Seeing how I had a completely wrong idea, I just have to check if something else has reasonable concept:

User A wants to send user B a message. This is what script does:

1) it checks if user A is allowed to send messages to user B
2) if he/she is, script adds message to messages table ( MessageID(PK), SenderID, RecipientID, message_body, timestamp )
3) when user B logs on, script checks if there are messages for him/her waiting, and displays message from user A
Reply With Quote
  #4 (permalink)  
Old 05-03-07, 11:11
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Logic looks fine to me. After you write the sproc to implement it, post it here for review.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On