views:

47

answers:

2

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?

+2  A: 

Several 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
+1  A: 

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.

Nick Gorbikoff
Um... no. Question -> Answer is a self-referential one-to-many relationship. That's classic E-R modelling and something any database person will easily understand.
cletus
@Nick: cletus is right, last place I was at called it a "pig tail".
OMG Ponies
I understood what you are talking about however in this case nto the best solution. Here is why. If you have something of the same kind that needs to reference itself - then yes this approach is the best. For instance let's say a marketing company has a website and a table of accounts that needs to show who refered whom to sign up for the website to pay referral fee, then yes you can use pig tail. However depending on how your data is setup answers and questions are most likely different type of objects. However IF answers and questions are modeled the same on OS it is a valid approach.
Nick Gorbikoff