Skip to content
Advertisement

SQL – Multiple one-to-many relationships

Is it possible somehow to do multiple one-to-many relationships between 2 tables? Like:

Table abc

  • abcID
  • defID
  • message

Table def

  • defID
  • abcID
  • message

If yes how can I then make a new abc entry with the entity framework?

Advertisement

Answer

You only need a single Many-Many relationship. Simply move QuestionId out of the quiz_answers table and move AnswerId out of the quiz_questions table:

Create Table quiz_questions
(
QuestionId ... Not Null Primary Key
, Question ...
, ...
)

Create Table quiz_answers
(
AnswerId ... Not Null Primary Key
, Answer ...
, ...
)

Create Table quiz_question_answers
(
QuestionId ... Not Null References quiz_questions ( QuestionId )
, AnswerId ... Not Null References quiz_answers ( AnswerId )
, Constraint PK_quiz_question_answers Primary Key ( QuestionId, AnswerId )
)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement