private message database design

删除回忆录丶 提交于 2019-12-10 10:11:43

问题


I'm creating a simple private message system and I'm no sure which database design is better.

The first design is a table for messages, and a table for message comments:

Message
---------------
id
recipientId
senderId
title
body
created_at

MessageComment
---------------
id
messageId
senderId
body
created_at

the second design, is one table for both messages and comments, and an addition field messageId so i'll be able to chain messages as comments.

Message
---------------
id
recipientId
senderId
messageId
title
body
created_at

I'd like to hear your opinion!


回答1:


In this case, I'd vote for one table.

In general, whenever the data in two tables is the same or very similar and the logical concepts they represent are closely related, I'd put them in a single table. If there are lots of differences in the data or the concepts are really different, I'd make them two tables.

If you make two tables and you find yourself regularly writing queries that do a union of the two, that's an indication that they should be combined.

If you make one table but you find there are many fields that are always null for case A and other fields that are always null for case B, or if you're giving awkward double-meanings to fields, like "for type A this field is the zip code but for type B it's the product serial number", that's an indication they should be broken out.




回答2:


Using a single table is the most advantageous.

It allows better message threading possibilities and it reduces duplication of effort, i.e. what happens when you want to add a column.




回答3:


I would rather use the first one and include an additional field del_code to both tables. So, you'll be able to hide deleted messages and still have them in your database.



来源:https://stackoverflow.com/questions/6984750/private-message-database-design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!