我在尝试从mysql工作台转发工程师时收到错误'#1215 Cannot add foreign key'
。
我有一个矩形表,它引用了scale和entities表中的列。 create rectangle语句抛出错误。
这是我的架构
-- -----------------------------------------------------
-- Table `newDb`.`Scales`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `newDb`.`Scales` (
`idScales` INT NOT NULL AUTO_INCREMENT,
`scale_name` VARCHAR(100) NULL,
`pid` INT NULL,
`col_Id` INT NULL,
`type` VARCHAR(45) NULL,
`range_from` INT NULL,
`range_to` INT NULL,
`bandpadding` INT NULL,
PRIMARY KEY (`idScales`),
UNIQUE INDEX `idScales_UNIQUE` (`idScales` ASC),
INDEX `pid_idx` (`pid` ASC),
INDEX `col_id_idx` (`col_Id` ASC),
CONSTRAINT `pid`
FOREIGN KEY (`pid`)
REFERENCES `newDb`.`projects` (`pid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `col_id`
FOREIGN KEY (`col_Id`)
REFERENCES `newDb`.`data_sets_columns` (`col_Id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `newDb`.`Entities`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `newDb`.`Entities` (
`idEntities` INT NOT NULL AUTO_INCREMENT,
`entity_name` VARCHAR(100) NULL,
`entity_type` VARCHAR(45) NULL,
`pid` INT NOT NULL,
PRIMARY KEY (`idEntities`),
UNIQUE INDEX `idEntities_UNIQUE` (`idEntities` ASC),
INDEX `pid_idx` (`pid` ASC),
CONSTRAINT `pidEF`
FOREIGN KEY (`pid`)
REFERENCES `newDb`.`projects` (`pid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `newDb`.`Rectangle`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `newDb`.`Rectangle` (
`idRectangle` INT NOT NULL AUTO_INCREMENT,
`rect_name` VARCHAR(100) NULL,
`X_pos` INT NOT NULL DEFAULT 0,
`Y_pos` INT NOT NULL DEFAULT 0,
`Height` INT NOT NULL DEFAULT 50,
`Width` INT NOT NULL DEFAULT 50,
`Offset_X` INT NOT NULL DEFAULT 0,
`Offset_Y` INT NOT NULL DEFAULT 0,
`Opacity` INT NULL DEFAULT 100,
`Color` VARCHAR(10) NOT NULL DEFAULT 'black',
`idScale` INT NOT NULL,
`idEntities` INT NOT NULL,
`idColorScale` INT NULL,
PRIMARY KEY (`idRectangle`),
UNIQUE INDEX `idRectangle_UNIQUE` (`idRectangle` ASC),
INDEX `idScales_idx` (`idScale` ASC, `idColorScale` ASC),
INDEX `idEntities_idx` (`idEntities` ASC),
CONSTRAINT `idScalesF`
FOREIGN KEY (`idScale` , `idColorScale`)
REFERENCES `newDb`.`Scales` (`idScales` , `idScales`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `idEntitiesF`
FOREIGN KEY (`idEntities`)
REFERENCES `newDb`.`Entities` (`idEntities`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
还有一个问题:MySQL Error 1215: Cannot add foreign key constraint要求同样的事情。
我跟着它,根据答案我要么发动机不匹配,列类型不匹配或列不匹配。
我似乎没有看到任何不匹配,因为我所有引用的列和外键coloum都是无符号的INT,名称相同,引擎是INNODB。
似乎无法找到我在这里缺少的东西
答案 0 :(得分:1)
以下约束毫无意义:
CONSTRAINT `idScalesF`
FOREIGN KEY (`idScale` , `idColorScale`)
REFERENCES `newDb`.`Scales` (`idScales` , `idScales`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
这就像你在试图说Rectangle
中的每一行都有一对idScale
和idColorScale
与Scales
中的一对列相匹配这两个都叫idScales
。如果您试图说idScale
和idColorScale
每个引用Scales
中的一行,则需要将其写为两个单独的外键。
另外,PRIMARY KEY总是隐式唯一,并充当索引。您创建的…_UNIQUE
索引(例如idScales_UNIQUE
)都是多余的,应该删除。