1

当我执行以下 SQL 命令时:

CREATE TABLE `TableA` (
  `tableAId` INT(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`tableAId`)
);

CREATE TABLE `TableB` (
  `tableBId` INT(11) NOT NULL AUTO_INCREMENT,
  `tableAId` INT(11) DEFAULT NULL,
  PRIMARY KEY (`tableBId`),
  CONSTRAINT `FK_TABLE_A_ID` FOREIGN KEY (`tableAId`) REFERENCES `TableA` (`tableAId`)
);

ALTER TABLE `TableB`
    RENAME TO `NewTableB`;

ALTER TABLE `TableA`
    RENAME TO `NewTableA`,
    CHANGE COLUMN `tableAId` `newTableAId` INT(11) NOT NULL AUTO_INCREMENT FIRST;

DROP TABLE IF EXISTS NewTableA;

DROP TABLE IF EXISTS NewTableB;

CREATE TABLE `TableA` (
  `tableAId` INT(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`tableAId`)
);

我在最后一个命令(即)上有以下错误CREATE TABLE TableA (...)

Erreur SQL (1005): 无法创建表 'TableA' (errno: 150) 外键约束的格式不正确

当我执行时,show engine innodb status我有:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
130531 12:06:05 Error in foreign key constraint of table TableB:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
  CONSTRAINT `FK_TABLE_A_ID` FOREIGN KEY (`tableAId`) REFERENCES `NewTableA` (`tableAId`)
4

1 回答 1

0

老问题,但对于处于类似情况的其他人,我的答案是:

https://dba.stackexchange.com/a/87587/56052

可能会有所帮助。

  • 使用与以前相同但名称不同的外键规范重新创建表。
  • 删除结果表(也将删除原始孤立外键)
  • 使用原始或没有外键重新创建表
于 2015-01-05T17:14:40.950 回答