16

我目前正在尝试在 MySQL 中创建一个自引用表,但是似乎我无法在表本身上创建外键,我收到了 MySQL 错误:

Error Code: 1005. Can't create table 'biological classification' (errno: 150)

这是我的代码:

# Table creation
DROP TABLE IF EXISTS `biological classification`;
CREATE TABLE `biological classification` (
  `idBC` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `idParent` int(11) DEFAULT NULL,
  `type` varchar(45) DEFAULT NULL,
  `value` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`idBC`),
  UNIQUE KEY `idnew_table_UNIQUE` (`idBC`),
  CONSTRAINT `SelfKey` FOREIGN KEY (`idParent`) REFERENCES `biological classification` (`idBC`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1 COMMENT='A table that contains the Biological Classification of anima';

# insert into table
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (1,NULL,'Class','Mammalia');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (2,1,'Genus','Giraffa');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (3,2,'Species','Giraffa camelopardalis');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (5,1,'Genus','Panthera');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (6,1,'Genus','Loxodonta');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (7,5,'Species','Panthera leo');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (8,5,'Species','Panthera tigris');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (9,6,'Species','Loxodonta africana');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (10,1,'Class','Marsupialia');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (11,10,'Genus','Macropus');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (12,11,'Species','Macropus rufus');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (13,1,'Genus','Sarcophilus');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (14,13,'Species','Sarcophilus harrisii');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (15,10,'Genus','Didelphis');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (16,15,'Species','Didelphis virginiana');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (17,NULL,'Class','Aves');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (18,17,'Genus','Aquila');
INSERT INTO `biological classification` (`idBC`,`idParent`,`type`,`value`) VALUES (19,18,'Species','Aquila chrysaetos');
4

2 回答 2

20

将类型从 column 更改idParentint(10) unsigned。所以它与引用的列是同一类型idBC

DROP TABLE IF EXISTS `biological classification`;
CREATE TABLE `biological classification` (
  `idBC` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `idParent` int(10) unsigned DEFAULT NULL,
  `type` varchar(45) DEFAULT NULL,
  `value` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`idBC`),
  UNIQUE KEY `idnew_table_UNIQUE` (`idBC`),
  CONSTRAINT `SelfKey` FOREIGN KEY (`idParent`) REFERENCES `biological classification` (`idBC`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1 COMMENT='A table that contains the Biological Classification of anima';
于 2011-08-29T12:42:41.447 回答
12

为什么你会在表名中放一个空格???

biological_classification用下划线而不是空格来称呼它。

然后使两列使用相同的数据类型,两者int(10)或两者都使用int(11),并使它们同时使用signed或两者都使用unsigned(编辑)。

于 2011-08-29T12:43:18.883 回答