U
U
Uglik2016-02-01 21:13:07
Database design
Uglik, 2016-02-01 21:13:07

Database design. Correspondence between users?

Hello.
Prompt how to implement similar structure:
There is a table USERS (users). It is necessary to implement correspondence between them. And display the correspondence with each user separately.
For example: A user with id_user 5 writes to a user with id_user 10, user 10 answers user 5. A user with user_id 20 writes to a user with user_id 5, etc.
As I understand it, we create a Messages table (id, message, datetime, is_read) and a connection table between users and messages.
I can not figure out how to identify each correspondence.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2016-02-01
@sim3x

id, id_user_from, id_user_to, message, datetime, is_read

V
Vyacheslav Onufriyuk, 2016-02-02
@fynivx

No, the auxiliary table is not needed here.
If it is necessary that several different correspondences can take place between users, it is worth creating a Conversations table (id, initiator_id, date_started, etc ...) - not for communication, but as an independent entity, and add three references to the Messages table - sender_id, receiver_id and conversation_id.
But if it is assumed that messages between two users are always one correspondence, then it is enough to simply add sender_id and receiver_id to Messages. This will uniquely link each message to two users.
But if you need correspondence between several users at once, then you already need an auxiliary table ConversationMembers (user_id, conversation_id), and you need to remove the receiver_id from Messages - the message will already be associated with the correspondence, and all ConversationMembers will be able to receive it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question