Database Design: Should I use a mapping table?

Using a question and answer site like this, for example. Say I have tables question, answerand comment. Questions and answers may have several comments.

Would be better:

  • create QuestionCommentand AnswerCommenttables for comparing the questions / answers in the comments (each containing a question / pk answer and comment pk)?

  • Or do I need to have only a comment table containing 2 zero foreign keys for the question and answer (one of which will always be empty, since the comment can be applied to only one "element")?

It seems that (1) supports referential integrity, while (2) is more compact. Is one of them preferable to the other? Should matching tables be maintained only for many-to-many relationships?

+3
source share
2 answers

Will you use both comments in the same way? If so, then (2) otherwise (1)

If you use (1), you can create a view on both tables so that they appear as one.

In case (2), you can add a trigger to ensure that there is only one Foreign Key column to fill in the row, or as @Ronnis it is suggested to use the CHECK constraint (best method).

+1
source

, , (1) :

, (1) , - - . , , , .

, : , . ( , -.)

0

Source: https://habr.com/ru/post/1779083/


All Articles