I'm in the midst of modifying my current DB schema. I'd like to drop the child side of a one-to-one relationship, but I keep getting SQL errors about the foreign key restraint, specifically:
#1217 - Cannot delete or update a parent row: a foreign key constraint fails
Even though there doesn't seem to be any foreign key in play at the moment (the child table is completely empty).
Any ideas?
First, DROP the foreign key constraint from the referencing (child) table, and then DROP the referenced (parent) table.
For example:
ALTER TABLE child
DROP FOREIGN KEY FK_child_parent ;
DROP TABLE parent ;
(Obviously, you'd need to replace the "child", "parent" and "FK_child_parent" with the actual identifiers for your tables and foreign key constraint.
One easy way to get the name of the foreign key constraint is to use the SHOW CREATE TABLE statement,
SHOW CREATE TABLE child ;
The output from that will show the name of the foreign key constraint.
If you don't know which tables have a foreign key referencing the table, you can query the information_schema.referential_constraints table to find them
WHERE referenced_table_name = 'parent'
AND constraint_schema = 'mydatabase'
Have you tried
SET foreign_key_checks = 0;
//Do your drop statement
SET foreign_key_checks = 1;
?
Related
I have a foreign key that references two columns in a child table. Id like to delete the foreign key but mySQL just hangs and nothing happens:
Here is the output from SHOW CREATE TABLE table_name:
KEY FK_animal_index (animal_type,food_index),
CONSTRAINT FK_animal_index FOREIGN KEY (animal_type, food_index)
REFERENCES animal_schedules (animal_type, food_index)
ENGINE=InnoDB
I have tried deleting this foreign key using:
`ALTER TABLE table_name DROP FOREIGN KEY FK_animal_index;`
`ALTER TABLE table_name DROP FOREIGN KEY animal_type;`
ALTER TABLE section_configuration DROP FOREIGN KEY FK_animal_index,
DROP KEY (animal_type, food_index);
AND
ALTER TABLE section_configuration DROP FOREIGN KEYFK_animal_index, ADD CONSTRAINT FK_animal_type FOREIGN KEY (animal_type) REFERENCESanimal_schedules(animal_type) ON DELETE SET NULL;
Others have mentioned that ON DELETE not being set can prevent you changing key values (mySQL will reject the change if ON DELETE is not set to anything)
But to no avail, mySQL just hangs and 30 minutes later still nothing. The database is very small at the moment so removing the FK should be fast.
The answer to this question was not about primary keys at all.
I had to stop my program from accessing the table in order to make modifications to it.
In mySQL db, you can add tables and add columns at any time, but if you want to change a column or remove a column, it must not be in use by any program or it will hang indefinitely.
I mistakenly added two foreign keys in one table in MySQL even though I proceeded in the described manner to create one foreign key only. But that aside, the problem is that I need to remove the foreign key which is NOHOW related to the main table in the database which the primary keys are referenced from.
That is, I need to remove the field CId from customer table.
Tables' image
You may need to first use
SET FOREIGN_KEY_CHECKS=0;
then you can drop the foreign key
ALTER TABLE <TABLE_NAME> DROP CONSTRAINT <FOREIGN_KEY_NAME>
And then make sure you set foreign keys back using
SET FOREIGN_KEY_CHECKS=1;
I'm running a series of upgrade scripts. As a note, in a an earlier script, there were many of these:
ALTER TABLE files
DROP FOREIGN KEY files_ibfk_1,
DROP FOREIGN KEY files_ibfk_2;
However, those keys did not exist….
Now I'm getting an error…:
Cannot add or update a child row: a foreign key constraint fails (pacsdbcmi.#sql-536_77, CONSTRAINT #sql-536_77_ibfk_1 FOREIGN KEY (series_fk) REFERENCES series (pk))
What is #sql-536_77 ? Can someone explain what this means? I do not have a table named #sql-536-77, nor a key in series called #sql-536_77_ibfk_1
Thanks
This is because there are another tables which is foreign to the parent table.CASCADE relation should be there.So before you ALTER the parent table foreign key please remove existing CASCADES
OR
USE
SET foreign_key_checks = 0;
AND
ALTER TABLE files DROP FOREIGN KEY files_ibfk_1, DROP FOREIGN KEY files_ibfk_2;
THEN
SET foreign_key_checks = 1;
By setting the foreign key check to 0, you can able to ALTER table. Once It was done with operations on the table, you can reset the key check to 1 again and everything is back in place now.
I have a pretty standard MySQL-query in my winform application like this:
SELECT * FROM tblTable WHERE tblTable.tableID = " & variable
One of the fields is a foreign key. And when I try to add a record to my datagridview I get the error:
Cannot add or update a child row: a foreign key constraint fails
Can't MySQLCommandBuilder make INSERT-commands with tables containing foreign keys?
A foreign key constraint is there to prevent behavior like this (for the sake of the integrity of the database). You are probably trying to insert a value that reference another table by a primary key that doesn't exist.
Try changing your foreign key value to a existing primary key within the other table.
You can force the insert the record with this, but is not recommended
SET FOREIGN_KEY_CHECKS = 0;
your select statement;
SET FOREIGN_KEY_CHECKS = 1;
Foreign key check is to avoid inconsistency between tables, the correct way to do this is: first insert the foreing key in the main table then add the record on the child table
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.