Cant figure out how to fix Error 1452 - mysql

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.

Related

could anyone suggest me 2 solve this?

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

Whats wrong with this IF statement?

I'm learning about MySQL, I'm using PHPMyAdmin in wamp server. Now I'm trying to make a trigger for a table but I'm having troubles when I insert data in the parent table. I just want to evaluate a field named Es_Ban and depending on its value fill a table o another
here is my code:
IF new.es_ban = 1 THEN
INSERT INTO Banca(Ban_Due,Nom_Ban,Mon_Banca)
VALUES (new.Usu_Log, new.Ban_Age, 0 );
END IF;
After inserting a row in the parent named logins I get this error:
1452 - Cannot add or update a child row: a foreign key constraint
fails (`prueba`.`banca`, CONSTRAINT `banca_ibfk_1` FOREIGN KEY
(`Ban_Due`) REFERENCES `logins` (`Usu_Log`) ON DELETE CASCADE ON UPDATE CASCADE
The only way I got it to work was by delenting the relationship between Login and Banca...

Sql query not running

When I run the following query on phpmyadmin then it gives me following error
INSERT INTO `infrastructure_support_info`(`s_no`, `student_no`, `books_availability`, `basic_requirements`, `technological_support`, `study_material`, `resource_availability`, `cleaniliness_of_class`)
VALUES ('', '', '$availabilityOfBooks', '$basicRequirements', '$technologicalSupport', '$photocopyOfStudyMaterial', '$availabilityOfOtherResources', '$cleanlinessOfClass')
#1452 - Cannot add or update a child row: a foreign key constraint fails (feedback_system_db.infrastructure_support_info, CONSTRAINT user_std_no FOREIGN KEY (student_no) REFERENCES user_master (student_no))
Your table has a reference to another one. If you want to leave that link empty then use null instead of a blank string ''.

Insert dates in MySql Workbench

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;

INSERT INTO... SELECT in same table generates foreign key error

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.