我正在使用 MySQL,并且我有一个带有索引的表,该索引在许多其他表中用作外键。我想更改索引的数据类型(从有符号整数到无符号整数),最好的方法是什么?
我尝试更改索引字段上的数据类型,但失败了,因为它被用作其他表的外键。我尝试更改其中一个外键的数据类型,但失败了,因为它与索引的数据类型不匹配。
我想我可以手动删除所有外键约束,更改数据类型并重新添加约束,但这将是很多工作,因为我有很多表使用这个索引作为外键。有没有办法在进行更改时暂时关闭外键约束?另外,有没有办法获取引用索引作为外键的所有字段的列表?
更新: 我在关闭外键检查后尝试修改一个外键,但似乎并没有关闭检查:
SET foreign_key_checks = 0;
ALTER TABLE `escolaterrafir`.`t23_aluno` MODIFY COLUMN `a21_saida_id` INTEGER DEFAULT NULL;
这是错误:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
090506 11:57:34 Error in foreign key constraint of table escolaterrafir/t23_aluno:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match to the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
CONSTRAINT FK_t23_aluno_8 FOREIGN KEY (a21_saida_id) REFERENCES t21_turma (A21_ID)
索引表的定义:
DROP TABLE IF EXISTS `escolaterrafir`.`t21_turma`;
CREATE TABLE `escolaterrafir`.`t21_turma` (
`A21_ID` int(10) unsigned NOT NULL auto_increment,
...
) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=latin1;
以及具有指向它的外键的表:
DROP TABLE IF EXISTS `escolaterrafir`.`t23_aluno`;
CREATE TABLE `escolaterrafir`.`t23_aluno` (
...
`a21_saida_id` int(10) unsigned default NULL,
...
KEY `Index_7` (`a23_id_pedagogica`),
...
CONSTRAINT `FK_t23_aluno_8` FOREIGN KEY (`a21_saida_id`) REFERENCES `t21_turma` (`A21_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=387 DEFAULT CHARSET=latin1;