views:

38

answers:

1

Hi

I need some advice regarding multiple records. I have a table with fields username,*message* and message_to. the scenario could be of sending same message to multiple users in a go. What do you suggest? will it be efficient to save all recipients in a single column with comma separated values or I add multiple entries ?

Thanks

/A

+4  A: 

No, no, no, no, no.

This would be a capital violation of the relational database model.

Create three tables: Users, Messages, and MessagesSentToUsers.

In the Users table, store a user_id value, and the user's name.

In the Messages table, store a message_id value and the message text.

In the MessagesSentToUsers table create on record for each time a message is sent. This record should contain only the user_id for the user who got the message, and a message_id pointing to the message text they received.

In general, the id columns in each table will be of type integer. The most common case is to have the DBMS assign a value to each row (so you don't have to manage the numbers).

Larry Lustig
Agreed; sometimes denormalisation is appropriate. But this is not such a time.
Noon Silk
@silky: why? If you need to know "What messages did UserX receive?", this is reasonable. Since Adnan calls the recipients "users", they might already have their own table. If so, @Larry's solution only adds one simple table.
egrunin
Won't extra JOINs effect the performance?
Adnan
@Adnan: the negative effect on performance that JOIN will bring and that you seem to fear so much pales in comparison to the goodness of a nicely and properly designed database system. A tiny little bit of performance isn't worth tossing out the most fundamental database design principles. Don't overoptimize ahead of time!
marc_s
@marc_s: you're right. Later on I can apply caching techniques once it starts getting out of control. Thanks
Adnan