MySQL: FK constraint checked between empty tables when truncating - mysql

I was truncating tables like this
TRUNCATE TABLE `enterprise_url_rewrite`; -- error
TRUNCATE TABLE `enterprise_catalog_category_rewrite`; -- works fine
in that order or reversed when I got this error.
Cannot truncate a table referenced in a foreign key constraint
(`magento`.`enterprise_catalog_category_rewrite`, CONSTRAINT
`FK_415B32DA3DF924D5C803CF24EB3AC1D9` FOREIGN KEY (`url_rewrite_id`) REFERENCES
`magento`.`enterprise_url_rewrite` (`url_rewrite_id`)
I thought foreign key constraints were enforced to prevent deletion when there are actual values being referenced. I recollect (I could be wrong) being able to TRUNCATE this table previously without disabling key checks. Note that DELETE FROM enterprise_url_rewrite works.
More info on the constraint definition on enterprise_catalog_category_rewrite.
KEY `FK_744D72D1D79D148B7C2542E53B0370B5` (`url_rewrite_id`),
CONSTRAINT `FK_744D72D1D79D148B7C2542E53B0370B5` FOREIGN KEY (`url_rewrite_id`)
REFERENCES `enterprise_url_rewrite` (`url_rewrite_id`) ON DELETE CASCADE ON UPDATE NO ACTION

Related

How to delete a foreign key thats references multiple columns in mySQL

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.

MySql workbench: add Foreign key error

I am trying to add foreign key 'USERNAME' in tutorial table, but there was an error.
Executing:
ALTER TABLE `databse`.`tutorial`
ADD CONSTRAINT `USERNAME`
FOREIGN KEY (`USERNAME`)
REFERENCES `databse`.`register` (`USERNAME`)
ON DELETE CASCADE
ON UPDATE CASCADE;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1452: Cannot add or update a child row:
a foreign key constraint fails (`databse`.`#sql-e7c_5`, CONSTRAINT `USERNAME` FOREIGN KEY (`USERNAME`)
REFERENCES `register` (`USERNAME`) ON DELETE CASCADE ON UPDATE CASCADE)
SQL Statement:
ALTER TABLE `databse`.`tutorial`
ADD CONSTRAINT `USERNAME`
FOREIGN KEY (`USERNAME`)
REFERENCES `databse`.`register` (`USERNAME`)
ON DELETE CASCADE
ON UPDATE CASCADE
Foreign key setting:
Tutorial table setting:
Any ideas ? thank you
I solved it, i created a new 'tutorials' table replace 'tutorial' table, and use same way to add foreign key, it worked! = =
still thank you for your helps !!
as stated here :
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.
In the code you show : you try to reference the "USERNAME" column, wich is not a primary key in your last capture
So you can either change your primary key in the register table to USERNAME, or you can change the foreign key to reference TutorialName
You have two table one is child table and second is parent table .So you would need to guarantee that each child column has NULL or has values that present in parent column.
This problem is normally caused by mismatching of values presented in the two columns constrained by the new foreign key.
That is, the value presented in the child table does not have a reference presented in the parent table.
when creating a foreign key, you need to make sure that:
the table type supports foreign key
there is a index on the foreign key column
the types of data of the two columns constrained by a foreign key are similar enough so that they can be converted to each other
the data presented in both columns constrained by a foreign key is consistent.

MY SQL Foreign Key Constraint Error

I m using MySQL database as back end for my application. I was playing with database and i did something wrong couple of days ago and now i am not able to apply foreign key on my DB Table.
I have 2 tables shoplocation and pizaorderdetail. In pizzaordrdetail i am passing id from shoplocation as foreign key. And i am getting following error. The table already contains data in it. And earlier i had foreign key constraint available on pizzaorderdetail but somehow it got deleted. Please help me out how to resolve this error.
Executing:
ALTER TABLE order.pizzaorderdetail
ADD INDEX FK_idx (LocationID ASC);
ALTER TABLE order.pizzaorderdetail
ADD CONSTRAINT FK
FOREIGN KEY (LocationID)
REFERENCES order.location (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (order.#sql-714_31, CONSTRAINT FK FOREIGN KEY (LocationID) REFERENCES location (id) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
ALTER TABLE order.pizzaorderdetail
ADD CONSTRAINT FK
FOREIGN KEY (LocationID)
REFERENCES order.location (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
You need to check if your FK is ok. When I recieved this message, I just deleted my FK's and it was solved.
According to manual: https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
InnoDB reports this error when you attempt to drop the last index that
can enforce a particular referential constraint.
For optimal performance with DML statements, InnoDB requires an index
to exist on foreign key columns, so that UPDATE and DELETE operations
on a parent table can easily check whether corresponding rows exist in
the child table. MySQL creates or drops such indexes automatically
when needed, as a side-effect of CREATE TABLE, CREATE INDEX, and ALTER
TABLE statements.
When you drop an index, InnoDB checks if the index is used for
checking a foreign key constraint. It is still OK to drop the index if
there is another index that can be used to enforce the same
constraint. InnoDB prevents you from dropping the last index that can
enforce a particular referential constraint.

Why does MySQL give me a foreign key constraint error on a table that I don't have?

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.

How to alter Foreign Keys in phpMyAdmin

I have set up a Foreign Key in my mysql database with:
ALTER TABLE `gameplayers` ADD CONSTRAINT `FK_GAMENUMBER` FOREIGN KEY (`GameNumber`) REFERENCES `games`(`GameNumber`) ON UPDATE CASCADE ON DELETE CASCADE;
However, I am not sure I want the ON UPDATE and ON DELETE anymore.
So I go into my phpAdmin, click on the edit pencil icon in the Index section of the Structure Tab and I get this:
Warning: ("PRIMARY" must be the name of and only of a primary key!)
Do alterations just have to be done manually? Ie the pencil icon will just not work.
ALSO: Do foreign keys have the same speed bonus effect on mysql searches, similar to Indexes?
Foreign keys require indexes, so effectively, the foreign key constrain creates and index and it can be used to resolve queries just like normal indexes.
I'm not sure which version of phpMyAdmin you are using, I think foreign key constains are supported in newest versions, but it seems your does not list foreign key indexes, and the primary key is not what you are looking for. You can however modify the keys with plain SQL:
ALTER TABLE `gameplayers` DROP FOREIGN KEY FK_GAMENUMBER,
ADD CONSTRAINT `FK_GAMENUMBER` FOREIGN KEY (`GameNumber`) REFERENCES `games`(`GameNumber`) ON UPDATE NO ACTION ON DELETE NO ACTION;