user2878851
user2878851

Reputation: 163

not able to create Foreign key in sql server table

I have two table like this:

tblGender

enter image description here

and TblPerson

enter image description here

I want to set foreign key for GenderID.. but I'm getting this error:

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "tblPerson_GenderID_FK". The conflict occurred in database "Sample", table "dbo.tblGender", column 'id'.

Upvotes: 1

Views: 1514

Answers (2)

Mihai
Mihai

Reputation: 26784

You might have records in tblGender which don't have match in tblPerson on their respective columns.Try emptying the tables if that is an option.

Upvotes: 4

Vijay Hulmani
Vijay Hulmani

Reputation: 979

This is the script to add foreign key constraint.

alter table TblPerson
add constraint tblPerson_GenderID_FK FOREIGN KEY ( GenderID) references tblGender(id)

Upvotes: 0

Related Questions