Reputation: 65278
I have learned that SO has put questions and answers in the same table. They say that a question will not have a parent id and answers will have parent IDs. Why is it better to do it this way instead of putting questions and answers in separate tables?
Upvotes: 3
Views: 194
Reputation: 7038
Cletus is right, however keep in mind that by doing do you are denormalizing your DB, since while answers and questions are similar, they are however not the same. In particular One Question has many answers, but each answer belongs only to one question - there for you will start storing redundancy repetitious data in the table. While there might be some speed / development advantages to doing so depending on your infrastructure, if you tell about this to any DB admin they'll have a heart attack :-) So yes you can do it and it makes it slightly easier to develop, it is however not that difficult to write some joins and unions to pull the same data.
Upvotes: 1
Reputation: 625237
Several reasons:
The best way to look at this problem is to run a particular model through some use cases.
For example: list all posts by a user in some order (votes, date, etc). Across two tables you end up doing some kind of UNION, which is not really undesirable. If they're stored in the same table it's much easier. If you just want to limit it to questions or answers then the same table is still easy as it's just an extra criteria (eg WHERE parentID IS NULL for questions).
Upvotes: 2