MySQL #1452 - Cannot add or update a child row - mysql

I'm begginer here, all what i'm trying to do is insert into a table field which is a foreign key, please take a look at this two tables :
Table Categorie
CREATE TABLE IF NOT EXISTS `categorie` (
`id_cat` int(2) NOT NULL AUTO_INCREMENT,
`nom_cat` varchar(20) NOT NULL,
PRIMARY KEY (`id_cat`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
and table Annonce
CREATE TABLE IF NOT EXISTS `annonce` (
`id_annonce` int(6) NOT NULL AUTO_INCREMENT,
`titre` varchar(30) NOT NULL,
`description` varchar(255) NOT NULL,
`tarif` float NOT NULL,
`deplacement` int(2) NOT NULL,
`date_creation` date NOT NULL,
`date_expiration` date NOT NULL,
`image` varchar(255) NOT NULL,
`id_cat` int(2) DEFAULT NULL,
PRIMARY KEY (`id_annonce`),
KEY `id_cat` (`id_cat`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
After linking the foreign key id_cat manually (ON UPDATE SET NULL ON DELETE CASCADE) that's how the db looks like
and after inserting data into Categorie table it looks like this
But unfortunately i couldn't execute this query :
INSERT INTO annonce (id_annonce, titre, description, tarif, deplacement,
date_creation, date_expiration,id_cat)
VALUES('','anything','anything',2,3,'2017-04-01','2017-04-01',2)
the error says :
1452 - Cannot add or update a child row: a foreign key constraint fails
(lametcom.annonce, CONSTRAINT annonce_ibfk_2 FOREIGN KEY (id_cat)
REFERENCES annonce (id_cat) ON DELETE SET NULL ON UPDATE CASCADE)
Can anyone help please and so sorry about my poor English i hope you can understand what i mean

Your foreign key constraint is wrong. You have
FOREIGN KEY id_cat REFERENCES annonce (id_cat)
but it should be:
FOREIGN KEY id_cat REFERENCES categorie (id_cat)
The table name in the foreign key constraint has to be the table you're linking to.

Related

mysql one to many relation is not working

Having some trouble adding a relation between tables
CREATE TABLE IF NOT EXISTS `employees` (
`emp_id` int(11) NOT NULL auto_increment,
`emp_fname` char(50) NOT NULL,
`emp_sname` char(50) NOT NULL,
`emp_patr` char(50) NOT NULL,
`emp_birth` datetime NOT NULL default CURRENT_TIMESTAMP,
`isInCharge` boolean default NULL,
`depart_id` int(11) NOT NULL,
PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=cp1251;
CREATE TABLE IF NOT EXISTS `department` (
`depart_id` int(11) NOT NULL auto_increment,
`depart_name` char(50) NOT NULL,
PRIMARY KEY (`depart_id`),
FOREIGN KEY (depart_id) REFERENCES employees (depart_id) on delete
cascade on update cascade
) ENGINE=InnoDB DEFAULT CHARSET=cp1251;
Cannot add foreign key constraint
What is going wrong? I need to make possible for many employees to be in one department.
Also, how can I generate a random birth date from 1950 to 1990?
Your foreign key is in the wrong place. It belongs in the employees table not the department table. And it should be:
FOREIGN KEY (depart_id) REFERENCES department(depart_id) on delete cascade on update cascade
This should be easy to remember. You define the primary key in the table where the column is unique. You define the foreign key in all tables that refer to that primary key.
It should be the other way around right? dept_Id in Employee shall be foriegn key referencing the primary key Dept_ID of Department.

Proper use of ON CASCADE UPDATE?

While trying to run the following query:
UPDATE Flight
SET FLNO = '1001'
WHERE FLNO = '1000';
I receive the following error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`user/FlightLegInstance`, CONSTRAINT `FlightLegInstance_ibfk_1` FOREIGN KEY (`FLNO`) REFERENCES `FlightLeg` (`FLNO`) ON UPDATE CASCADE)
The following are my creation queries:
Flight:
CREATE TABLE Flight (
FLNO INTEGER NOT NULL,
Meal varchar(50) NOT NULL,
Smoking char(1) NOT NULL,
PRIMARY KEY (FLNO)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
FlightLeg:
CREATE TABLE FlightLeg (
FLNO INTEGER NOT NULL,
Seq char(25) NOT NULL,
FromA char(3) NOT NULL,
ToA char(3) NOT NULL,
DeptTime DATETIME NOT NULL,
ArrTime DATETIME NOT NULL,
Plane INTEGER NOT NULL,
PRIMARY KEY (FLNO, Seq),
FOREIGN KEY (FLNO) REFERENCES Flight(FLNO) ON UPDATE CASCADE,
FOREIGN KEY (FromA) REFERENCES Airport(Code),
FOREIGN KEY (ToA) REFERENCES Airport(Code)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
FlightLegInstance:
CREATE TABLE FlightLegInstance (
Seq char(25) NOT NULL,
FLNO INTEGER NOT NULL,
FDate DATE NOT NULL,
ActDept DATETIME NOT NULL,
ActArr DATETIME NOT NULL,
Pilot INTEGER NOT NULL,
PRIMARY KEY (Seq, FLNO, FDate),
FOREIGN KEY (FLNO) REFERENCES FlightLeg(FLNO) ON UPDATE CASCADE,
FOREIGN KEY (FLNO, FDate) REFERENCES FlightInstance(FLNO, FDate) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I would assume that the error is in one of the two FK definitions of FlightLegInstance, but I'm not sure. Can anyone help me with this?
Thanks.
this is not working just because you have defind [flno] column in second table as foriegn key
the solution of this problem is you have to update it at both places
thanks

Cannot add or update a child row: a foreign key constraint fails mysql

CREATE TABLE `class` (
`class_id` int(11) NOT NULL AUTO_INCREMENT,
`section_name` varchar(50) NOT NULL,
`class_alias` varchar(200) NOT NULL,
`grading_scheme` int(11) NOT NULL DEFAULT '0',
`year` year(4) NOT NULL,
`grade_calc_method_id` varchar(20) DEFAULT NULL,
PRIMARY KEY (`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=48819 DEFAULT CHARSET=latin1;
CREATE TABLE `teachers` (
`teacher_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`teacher_subject` varchar(20) NOT NULL DEFAULT 'None',
PRIMARY KEY (`teacher_id`),
KEY `user_id` (`user_id`,`school_id`)
) ENGINE=InnoDB AUTO_INCREMENT=48606 DEFAULT CHARSET=latin1;
CREATE TABLE `teacher_classes` (
`teacher_class_id` int(11) NOT NULL AUTO_INCREMENT,
`teacher_id` int(11) NOT NULL,
`class_id` int(11) NOT NULL,
PRIMARY KEY (`teacher_class_id`),
UNIQUE KEY `teacher_id_class_id` (`teacher_id`,`class_id`),
KEY `teacher_id` (`teacher_id`,`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=46707 DEFAULT CHARSET=latin1;
Trying to insure data consistency between the tables by using foreign key so that the DBMS can check for errors.I have another junction table teacher_classes
Here is my query to add foreign keys constraint
ALTER TABLE teacher_classes
ADD CONSTRAINT `tc_fk_class_id` FOREIGN KEY (`class_id`)
REFERENCES class (`class_id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
ADD CONSTRAINT `tc_fk_teacher_id` FOREIGN KEY (`teacher_id`)
REFERENCES teachers (`teacher_id`) ON UPDATE NO ACTION ON DELETE NO ACTION;
've seen the other posts on this topic, but no luck, getting following error.
Cannot add or update a child row: a foreign key constraint fails
(DB_NAME.#sql-403_12, CONSTRAINT
tc_fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teachers
(teacher_id) ON DELETE NO ACTION ON UPDATE NO ACTION)
Too late to Answer. I just had the same problem the solution is easy.
You're getting this error because you're trying to or UPDATE a row to teacher_classes doesn't match the id in table teachers.
A simple solution is disable foreign key checks before performing any operation on the table.
SET FOREIGN_KEY_CHECKS = 0;
After you are done with the table enable it again.
SET FOREIGN_KEY_CHECKS = 1;
Or you can remove not null constraint and insert a NULL value in it.
That's most probably the column definition doesn't match properly. For table teachers the PK column definition is as below.
`teacher_id` int(11) NOT NULL AUTO_INCREMENT
Make sure you have the same definition in your child table teacher_classes

Error 1215: Cannot add foreign key constraint SQL Statement

Not sure why I am still encountering the issue "Error 1215" wherein they have the same data type and parent table is in primary key.
child table:
CREATE TABLE `customer_notice_type` (
`CUSTOMER_NOTICE_TYPE_ID` int(11) NOT NULL AUTO_INCREMENT,
`CUSTOMER_ID` int(11) NOT NULL,
`CUSTOMER_NOTICE_TYPE_NAME` varchar(50) NOT NULL,
`SYSTEM_NOTICE_TYPE_ID` int(11) NOT NULL,
`STATUS` char(1) NOT NULL,
`CREATED_BY` varchar(50) NOT NULL,
`CREATED_DATE` datetime NOT NULL,
`MODIFIED_BY` varchar(50) DEFAULT NULL,
`MODIFIED_DATE` datetime DEFAULT NULL,
PRIMARY KEY (`CUSTOMER_NOTICE_TYPE_ID`),
KEY `fk_customer_id_customer_notice_type_idx` (`CUSTOMER_ID`),
CONSTRAINT `fk_customer_id_customer_notice_type` FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `customer` (`CUSTOMER_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=494 DEFAULT CHARSET=latin1;
parent table:
CREATE TABLE `system_notice_type` (
`SYSTEM_NOTICE_TYPE_ID` int(11) NOT NULL,
`SYSTEM_NOTICE_TYPE_NAME` varchar(45) NOT NULL,
`LINE_OF_BUSINESS_ID` int(11) NOT NULL,
`STATUS` char(1) NOT NULL,
PRIMARY KEY (`SYSTEM_NOTICE_TYPE_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;
SQL script to create Foreign Key:
ALTER TABLE `fexpress`.`customer_notice_type`
ADD CONSTRAINT `fk_system_notice_type_customer_notice_type`
FOREIGN KEY (`SYSTEM_NOTICE_TYPE_ID`)
REFERENCES `fexpress`.`system_notice_type` (`SYSTEM_NOTICE_TYPE_ID`)
ON DELETE CASCADE ON UPDATE CASCADE;
You have two potential problems. First, the alter table statement references fexpress. This may or may not be the correct schema for the table. So, that is one potential problem.
The second real problem is the constraint defined in the child table:
CONSTRAINT `fk_customer_id_customer_notice_type` FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `customer`(`CUSTOMER_ID`) ON DELETE CASCADE ON UPDATE CASCADE
The parent table is not yet defined, so it generates an error.
Removing this row and adjusting the schema name results in working code, as in this SQL Fiddle.

Error #1452 when altering mySQL table to add foreign key

Trying to help an intern with her project. She wants to add foreign keys to an existing table but this query:
ALTER TABLE `document`
ADD CONSTRAINT `document_ibfk_1` FOREIGN KEY (`cle_author`)
REFERENCES `author` (`id_author`)
ON DELETE CASCADE
ON UPDATE CASCADE;
gives this error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`wrc_mysql`.<result 2 when explaining filename '#sql-30e4_7000d'>, CONSTRAINT `document_ibfk_1` FOREIGN KEY (`cle_author`) REFERENCES `author` (`id_author`) ON DELETE CASCADE ON UPDATE CASCADE)
Schema are like so
CREATE TABLE `document` (
`id_document` int(11) NOT NULL AUTO_INCREMENT,
`abstract` text,
`number_of_pages` int(10) DEFAULT NULL,
`original_surrey_citation` varchar(255) DEFAULT NULL,
`updated_citation` varchar(255) DEFAULT NULL,
`library_of_congress` varchar(10) DEFAULT NULL,
`cross_citation` varchar(50) DEFAULT NULL,
`doc_type` varchar(255) DEFAULT NULL,
`questions` varchar(255) DEFAULT NULL,
`keywords` varchar(255) DEFAULT NULL,
`cle_author` int(10) NOT NULL,
PRIMARY KEY (`id_document`),
KEY `cle_author` (`cle_author`)
) ENGINE=InnoDB AUTO_INCREMENT=22591 DEFAULT CHARSET=utf8
CREATE TABLE `author` (
`id_author` int(10) NOT NULL AUTO_INCREMENT,
`author_name` varchar(255) DEFAULT NULL,
`sender_office` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id_author`),
KEY `author_name` (`author_name`,`sender_office`)
) ENGINE=InnoDB AUTO_INCREMENT=22591 DEFAULT CHARSET=utf8
Anyone know what is going wrong?
You probably have inconsistent data between your two tables. This error means that you have a cle_author value in your document table that doesn't have a corresponding entry in the author table. Since the cle_author value is going to be set up as a foreign key, each value for that field must have a corresponding entry in the author table's id_author field.
Per this page: Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails
checked intern's data with
SELECT cle_author FROM document doc
LEFT JOIN author a ON doc.cle_author=a.id_author
WHERE a.id_author IS NULL;
And found ALL of her cle_author data is bogus and does not hold valid references to id_author values.