The ALTER TABLE statement conflicted with the FOREIGN KEY constraint The ALTER TABLE statement conflicted with the FOREIGN KEY constraint sql-server sql-server

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint


It occurred because you tried to create a foreign key from tblDomare.PersNR to tblBana.BanNR but/and the values in tblDomare.PersNR didn't match with any of the values in tblBana.BanNR. You cannot create a relation which violates referential integrity.


This query was very useful for me. It shows all values that don't have any matches

select FK_column from FK_tableWHERE FK_column NOT IN(SELECT PK_column from PK_table)


Try this solution:

There is a data item in your table whose associated value doesn't exist in the table you want to use it as a primary key table. Make your table empty or add the associated value to the second table.