Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 370
RS7 Avatar asked Oct 29 '25 09:10

RS7


2 Answers

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.

like image 200
Jad Avatar answered Oct 30 '25 23:10

Jad


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.

like image 26
Miguel El Merendero Avatar answered Oct 31 '25 00:10

Miguel El Merendero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!