Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change this many to one relationship to one to one?

Tags:

c#

.net

sql

I have two tables as in the snapshot below.

![Diagram][1]

Scenario: A question should have only one correct answer, but can have many ( 3 in my case) wrong answers ( like a quiz show).

Problem:

Questions table has multiple answers in the Answers table, but only one correct answer. The correct answer is the AnswerID in Questions table and it is related to AnswerID column in Answer table. But it shows up as a Many to One relationship ( please see the bolded fields).

I have applied UNIQUE constraint to AnswerID in Question table but still it shows a many to one relationship. What can I do so that each AnswerID column entry is linked to a single AnswerID in the Question table? or is this ok as it is?

Thank you

QUESTIONS TABLE:

CREATE TABLE [dbo].[Questions](
        [QuestionID] [int] NOT NULL,
        [QuestionText] [nvarchar](max) NOT NULL,
        [AnswerID] [int] UNIQUE NOT NULL,
        [ImageLocation] [ntext] NULL,
     CONSTRAINT [PK_Questions_1] PRIMARY KEY CLUSTERED 

ANSWERS TABLE:

CREATE TABLE [dbo].[Answers](
    [AnswerID] [int] NOT NULL,
    [AnswerText] [nchar](50) NOT NULL,
    [QuestionID] [int] NOT NULL,
 CONSTRAINT [PK_Answers] PRIMARY KEY CLUSTERED 
like image 847
iAteABug_And_iLiked_it Avatar asked Aug 30 '13 13:08

iAteABug_And_iLiked_it


1 Answers

Another approach:

  1. Drop the QuestionID from your Answers table.
  2. Drop the AnswerID column from your Questions table.
  3. Create a new table, QuestionAnswer (or something more meaninful, like Exam, Test, etc.), which has a QuestionID, an AnswerID, and an IsCorrect flag.
  4. Map all Answers to their Questions, and mark which one is correct.

Now you can re-use answers for other questions as well, and every question and ever answer only exists once. It may also be useful if you want to add additional metadata about the question answer combinations. For example:

  1. A Sequence column to control the order in which the answers appear.
  2. A PointValue column to help produce a final score or grade.
like image 53
Cᴏʀʏ Avatar answered Oct 24 '22 18:10

Cᴏʀʏ