This question already has answers here:
Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails
(21 answers)
Closed 9 years ago.
I am trying to insert statements into my database. Here are the insert statements:
insert into advisor values ('00001', '11111');
insert into advisor values ('00002', '22222');
insert into advisor values ('00003', '33333');
insert into advisor values ('00004', '44444');
insert into advisor values ('00005', '55555');
insert into advisor values ('00006', '66666');
insert into advisor values ('00007', '77777');
insert into advisor values ('00008', '88888');
insert into advisor values ('00009', '99999');
insert into advisor values ('00010', '10101');"
I am getting an error on this one:
insert into advisor values ('00004', '44444');
with this error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`university database`.`instructor`, CONSTRAINT instructor_ibfk_1 FOREIGN KEY (`department_name`) REFERENCES `department` (`department_name`) ON DELETE SET NULL)
Can anyone lend any knowledge to this error?
Thanks!
The error is self explanatory. The value(s) you are entering must first exist in the referenced table in the foreign key constraint.
You should also use a column list in your INSERT statements.
Related
A Database Error Occurred
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (dihr.tbl_contract, CONSTRAINT tbl_contract_ibfk_2 FOREIGN KEY (id) REFERENCES tbl_attactment (contract_id) ON DELETE CASCADE ON UPDATE CASCADE)
INSERT INTO tbl_contract (staff_id, type_id, start_date, end_date, status) VALUES ('2', '1', '2018-03-20', '2018-03-30', '1')
Filename: C:/xampp/htdocs/system/database/DB_driver.php
Line Number: 691
This is relationship table here
This is error message here
The id column in the table tbl_contract is dependent on the contract_id column in table tbl_attactment
You are not inserting this tbl_contract.id column in your insert.
If you do, you should be aware that the tbl_contract.id should exist in the tbl_attactment.contract_id
This question already has answers here:
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
(28 answers)
Closed 6 years ago.
Trying to do a simple insert into statement but getting an foreign key relationship error.
insert into orders (userId, orderDate, shippingDate)
values('xyz123', now(), now());
The error I am getting is
"Cannot add or update a child row:a foreign key constraint fails ('example_1010.orders,CONSTRAINTorders_ibfk1FOREIGN KEY (userId) REFERENCESusers(userid`))"
I think I need to use an "in clause" to bypass the constraint but I don't think I am using it correctly.
insert into orders (userId, orderDate, shippingDate)
values('xyz123', now(), now())
in (select userId from users);
Just disable the foreign key
SET FOREIGN_KEY_CHECKS=0;
you don't need any other thing to add. your query is perfect
insert into orders (userId, orderDate, shippingDate)
values('xyz123', now(), now());
after that again enable foreign key
SET FOREIGN_KEY_CHECKS=1;
You should add that user_id in your parent table(users) as suggested by #drew
I have a error when I try to insert dates with MySql Workbenck
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (myfacebook.mensajes, CONSTRAINT CodigoRedDest FOREIGN KEY (Codigo) REFERENCES redessociales (Codigo) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL statement:
INSERT INTO `myfacebook`.`mensajes` (`Codigo`, `NickUsuario`, `CodigoRedDest`, `Mensaje`, `Fecha`)
VALUES ('7', 'MaGo', '1', 'M7', '2013-09-23')
I'm beginner and I don't know what is the problem. If you need more dates about my tables, you say me.
Thanks for your answers.
One solution is to disable the foreign key check with SET FOREIGN_KEY_CHECKS:
SET FOREIGN_KEY_CHECKS=0;
INSERT INTO `myfacebook`.`mensajes` (`Codigo`, `NickUsuario`, `CodigoRedDest`, `Mensaje`, `Fecha`)
VALUES ('7', 'MaGo', '1', 'M7', '2013-09-23')
SET FOREIGN_KEY_CHECKS=1;
I am having trouble with the following insert query.
INSERT INTO CM_LABEL_CALENDAR (
label_id,
label_name,
order_seq,
meal_id,
hyperlink
)
SELECT
label_id,
label_name,
order_seq,
(meal_id + 315),
hyperlink
FROM
CM_LABEL_CALENDAR
WHERE
(meal_id BETWEEN '1466' AND '1521');
When I try to execute it I get the following error:
Lookup Error - MySQL Database Error: Cannot add or update a child row: a foreign key constraint fails (TEST_PBMS.CM_LABEL_CALENDAR, CONSTRAINT CM_LABEL_CALENDAR_ibfk_1 FOREIGN KEY (meal_id) REFERENCES CM_MEAL_CALENDAR (meal_id))
I've tried looking for an answer but couldn't find one.
There is a foreign key constraint between CM_LABEL_CALENDAR(meal_id) and CM_MEAL_CALENDAR(meal_id)
You are getting this error because you are trying to insert values in the meal_id column that do not exist in the CM_MEAL_CALENDAR table.
I cant figure out this error message.I know there are similar post but they have not helped.
ERROR 1452: Cannot add or update a child row: a foreign key constraint
fails (workingmodel.cadet, CONSTRAINT fk_Cadet_YEAR_t1 FOREIGN
KEY (YEAR_t_idYEAR_t) REFERENCES year_t (idYEAR_t) ON DELETE NO
ACTION ON UPDATE NO ACTION)
SQL Statement:
INSERT INTO `workingmodel`.`cadet` (`CID`, `YEAR_t_idYEAR_t`, `Rank_idRank`, `BN_idBN`, `PTscore`, `Academic_idAcademic`, `CadetLastName`, `CadetFirstName`) VALUES ('00123456', '2015', 'CPL', '2', '220', 'CS', 'Matthews', 'Bob')
It is all about Referential Integrity.
Table cadet is dependent on table year_t on column YEAR_t_idYEAR_t into column idYEAR_t.
You are inserting a value 2015 on table cadet that the value of YEAR_t_idYEAR_t doesn't exist on table year_t.
To fix your problem, you need to insert 2015 on table year_t first and execute your INSERT statement again.