Login
Remember
Register
All Activity
Questions
Hot!
Unanswered
Tags
Categories
Users
Ask a Question
Ask a Question
Mysql Error 1452: Can not add or update a child row: a foreign key
Home
Programming
Mysql Error 1452: Can not add or update a child...
+7
vote
4.6k
views
asked
Apr 17, 2014
in
Programming
by
lennon
(
1.6k
points)
I have THE FOLLOWING
[U] [B] TABLE1 [/ B] [/ U] Indices: code, provider code: varchar (15) supplier: int (4)
[U] [B] TABLE 2 [/ B] [/ U] Indices: code, provider code: varchar (15) supplier: int (4)
And try to put a foreign key with this command
ALTER TABLE table1 ADD CONSTRAINT FOREIGN KEY `` `FK_tabla1 FK_tabla1` (`code`, `supplier`) REFERENCES `table2` (`code`, `supplier`) ON DELETE NO ACTION ON UPDATE NO ACTION;
And all the while I get the error
Can not add or update a child row: a foreign key constraint fails (`XXXXXXXXX / # sql-810_70491`, CONSTRAINT `fk_tabla1` FOREIGN KEY (`code`, `supplier`) REFERENCES `table2` (`code`, `supplier` ) ON DELETE NO ACTION ON UPDATE
Not to do ...
Thanks
Your answer
Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
1
Answer
+7
votes
answered
Apr 17, 2014
by
connor
(
17.1k
points)
You are trying to add a foreign key when no referential integrity. In table1 have code-provider couples who are not in table2 ... like the relationship is reversed?
Anyway note that when defining a multi reference key field references must be given together within the same row, so if you table2 (1, 1), (1, 2), (2, 3 ) and table 1 (1, 1), (1, 2), (1, 3) the tuple (1, 3) is not present in table2 and will not leave.
To see how many rows do not meet the integrity, you can launch a
SELECT COUNT (*) AS count FROM table1 t1 LEFT JOIN table2 t2 ON t1.codigo = t2.codigo t1.proveedor = t2.proveedor AND WHERE IS NULL t2.codigo
If not many can remove the COUNT and do you return the t1. * And so see what they are.
Greetings.
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Ask a Question
Welcome to WikiTechSolutions where you can ask questions and receive answers from other members of the community.
You can ask a question without registration.
Related questions
Error 1402 or 1406 (Could not open key: UNKNOWN \ Components)
Drupal 7 - add a button not a submit for javascript calls
I can not download or update anything on the App Store
Categories
All categories
Programming
(93)
Android
(3.1k)
IOS
(721)
SmartPhones
(82)
Windows
(219)
Windows Mobile
(49)
Linux
(61)
Internet
(55)
Computer
(101)
Sony
(79)
Safe Mode
(4)
Home
(124)
Help
(110)
Friend Sites
Smartphone Connections
Smartphone Problems
...