0

im getting the following error when trying to populate my products table

1452 - Cannot add or update a child row: a foreign key constraint fails (computer_wholesale.products, CONSTRAINT products_ibfk_1 FOREIGN KEY (supp_id) REFERENCES suppliers (supp_id))

this is the statement im using to populate the table

INSERT INTO products
(prod_id,prod_name,price,on_hand,supp_id)
Values
("DTHP7710","HP desktop","499.99","5","kbc355"),
("dtcp2109","cp desktop","479.99","2","adt217"),
("dtcp2149","cp desktop","799.99","2","adt217"),
("ltpbv060","packbell mz35-v060","349.99","3","cps533"),
("ltcpc504","cp c504ea","429.99","3","adt217"),
("lttaa100","toshiba a100","492.99","4","cps533"),
("LTPBMZ36","PackBell MZ36-T019","549.99","6","CPS533
"),
("LTTA0338","Toshiba A100-338","599.99","5","FCL162
"),
("DJHP1280","HP Deskjet 1280","172.92","5","KBC355
"),
("DJEPR180","Epson Stylus R1800","318.89","3","FCL162"),
("A1HPC318","HP Photosmart C3180","34.99","8","KBC355
"),
("A1HPF380","HP Deskjet F380","39.99","8","KBC355
"),
("A1LX5470","Lexmark X5470","49.99","4","FCL162"),
("A1EP6050","Epson Stylus DX6050","59.99","4","FCL162
");

if anyone could tell me what im doing wrong or give a solution that would fix the proble it would be greatly appreciated.

Thanks

Community
  • 1
  • 1
jmc1690
  • 75
  • 2
  • 2
  • 8

2 Answers2

0

you have at least one row in the second(supplier )table i.e. child table referencing a non-existent row in the parent table.

You could disable using --

SET FOREIGN_KEY_CHECKS = 0;
Abhishek
  • 1,543
  • 3
  • 13
  • 29
  • where would i enter this? – jmc1690 May 31 '15 at 17:04
  • ok i ran tha query then ran the insert query and still get the same error. – jmc1690 May 31 '15 at 17:08
  • Although , it should be ok, is that possible for you to remove all that data from current table and then run that query in sql query window. check below questions thread that might help you better as they generated alot of upvotes there .-- http://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa , http://stackoverflow.com/questions/5198600/mysql-foreign-key-error-1452 – Abhishek May 31 '15 at 17:15
0

Your supp_id in products table is foreign key which refers to supp_id of suppliers table. Due to this foreign key constraint you can only enter those values of supp_id which are present in suppliers table. You are getting this error because you are trying to insert value of supp_id in products table which is not present in suppliers table.

tec
  • 41
  • 9