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.
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.
what does the below error mean
{"info":{"error":{"errorInfo":["23000",1452,"Cannot add or update a child row: a foreign key constraint fails (`cs_test_db`.`accountinfo`, CONSTRAINT `fk_accinfo_accttypeid` FOREIGN KEY (`account_type_id`) REFERENCES `accounttype` (`accounttype_id`) ON DELETE NO ACTION ON UPDATE NO)"]},"unique_code":""}}
The error means that you are trying to update data in a table that have a constraint of foreign key. Actually, your current table is linked to another table that have a column with data that is the only ones to be present in it.
In order to see the link, please run this command: SHOW ENGINE IMMODB STATUS; and search for "Latest foreign key error". It will show you the parent table. Or run this other command: SHOW CREATE TABLE Accountinfo; to see the foreign key constraint with parent table.
Then, do a select on the parent table and check the data on the column you are using as a constraint... data values present are different from what you are trying to update.
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;
?
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.
So I am trying to add a primary key to one of the tables in my database. Right now it has a primary key like this:
PRIMARY KEY (user_id, round_number)
Where user_id is a foreign key.
I am trying to change it to this:
PRIMARY KEY (user_id, round_number, created_at)
I am doing this in phpmyadmin by clicking on the primary key icon in the table structure view.
This is the error I get:
#1025 - Error on rename of './database/#sql-2e0f_1254ba7' to './database/table' (errno: 150)
It is a MySQL database with InnoDB table engine.
There is probably another table with a foreign key referencing the primary key you are trying to change.
To find out which table caused the error you can run SHOW ENGINE INNODB STATUS and then look at the LATEST FOREIGN KEY ERROR section.
As was said you need to remove the FKs before. On Mysql do it like this:
ALTER TABLE `table_name` DROP FOREIGN KEY `id_name_fk`;
ALTER TABLE `table_name` DROP INDEX `id_name_fk`;
For those who are getting to this question via google... this error can also happen if you try to rename a field that is acting as a foreign key.
To bypass this in PHPMyAdmin or with MySQL, first remove the foreign key constraint before renaming the attribute.
(For PHPMyAdmin users: To remove FK constrains in PHPMyAdmin, select the attribute then click "relation view" next to "print view" in the toolbar below the table structure)
If you are trying to delete a column which is a FOREIGN KEY, you must find the correct name which is not the column name. Eg: If I am trying to delete the server field in the Alarms table which is a foreign key to the servers table.
SHOW CREATE TABLE alarm;
Look for the CONSTRAINT `server_id_refs_id_34554433` FORIEGN KEY (`server_id`) REFERENCES `server` (`id`) line.
ALTER TABLE `alarm` DROP FOREIGN KEY `server_id_refs_id_34554433`;
ALTER TABLE `alarm` DROP `server_id`
This will delete the foreign key server from the Alarms table.
I had this problem, it is for foreign-key
Click on the Relation View (like the image below) then find name of the field you are going to remove it, and under the Foreign key constraint (INNODB) column, just put the select to nothing! Means no foreign-key
Hope that works!
If you are adding a foreign key and faced this error, it could be the value in the child table is not present in the parent table.
Let's say for the column to which the foreign key has to be added has all values set to 0 and the value is not available in the table you are referencing it.
You can set some value which is present in the parent table and then adding foreign key worked for me.