INSERT statement conflicted with the FOREIGN KEY constraint - SQL Server

前端 未结 14 1098
-上瘾入骨i
-上瘾入骨i 2020-11-22 11:09

I am getting the following error. Could you please help me?

Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN

相关标签:
14条回答
  • 2020-11-22 11:48

    I ran into this problem when my insert value fields contained tabs and spaces that were not obvious to the naked eye. I had created my value list in Excel, copied, and pasted it to SQL, and run queries to find non-matches on my FK fields.

    The match queries did not detect there were tabs and spaces in my FK field, but the INSERT did recognize them and it continued to generate the error.

    I tested again by copying the content of the FK field in one record and pasting it into the insert query. When that record also failed, I looked closer at the data and finally detected the tabs/spaces.

    Once I cleaned removed tabs/spaces, my issue was resolved. Hope this helps someone!

    0 讨论(0)
  • 2020-11-22 11:53

    Double check the fields in the relationship the foreign key is defined for. SQL Server Management Studio may not have had the fields you wanted selected when you defined the relationship. This has burned me in the past.

    0 讨论(0)
  • 2020-11-22 11:55

    In your table dbo.Sup_Item_Cat, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.

    If you have SQL Server Management Studio, open it up and sp_help 'dbo.Sup_Item_Cat'. See which column that FK is on, and which column of which table it references. You're inserting some bad data.

    Let me know if you need anything explained better!

    0 讨论(0)
  • 2020-11-22 11:56
    1. run sp_helpconstraint
    2. pay ATTENTION to the constraint_keys column returned for the foreign key
    0 讨论(0)
  • 2020-11-22 11:57

    The problem is not with client_id from what I can see. It looks more like the problem is with the 4th column, sup_item_cat_id

    I would run

    sp_helpconstraint sup_item
    

    and pay attention to the constraint_keys column returned for the foreign key FK_Sup_Item_Sup_Item_Cat to confirm which column is the actual problem, but I am pretty sure it is not the one you are trying to fix. Besides '123123' looks suspect as well.

    0 讨论(0)
  • 2020-11-22 11:57

    In my case, I was inserting the values in the Child Table in the wrong order

    For Table with 2 columns- Column1 and Column2, i got this error when I mistakenly entered:

    Insert into Table values('value for column2''value for column1')
    

    Error resolved when I used below format :-

    Insert into Table (column1, column2) values('value for column2''value for column1')
    
    0 讨论(0)
提交回复
热议问题