john mossel
john mossel

Reputation: 2156

Differentiating between a question and answer

From this question: Stackoverflows Tags system, How To

SO considers questions and answers to be the same thing - a Post.

A question has a title, whereas a reply does not, how does SO deal with this? What would the table structure be?

How do you differentiate between a question and an answer and what would go in the title field for a answer? Can you show me what this would look like in MySQL?

Upvotes: 1

Views: 107

Answers (1)

Goran Jovic
Goran Jovic

Reputation: 9508

In general, this is a problem of object-oriented inheritance and its mapping to relational model used by databases like MySQL (or MS SQL in case of SO).

You can find more resources about that if you search using these keywords. I'll just give you a short list of different approaches to the problem:

  1. You can use one big table for all posts, and then have the values for title column have null value for answers. Good for performance, no need to have too many joins, too much unused data.
  2. You can have all different tables with redundant columns for shared properties. Good for performance, no extra joins, no null values using up space, some redundant code, though.
  3. You can have a separate table for the shared properties called say post and a table for each subtype questions, answers etc. No null values, no redundant code, but must use SQL joins for just about anything, theoretically cleanest as it is normalized (academic world seems to love that), but for large amount of data simply useless.

Which particular strategy was chosen for SO, I don't know. Maybe someone of the mods would care to answer that.

Upvotes: 4

Related Questions