Question

I'm trying to write a script that change engine of certain table to MEMORY. The script look like:

Echo SET FOREIGN_KEY_CHECKS=0; > %TmpFile%
Echo ALTER TABLE `%Database%`.`compare_review` ENGINE = MEMORY; >> %TmpFile%
Echo SET FOREIGN_KEY_CHECKS=1; >> %TmpFile%

mysql -h %host% -u %User% -p%Pwd% < %TmpFile%

When I run this script, I got the error: Cannot delete or update a parent row: a foreign key constraint fails

I wonder what causes this error because the sql file contains command: SET FOREIGN_KEY_CHECKS=0;

I'm sure FOREIGN_KEY_CHECKS = 0 because I try to put a command Select @@FOREIGN_KEY_CHECKS to check, too.

Was it helpful?

Solution

You can't change the engine for the table to one which doesn't support foreign keys without removing the foreign key constraints.

Documentation for the MEMORY engine

If the new engine doesn't support them, they simply can't exist, so you need to remove them before making the appropriate changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top