Messaging system database schema
Asked Answered
S

2

4

I'm trying to implement a messaging system in PHP and MySQL but I'm having some trouble deciding on how I should do the tables and queries.

What would be the best approach for a system that allows for multiple participants? I'm thinking I'd probably need 3 tables (aside from an users table).

Something like

Conversation
------------
id

Messages
--------
id
conversation_id
from_id
subject
message
from_timestamp

Participants
------------
conversation_id
user_id
last_read_timestamp

The way it is setup I'd have to check for read messages by the timestamp instead of ticking off each message. I'd also be able to add participants at any time.

What do you guys think?

Thanks in advance.

Spermaceti answered 17/5, 2011 at 15:17 Comment(3)
@ChrisWalton - The 'from_id' would be the user_id of the sender of the message. Many participants may belong to a conversation but a message may only belong to one participant (or ex participant - still thinking on how to do this). All participants of a conversation may see messages related to it. CheersSpermaceti
I think from_id is the user_id of the author. If I understood correctly the 'participants' table links conversations to the users table (which is not shown here) and stores the last time the user read this conversation.Captivity
Would love to see how you dealt with the one participant issue! This is one of the better table schemas I've seen.Jamshid
C
4

I don't see much to complain about :) I'd probably cache the last modification date on the conversations table so that a list of "unread" conversations can be displayed quickly. This date would be updated each time a message is posted on the conversation.

Captivity answered 17/5, 2011 at 15:38 Comment(0)
B
1

I would have put the subject field on the conversation table. Putting it on every single message looks redundant. Also, I'd save the creation time and the author user id of the conversation in its table.

Bonney answered 23/10, 2014 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.