Messaging system database schema

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x