2016-03-18 42 views
0

我有兩個表,分別爲QuestionsAnswers,它們分別存儲多選題和4個可能的答案。我試圖一次插入一個問題及其4個答案。在sql中插入選擇題和答案

The INSERT statement conflicted with the FOREIGN KEY constraint .... "dbo.Answers", column 'AnswerID'. 

The INSERT statement conflicted with the FOREIGN KEY constraint table "dbo.Questions", column 'QuestionID'. 

這裏是我的表是這樣的:

CREATE TABLE [dbo].[Questions] ( 
    [QuestionID] INT   IDENTITY (1, 1) NOT NULL PRIMARY KEY, 
    [HistID]  INT   NOT NULL, 
    [Question] NCHAR (300) NOT NULL, 
    [AnswerID] CHAR   NOT NULL, 
    FOREIGN KEY (AnswerID) REFERENCES [dbo].Answers (AnswerID), 
    FOREIGN KEY (HistID) REFERENCES [dbo].HistoricalEvents (HistID) 
); 

CREATE TABLE [dbo].[Answers] (
    [QuestionID] INT   NOT NULL, 
    [AnswerID] CHAR  NOT NULL, 
    [Choice]  NCHAR (200) NOT NULL, 
    FOREIGN KEY (QuestionID) REFERENCES [dbo].[Questions] (QuestionID), 
    PRIMARY KEY ([AnswerID]) 
); 

這些都是刀片我想:

INSERT INTO Questions (HistID, Question, AnswerID) 
VALUES (
2, 
'A major cause of the growth of state and Federal highway systems after World War II was the ___', 
'C' 
) 

Declare @QuestionID INT = SCOPE_IDENTITY() 


INSERT INTO Answers(QuestionID, AnswerID, Choice) 
VALUES 
(
@QuestionID, 
'A', 
'increased use of mass transit systems' 
), 

(
@QuestionID, 
'B', 
'growing prosperity of inner-city areas' 
), 

(
@QuestionID, 
'C', 
'rapid development of suburbs' 
), 

(
@QuestionID, 
'D', 
'return of city dwellers to farm areas' 
) 

預期輸出: 我想上面的命令(或者它應該改變成什麼)來對這些表進行以下更改:

Questions: 
QuestionID HistID Question     AnswerID 
-------------------------------------------------------- 
    1   2  "A major cause of the... C  

Answers: 
QuestionID AnswerID Choice 
-------------------------------- 
    1   A   increased use of mass transit systems 
    1   B   growing prosperity of inner-city areas 
    1   C   rapid development of suburbs 
    1   D   return of city dwellers to farm areas 
+0

這是SQL Server嗎? – tadman

+0

Microsoft SQL Server,是的 –

+1

而不是(選擇計數())你應該使用@@ IDENTITY或SCOPE_IDENTITY() – JamieD77

回答

4

您已完成所謂的循環引用。您在Answers表中將Answers設置爲FK,將Question設置爲FK。在這裏你已經在兩個桌子上放置了限制。所以你不能插入,因爲插入過程中沒有兩個存在。

這應該是:

問題--->答案[QuestionId] FK僅

這裏有一個提示,糾正你的模式:

問表 -

+------------+ 
| QuestionId | (PK, int) 
+------------+ 
| HistID  | (FK, int) 
+------------+ 
| Question | 
+------------+ 

AnswerTable(可供選擇) -

+------------+ 
| AnswerId | (PK, int) 
+------------+ 
| Choice  | (char) -- A, B, C, D etc. 
+------------+ 
| Answer  | (nvarchar(100)) -- limit depends on your choice 
+------------+ 
| QuestionId | (FK, int) -- 1 to many relationship means 1 question can have multiple answers choice. 
+------------+ 

正確答案(選擇)映射表(FK都將是複合鍵) -

+------------+ 
| AnswerId | (FK, int) 
+------------+   |--- composite primary key, 1-1 relationship means 1 question have only one correct choice. 
| QuestionId | (FK, int) 
+------------+ 

見 - SQL FIDDLE DEMO顯示在執行的想法。

+0

據我所知,不應該有循環引用,但我不確定如何刪除引用,或創建一個Map表,如你所說。 –

+0

在問題表中,沒有必要提及它的答案。然而,答案表確實需要知道它屬於哪個問題。繼續,答案表不需​​要知道正確的答案。映射表將記錄每個問題的正確答案。如果你問的是什麼,那麼看看Alter Table命令。 –

+0

@TysiaKrepps我已添加sqlfiddle演示!您可以從那裏獲取架構腳本。 – vendettamit