One or two tables for forum?

穿精又带淫゛_ 提交于 2019-12-11 16:48:22

问题


Im building a forum and I'm wondering whether I should have one table where I store all main posts and then all the answers in another table.

I've always stored everything in one table, making it east to count and let users comment every post (comments in another table).

What should I do? Pros and cons? Tried to google but didn't find anything.

Thanks for your help!


回答1:


I usually follow the rule every type of dataset gets a own table. This way you can cleanly define relationships

You have types like

  • userTypes (e.g. guest,user,mod,admin)
  • users (has one userType_id)
  • posts (has one user_id)
  • answers (has one post_id)
  • comments (has one post_id, has one answer_id)

Since comments can be added for both, a post and an answer, you could add two bridge tables to define this relationship.

  • comment_to_answer (has one comment_id, has one answer_id)
  • comment_to_question (has one comment_id, has one question_id)

In case you save both, posts and answers into one table, posts table would need to reference themselves to define the posts - answers one-to-many relationship, which would make querying more complicated.

If you want to be able to cascade, post can have an answer, answer can have an answer and so on, you probably go better with one posts table and a parent_id pointing at the id of posts

Hope this helps.




回答2:


This is highly subjective, given that it depends on your needs and intentions. Let me explain...

Forums, such as they are, usually take one of two forms. They are sometimes in the form of a main post, with subsequent comments. The main post, therefore logically would live in one table, and the comments in another. Facebook and Stack Exchange sites are examples of this.

In other cases, the content may take the form of a list of comments. More traditional forums take this form. In the case where hierarchy is called for, rather than pure date ordering, the single table approach makes more sense.

In both cases, hierarchy can be dealt with by creating a parent and child column.

My personal preference would be to go with one table, unless the main post contains an order of additional data that isn't needed for comments. That's just an efficiency thing, but you definitely don't want thousands or millions of rows with NULLs that take up space to no avail. To distinguish a post from a comment you can employ any number of logical schemes such as distinct ID's or flag columns.

Ultimately, an architectural situation like this depends on the project in question. There are advantages and disadvantages to both approaches. Using multiple tables offers a little more in the way of 'future proofing' your project in the case where complexity is added at a later date.




回答3:


Well - since one post can have multiple answers I would go for a separate table.

Pro:

  • Less redundant information
  • Answer can be updated/deleted independent from the post
  • Easier counting of questions / answers

Con

  • Non (well, perhaps that you need to join the tables for certain queries, but hey that does not really count)

Same reason why you have comments already in another table...



来源:https://stackoverflow.com/questions/14109838/one-or-two-tables-for-forum

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