mysql foreign key 出错_mysql – 添加FOREIGN KEY时出错“无法创建表…”

  • Post author:
  • Post category:mysql


我已经通过脚本创建了表:

SET SQL_MODE=”NO_AUTO_VALUE_ON_ZERO”;

DROP TABLE IF EXISTS `Table1`;

CREATE TABLE IF NOT EXISTS `Table1` (

`id` bigint(20) NOT NULL AUTO_INCREMENT,

`parentId` bigint(20) DEFAULT NULL,

`name` varchar(1024) NOT NULL,

`description` varchar(16384) NOT NULL DEFAULT ”,

`imageId` bigint(20) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `name` (`name`(255)),

KEY `parentId` (`parentId`),

KEY `imageId` (`imageId`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=27 ;

INSERT INTO `Table1` (`id`, `parentId`, `name`, `description`, `imageId`) VALUES

(0, NULL, ‘name1’, ”, NULL),

(12, 0, ‘name2’, ”, NULL);

然后我尝试添加外键:

ALTER TABLE `Table1`

ADD CONSTRAINT `Table1_ibfk_2`

FOREIGN KEY (`parentId`) REFERENCES `Table1` (`id`);

并获得以下错误:

ERROR 1005 (HY000): Can’t create table ‘sandbox.#sql-c28_4c’ (errno: 150)

怎么了?

我跑

SHOW ENGINE INNODB STATUS;

有最新的外键错误如下:

————————

LATEST FOREIGN KEY ERROR

————————

110504 22:06:55 Error in foreign key constraint of table sandbox/#sql-c28_61:

FOREIGN KEY (`parentId`) REFERENCES `Table1` (`id`):

Cannot resolve table name close to:

(`id`)

————

但它并没有帮助我认识到什么是错的.

我使用Windows Vista,MySql 5.5.11

更新:

从MySql 5.0.67升级时出现问题.



版权声明:本文为weixin_39563827原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。