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?
views:
47answers:
2Several reasons:
- Conceptually questions and answers aren't really that different from a modelling perspective;
- It makes it easier to search (one table rather than two);
- If you've ever tried to implement such a system across two tables (and this includes forums where you have posts that start threads and replies) you'll quickly discover the SQL gets really awkward really fast.
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).
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.