I am developing a message / comment table in MySQL for a social networking site. I have a messaging system in which two participating users (only two and no more) will have a single / unique stream. Thus, whenever a user sends messages to another user, then he will check whether both users have a unique IF NOT stream, after which he will create a unique stream for both users.
create table comment_threads (
thread_id int (PK),
user_id_1 int,
user_id_2 int,
created datetime
);
create table comments (
comment_id int (PK),
thread_id int (FK),
comment text,
created datetime
);
Whenever a user sends a message to another user each time, I have to check if both participating users had a previous thread, so I have to query the database for this (QUERY 1). And again, if there was no thread to create a thread in comment_thread (QUERY 2). Then post the comment again in the comment table (QUERY 3). So I have to request two or three times for messaging.
Questions:
- Are the above tables the correct way to solve my problem or to fix it?
- Is there any other better way to do this?
source
share