• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 526
  • Last Modified:

The INSERT statement conflicted with the FOREIGN KEY constraint ... SQL SERVER

Hi,

I am in the process of populating a relational database in sql and migrating data from a none relational database.
I get error message: "The INSERT statement conflicted with the FOREIGN KEY constraint ...."
for some of tables.
It happens when the relationship between the two table is like:

LookupTbl              (1: M )            childTbl

UnitOfMesure (PK)                       Name
                                                     Surname
                                                     Height
                                                     UnitOfMEasure(FK), null

sample records in Lookuptbl are: Meter, Inch

some of the old tables I am using as a source for migration do not have UnitOfMeasure column at all, so I have added them to the tables before doing the migration but they are obviously empty and have no value.
When I try to import data into childTbl, I keep getting this error message:The INSERT statement conflicted with the FOREIGN KEY constraint ....

I have been testing the migration, so I have deleted the tables and tried to populate them again and again and I am not sure why a column which can be NULL is causing problem...

Thanks in advance
0
shmz
Asked:
shmz
1 Solution
 
John_ArifinCommented:
You cannot insert a Null value to KEY FIELD.
To populate the table you have to:
1. Temporarily delete the Relationship of those two table (You can delete at the Diagram)
2. Insert the data (UnitOfMeasure should Allow Null, change it in the Table Design)
3. Update the  UnitOfMeasure : UPDATE childTbl SET UnitOfMeasure = 'Meter' Where UnitOFMeasure Is Null And Height < 2
4. Update the  UnitOfMeasure : UPDATE childTbl SET UnitOfMeasure = 'Feet' Where UnitOFMeasure Is Null And Height < 6
5. Update the  UnitOfMeasure : UPDATE childTbl SET UnitOfMeasure = 'Inch' Where UnitOFMeasure Is Null And Height < 70
6. Update the  UnitOfMeasure : UPDATE childTbl SET UnitOfMeasure = 'Cm' Where UnitOFMeasure Is Null And Height < 200

I am just guessing that there is no height below 70 cm, otherwise the inch will mix with the cm.

7. Change the Allow Null of the UnitOfMeasure to Null
8. Built Relationship again in the Diagram


0
 
BrandonGalderisiCommented:
You can insert a NULL value into the table which is referencing (as child) another table (as long as the column permits NULL).  Only non-null values are checked.
0
 
shmzAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now