Multiple values in a column
I need some advice regarding multiple records. I have a table with fields username
,message
and 开发者_如何学运维message_to
. The scenario could be sending the same message to multiple users in one 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?
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 auser_id
value, and the user'sname
. - In the
Messages
table, store amessage_id
value and the messagetext
. - In the
MessagesSentToUsers
table create on record for each time a message is sent. This record should contain only theuser_id
for the user who got the message, and amessage_id
pointing to the message text they received.
Like the following:
| User | | Message | | MessageSentToUsers |
|---------|------| |------------|------| |----------|------------|
| user_id | name | | message_id | text | | user_id | message_id |
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).
精彩评论