In a StackOverflow clone, what relationship should a Comments table have to Questions and Answers?

前端 未结 6 1965
盖世英雄少女心
盖世英雄少女心 2020-11-30 06:14

In an application similar to StackOverflow that I am building, I am trying to decide what relationship my Questions, Answers and Comments

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 06:34

    There are 2 ways that I can think of.

    First, use another column in the Comment table to indicate whether the comment belongs to Question or Answer. So the PK of the Comment table becomes wher PostID is the foreign key to Question or Answer and PostType can be someting like 1=Question and 2=Answer.

    Second, use a relationship table for each Question and Answer. So you have a Question, Answer, Comment, QuestionComment, and AnswerComment table.

    Let's say the primary key of Question, Answer, Comment tables are QuestionID, AnswerID, and CommentID respectively. Then the columns of QuestionComment would be [QuestionID, CommentID]. Similarly, the columns of AnswerComment would be [AnswerID, CommentID].

提交回复
热议问题