?
Solved

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

Posted on 2011-10-03
3
Medium Priority
?
518 Views
Last Modified: 2012-05-12
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
Comment
Question by:shmz
3 Comments
 
LVL 3

Expert Comment

by:John_Arifin
ID: 36907278
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
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 2000 total points
ID: 36908071
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
 

Author Closing Comment

by:shmz
ID: 36965836
Thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question