The MySQL manual tells us that regardless of whether or not we
use “SET FOREIGN_KEY_CHECKS=0″ before making schema changes,
InnoDB will not allow a column referenced by a foreign key
constraint to be modified in such a way that the foreign key will
reference a column with a mismatched data type. For instance, if
we have these two tables:
CREATE TABLE foo (
i INT NOT NULL PRIMARY KEY,
j INT NOT NULL,
INDEX(j),
FOREIGN KEY (j) REFERENCES bar(j)
) ENGINE=INNODB;
CREATE TABLE bar (
i INT NOT NULL PRIMARY KEY,
j INT NOT NULL,
INDEX(j)
) ENGINE=INNODB;
trying to do something like “ALTER TABLE bar DROP j” or “ALTER
TABLE bar MODIFY COLUMN j j SMALLINT NOT NULL” will produce an
error unless we first remove the foreign key constraint present
in table “foo”. Indeed, if we try it, that’s exactly what
happens:
(root@localhost) [foobar]> ALTER TABLE bar drop j;
ERROR 1025 (HY000): Error on rename of …
[Read more]