Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database Design - Column In One Table References Two Tables

Here is an example of what I have (take Stack Overflow). I have 2 tables, Questions and Answers. I also have a Comments table. The Comments table will reference both Questions and Answers.

How should I set up the database? Have 2 columns in Comments, a QuestionId and AnswerId. Have one table for both Questions and Answers? Have a table in between that somehow tells me Question or Answer?

EDIT: Found the SO Data explorer, it uses one table for both Questions and Answers ... I just don't like the POSTS table having so many NULLS in it. Does that have any negative effects, like on performance?

like image 544
Martin Avatar asked Oct 08 '10 02:10

Martin


1 Answers

StackOverflow models the questions and answers as being the same entity: POSTS. They have identical properties, aside from indicating the answer where accepted/granted.

Comments get their own table, and relate to the respective post using a foreign key -- the post_id.

Without needing to load the monthly SO dumps, you can view (and query) the SO schema via the StackExchange Data Explorer.

like image 75
OMG Ponies Avatar answered Sep 24 '22 15:09

OMG Ponies