消息547-FK约束错误

时间:2015-06-07 21:19:19

标签: sql foreign-keys

我是SQL的新手,我在将数据插入名为“Speech”的表时遇到错误:

Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK__Speech__speakeri__656C112C". The conflict occurred in database "team04", table "dbo.Speech", column 'speechid'.

语音表中的FK具有与相关表中的PK相对应的数据。我用来创建Speech表的代码是:

Create Table Speech
(speechid smallint primary key,
speakerid smallint,
presentationid smallint,
conferenceid smallint,
speechdate date,
speachstarttime time,
Foreign Key (speakerid) references Speech,
Foreign Key (presentationid) references Presentation,
Foreign Key (conferenceid) references Conference);

供参考: 表语音 - 语音(PK),扬声器(FK),演示文稿(FK),conferenceid(FK)表会议 - conferenceid(PK)表扬声器 - 扬声器(PK)表演示 - 演示文稿(PK) -

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

第一个外键关系不应该是:

Foreign Key (speakerid) references Speaker(SpeakerId),

另外,我希望用于外键关系的密钥在定义中:

    Foreign Key (presentationid) references Presentation(presentationid),
    Foreign Key (conferenceid) references Conference(conferenceid)
);

至少,我总是包括它们。