• Register
1 vote
29 views

Problem :

I am very new to the Sql Server. Currently I want to add the new foreign key to the already existing table in this table there is existing data in a column where I want to make the change.

I tried this approach on a table for a column where data is present and also for a column data does not exist. I have noticed where there is no data it works fine. Please find below the error message.

The alter table statement conflicted with the foreign key constraint”.

Can someone guide me in fixing this foreign key error?

8 5 2
3,230 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution :

I think you are facing above error because you may have the orphaned records in your table. The orphaned record is the record which is present in the child table without having the corresponding parent record in its parent table.

To fix the problem you need to cleanup your data.To cleanup your data you must first find the records in your child table which do not have the corresponding parent record in parent table. After that you need to either delete or update the required records in your child table or you can add your missing parent records in to your required parent table. Afterwar doing this exercise you should be able to create your foreign key constraint.

8 4
5,680 points

Related questions

0 votes
2 answers 23 views
23 views asked Dec 30, 2020 miki 1.9k points
0 votes
1 answer 42 views
42 views
Problem: I am able to create the table named employee which contains the column empid which is a computed column and empid column is the primary key also. It is the self referencing table and the column managerid references back to itself. But now if I try to save ... error my table is still working perfectly. I am trying to work in a table graphically not coding every time I make the change.
asked Jul 4, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 6 views
6 views
the delete statement conflicted with the reference constraint
asked Feb 16 ArifulIslam 7.5k points
0 votes
1 answer 25 views
25 views
Problem: I am unable to understand why a TRUNCATE on mygroup is not working? Even though I have it ON DELETE CASCADE SET I encounter below strange error: ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (mytest.instance, ...    DateTime     DATETIME DEFAULT NULL, FOREIGN KEY  (GroupID) REFERENCES mygroup(ID) ON DELETE CASCADE,    UNIQUE(GroupID) ) ENGINE=InnoDB;
asked Jul 23, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 302 views
302 views
I am facing this problems in many times and hardly getting the soluton. Can someone here help me to solve this issue?
asked Sep 14, 2020 Daniel Anderson 4k points
0 votes
2 answers 1.1K views
1.1K views
Problem : I have created the tables in MySQL Workbench. But when I try to insert the values into the ORDRELINJE table i get below error: Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (srdjank.Ordrelinje, CONSTRAINT Ordrelinje_fk FOREIGN KEY ... (OrdreID)) I have seen many other posts on this topic but unable to fix my issue. How can I fix this issue?
asked Jan 24, 2020 jwilliam 3.9k points
0 votes
2 answers 298 views
298 views
Problem : I have created the tables in MySQL Workbench. But when want to insert values into my TBL ORDRELINJE table I face below error : Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (srdjank.TBLOrdrelinje, CONSTRAINT TBLOrdrelinje_fk FOREIGN KEY ... do the research on this topic, but no luck. Am I doing any mistake or someone have any idea about what can I do?
asked Dec 21, 2019 alecxe 7.5k points
0 votes
1 answer 80 views
80 views
Problem : When I try to execute the below SQL command : INSERT INTO mytest_usershosts (RID,userid,hid,Usr,Pass) VALUES (NULL,1,1,"user","pass"); I am facing the below error message : ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f ... similar cases, but I am unable to find any solution. Is there anyone who have faced the same issue and knows the fix for the issue?
asked Dec 11, 2019 alecxe 7.5k points
0 votes
1 answer 256 views
256 views
Problem : In the MySQL I am trying to drop the table named bericht. I tried many options however still facing the same error that my table cannot be dropped. I am facing following error message : #1217 - Cannot delete or update a parent row: a foreign key constraint fails Does anybody have any clue about this error #1217?
asked Dec 2, 2019 alecxe 7.5k points
1 vote
1 answer 8 views
8 views
Problem: (Using sql server 2008 r2) Can you truncate the table whose primary key is lined as foreign key in the other table?
asked Feb 23 Sofia Castro 4.2k points