MySQL没有用errno 105创建的外键和表

时间:2014-11-23 21:26:55

标签: mysql innodb

我试图使用以下SQL将一些表添加到数据库,问题是它是否正在创建第一个表,但是引发了错误:

[Err] 1005 - Can't create table 'BigBlockStudios_woodcraft.sbb_categories' (errno: 150)

当我尝试运行它时。

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `sbb_catalog`
-- ----------------------------
DROP TABLE IF EXISTS `sbb_catalog`;
CREATE TABLE `sbb_catalog` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `sku` varchar(50) NOT NULL,
  `category` int(8) NOT NULL,
  `type` int(8) NOT NULL,
  `make` int(8) NOT NULL,
  `model` int(8) NOT NULL,
  `year` int(8) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX (`category`,`type`,`make`,`model`,`year`),
  FOREIGN KEY (`category`) REFERENCES sbb_categories(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  FOREIGN KEY (`type`) REFERENCES sbb_type(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  FOREIGN KEY (`make`) REFERENCES sbb_make(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  FOREIGN KEY (`model`) REFERENCES sbb_model(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  FOREIGN KEY (`year`) REFERENCES sbb_year(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Table structure for `sbb_categories`
-- ----------------------------
DROP TABLE IF EXISTS `sbb_categories`;
CREATE TABLE `sbb_categories` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `category` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Table structure for `sbb_makes`
-- ----------------------------
DROP TABLE IF EXISTS `sbb_makes`;
CREATE TABLE `sbb_makes` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `make` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Table structure for `sbb_models`
-- ----------------------------
DROP TABLE IF EXISTS `sbb_models`;
CREATE TABLE `sbb_models` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `model` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Table structure for `sbb_vehicle_types`
-- ----------------------------
DROP TABLE IF EXISTS `sbb_vehicle_types`;
CREATE TABLE `sbb_vehicle_types` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `type` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Table structure for `sbb_years`
-- ----------------------------
DROP TABLE IF EXISTS `sbb_years`;
CREATE TABLE `sbb_years` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `year` decimal(4,0) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

我第一次尝试使用关系构建创建innodb表[试图学习一些YII]

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

我很确定原因是由于id中的sbb_categories列与category中的sbb_catalog列之间的数据类型不匹配。从前者中删除unsigned或将其添加到后者。 所有其他外键引用也是如此。

正如@Mihai在评论中指出的那样,你也有不匹配的名字 - 这显然需要改变,例如:

FOREIGN KEY (`type`) REFERENCES sbb_type(`id`) 

应改为

FOREIGN KEY (`type`) REFERENCES sbb_types(`id`) 

进行这些更改会使其起作用(至少在SQL Fiddle中)