Here are 2 tables.
Full size image
I'm trying to create relationship between them by creating foreign keys courses:parent<->child.parent_cid <=> courses.id and courses:parent<->child.child_cid <=> courses.id
SQL looks like that
ALTER TABLE `courses: parent<->child` ADD CONSTRAINT `cpc.parent_cid_courses.id` FOREIGN KEY (`parent_cid`) REFERENCES `courses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `courses: parent<->child` ADD CONSTRAINT `cpc.child_cid_courses.id` FOREIGN KEY (`child_cid`) REFERENCES `courses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Getting this error message
What am I doing wrong? Any suggestions?
My first suggestion: rename the child table and the foreign key constraints using only alphanumeric characters and underscores.
The error message implies that there are invalid foreign key values in the child table. You can tell the MySQL server to ignore those values like this before running the ALTER TABLE statements:
set foreign_key_checks = 0;
Or you can fix the data by either adding the missing parent rows or deleting the invalid child rows before adding the constraints.
You are trying to add foreign keys. The error means that child table has data which doesn't exist in parent table.
In your case cpc.parent_cid_courses.id.parent_cid has wrong values, there are no corresponding values in parent field courses.id.
Related
here is my table structure
I would like that parent_forum would save an id of parent forum, which is the ID of the same table id column value. As you can see both columns have the same type. My table engine is InnoDB, I try the following query to add a constraint.
ALTER TABLE `forums` ADD CONSTRAINT `parent_forum constraint` FOREIGN KEY (`id`) REFERENCES `codeigniter`.`forums`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
and I get an error that is written on the title.
What is actually wrong here? Category_id successfully works with other table ID value.
I think this is the syntax you are looking for:
ALTER TABLE forums ADD CONSTRAINT parent_forum_constraint
FOREIGN KEY (parent_forum) REFERENCES codeigniter.forums(id)
ON DELETE RESTRICT ON UPDATE RESTRICT;
The column in parentheses is the one that refers to the column after the references. Also, don't put spaces in names, unless you have a really good reason. Code is much more readable without all the backticks.
I have two tables in my database :
-etudiant(id(PK),nom,prenom,tel,adresse,filiere(FK))
-filiere(id(PK),libelle)
In the etudiant table I have a foreign key filiere which references the table filiere.
Sometimes I want to add records to etudiant table without specifing the foreign key filiere, but I get this error message when I do that :
Cannot add or update a child row: a foreign key constraint fails
(gecole.etudiant, CONSTRAINT etudiant_ibfk_1 FOREIGN KEY
(filiere) REFERENCES filiere (id) ON DELETE CASCADE ON UPDATE
CASCADE)
You're getting this error because you're trying to add/update a row to etudiant table that does not have a valid value for the etudiant_ibfk_1 field based on the values currently stored in filiere. If you post some more code I can help you diagnose the specific cause.
Update :
Try DISABLE KEYS or
SET FOREIGN_KEY_CHECKS=0;
make sure to
SET FOREIGN_KEY_CHECKS=1;
after.
I have 2 tables :
Installers (Fields: id,company,country,experience,name)
Contacts (Fields: name,phone,address)
I would like to match both names, thereby I could click in one value of the name of Installers and it could show me the values of Contacts table.
However when I am trying to set up the foreign key (my child table will probably be Installers , as I have more tables like that and Contacts would be the parent.) It states this error:
query SQL:
ALTER TABLE `Installers`
ADD FOREIGN KEY (`name`)
REFERENCES `SOLAR_PV`.`Contacts`(`name`)
ON DELETE CASCADE ON UPDATE CASCADE;
MySQL ha dicho: Documentación
1452 - Cannot add or update a child row: a foreign key constraint
fails (SOLAR_PV.#sql-32a_183, CONSTRAINT #sql-32a_183_ibfk_1
FOREIGN KEY (name) REFERENCES Contacts (name) ON DELETE CASCADE
ON UPDATE CASCADE)
Both tables are InnoDB and Contacts.name is indexed as well as Installers.name
Primary Key of Installers is id and Primary Key of Contacs is name.
Any idea about what would be the problem?
It seems your child table contains few records those don't have in master, you can check it by below query-
SELECT id FROM Installers ins
LEFT JOIN SOLAR_PV.Contacts cnt ON ins.name=cnt.name
WHERE cnt.name IS NULL;
Note: Assuming name is int type for better performance as it is primary key in one table.
If you get few records by above query then you can follow below 2 approach-
Approach1: You can either delete these records in child table or insert in master table also and then you can create relationship by this alter command.
Approach2: If you don't want to change in your tables existing data and still want to execute your alter query then use as per below-
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE `Installers`
ADD FOREIGN KEY (`name`)
REFERENCES `SOLAR_PV`.`Contacts`(`name`)
ON DELETE CASCADE ON UPDATE CASCADE;
SET FOREIGN_KEY_CHECKS=1;
I am trying to update two columns in a xref database. I am getting this error message:
Cannot add or update a child row: a foreign key constraint fails (`globaldetroit`.`org_cult_xref`, CONSTRAINT `org_cult_xref_ibfk_1` FOREIGN KEY (`org_id`) REFERENCES `organization` (`org_id`) ON DELETE CASCADE ON UPDATE CASCADE)
I want to be able to have a many-many relationship, and these errors seem to prevent me from having one.
EDIT:
That is very odd! There most certainly is a column org_id with the value of "6" (as an integer) in the table organization! I just checked!
You are trying to set a value that has a foreign key constrant -- ie the key does not exist in the foreign table.
So globaldetroit's org_cult_xref references an org_id in organization that does not exist.
you're putting in field globaldetroit.org_cult_xref value not existing in organization.org_id
Many-many relationship is bad, don't go there.
Your error seems to be caused because the row you insert in org_cult_xref has a column org_id and the value you insert there cannot be found in the organization table.
You have a foreign key defined on the table you are trying to insert/update into, which basically says the value in org_id should exist in the organization table, and it is not the case.
I have two tables, Parent and Child. The column Parent.favorite_child has a foreign key constraint pointing to Child.id. And the column Child.parent has a foreign key constraint pointing to Parent.id.
Now here's the weird part. I'm trying to drop my Child table, which contains no records, but MySQL is giving me the error:
ERROR 1217: Cannot delete or update a parent row: a foreign key constraint fails
SQL Statement:
drop table `mydatabase`.`Child`
Why is MySQL throwing this error? There are literally no records in the Child table with which anything could be pointing to or from.
You need to first drop the foreign key on the parent table before you can delete the child table:
ALTER TABLE `Parent` DROP FOREIGN KEY `Parent_ibfk_1` ;
I'd try dropping the foreign key constraint first
you might also want to try the command "show engine innodb status" - it may indicate if there is some leftover problem from when the tables had data.
Try to delete the Foreign key constraints from your table.
try to do this as shown in image
u could see that there are foreign key constraints in yellow shading
just right click on that key section and delete the foreign key like this.
inside red section the image and after that click okk to delete foreign key and that's it now you can delete the column by using simple
AlTER table table_name drop column column_name;
thank you.