错误1215 MYSQL无法添加外键约束,

我有这样的代码:

CREATE  TABLE IF NOT EXISTS `biblioteca`.`ejemplar` (

  `idejemplar` INT(11) NOT NULL AUTO_INCREMENT ,

  `estado` VARCHAR(45) NOT NULL ,

  `comentario` VARCHAR(45) NULL ,

  `isbn` VARCHAR(45) NOT NULL ,

  PRIMARY KEY (`idejemplar`) ,

  INDEX `fk_ejemplar_libro1_idx` (`isbn` ASC) ,

  CONSTRAINT `fk_ejemplar_libro1`

    FOREIGN KEY (`isbn` )

    REFERENCES `biblioteca`.`libro` (`isbn` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

ENGINE = InnoDB

DEFAULT CHARACTER SET = utf8

和引用的表是:

CREATE  TABLE IF NOT EXISTS `biblioteca`.`libro` (
  `isbn` VARCHAR(25) NOT NULL ,
  `idcategoria` INT(11) NOT NULL ,
  `ideditorial` INT(11) NOT NULL ,
  `titulo` VARCHAR(45) NOT NULL ,
  `autor` VARCHAR(45) NOT NULL ,
  `reseña` VARCHAR(45) NULL ,
  PRIMARY KEY (`isbn`) ,
  INDEX `fk_libro2_idx` (`idcategoria` ASC) ,
  INDEX `fk_libro3_idx` (`ideditorial` ASC) ,
  CONSTRAINT `fk_libro2`
    FOREIGN KEY (`idcategoria` )
    REFERENCES `biblioteca`.`categoria` (`idcategoria` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_libro3`
    FOREIGN KEY (`ideditorial` )
    REFERENCES `biblioteca`.`editorial` (`ideditorial` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

我认为那些外键是好的。


如果这些是没有值的新鲜表格,那么看起来像是列的错误

一个varchar(25)另一个var_char(45)使它们相同

编辑:

CREATE  TABLE IF NOT EXISTS `biblioteca`.`libro` (
  `isbn` VARCHAR(45) NOT NULL ,
  `idcategoria` INT(11) NOT NULL ,
  `ideditorial` INT(11) NOT NULL ,
  `titulo` VARCHAR(45) NOT NULL ,
  `autor` VARCHAR(45) NOT NULL ,
  `reseña` VARCHAR(45) NULL ,
  PRIMARY KEY (`isbn`) ,
  INDEX `fk_libro2_idx` (`idcategoria` ASC) ,
  INDEX `fk_libro3_idx` (`ideditorial` ASC) )

ENGINE = INNODB
DEFAULT CHARACTER SET = utf8;



CREATE  TABLE IF NOT EXISTS `biblioteca`.`ejemplar` (

  `idejemplar` INT(11) NOT NULL AUTO_INCREMENT ,

  `estado` VARCHAR(45) NOT NULL ,

  `comentario` VARCHAR(45) NULL ,

  `isbn` VARCHAR(45) NOT NULL ,

  PRIMARY KEY (`idejemplar`) ,

  INDEX `fk_ejemplar_libro1_idx` (`isbn` ASC) ,

  CONSTRAINT `fk_ejemplar_libro1`

    FOREIGN KEY (`isbn` )

    REFERENCES `biblioteca`.`libro` (`isbn` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

ENGINE = INNODB

DEFAULT CHARACTER SET = utf8;

对我来说没有问题。 我带走了其他2个键,但我没有模式

链接地址: http://www.djcxy.com/p/86269.html

上一篇: error 1215 MYSQL Cannot add foreign key constraint,

下一篇: Using inner Join in Solr query