Msg 547- FK Constraint Error

169 views Asked by At

I am a newbie with SQL and I am getting an error when inserting data into a table called "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'.

The FK's in the Speech table have data that correspond to the PK's in the related tables. The code I used to create the Speech table is:

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);

For reference: Table Speech- speechid(PK), speakerid(FK), presentationid(FK), conferenceid(FK) Table Conference- conferenceid(PK) Table Speaker- speakerid(PK) Table Presentation- presentationid(PK) –

Any help would be appreciated.

1

There are 1 answers

1
Gordon Linoff On

Shouldn't the first foreign key relationship be:

Foreign Key (speakerid) references Speaker(SpeakerId),

Also, I would expect the keys used for the foreign key relationships to be in the definition:

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

At least, I always include them.