views:

318

answers:

1

Hi guys, I'm building a small messaging system like you have in facebook. Only thing is that I want to know whats the best way to get on about it. The database design to be precise in this case.

Like all users would have an inbox and sent items folder. Logically something in one persons sent folder would actually be in someone elses inbox. However I don't think using one table to hold the messages would work as lets say someone deletes a message form his sent box it would be gone since both reciever and sender would be referring to the same table. How do I implement this...

+5  A: 
Blackethylene
What did you use to make that diagram?
Unknown
I'd like to know that as well.
Maciek
It's MySQL Workbench, a wonderful tool to visually design MySQL databases.http://dev.mysql.com/downloads/workbench/
Blackethylene
Nice - how do I set it for both recievers and senders. DO I need to make separate tables for both?
Ali
No, you can simply add a field in the "users_has_messages" table, where you can specify if the user is the sender or a receiver. To keep it minimalistic, this field could be a boolean field named something like "is_sender".
Blackethylene
you said: "it only deletes a row in the "users_has_messages" table, and not the message itself"and what about the messages? why do you keep the message? if it's deleted i don't think we need to keep it. or am i wrong?
fabrik
Usually a message has one sender and one/several receiver(s). If one of them erase it in its inbox, the application just erases the row in "users_has_messages" table. So others can still see it. In case all the users have deleted this message, it's true we do not need to keep it. There are many solutions to 'clean' the table from orphan messages, a cron job for example.
Blackethylene