1

此错误的原因可能是什么?

 CREATE  TABLE IF NOT EXISTS `myhotel`.`roomer` (
      `id` INT NOT NULL ,
      `name` VARCHAR(45) NOT NULL ,
      `start` DATE NOT NULL ,
      `finish` DATE NOT NULL ,
      `day` INT NOT NULL ,
      PRIMARY KEY (`id`) ,
      INDEX `fk_id` (`id` ASC) ,
      CONSTRAINT `fk_id`
        FOREIGN KEY (`id` )
        REFERENCES `myhotel`.`all_roomers` (`id_roomer` )
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB;

CREATE  TABLE IF NOT EXISTS `myhotel`.`all_roomers` (
  `id_roomer` INT NOT NULL ,
  `id_room` INT NOT NULL ,
  `status` TINYINT(1) NOT NULL ,
  INDEX `fk_id_room` (`id_room` ASC) ,
  PRIMARY KEY (`id_roomer`, `id_room`) ,
  CONSTRAINT `fk_id_room`
    FOREIGN KEY (`id_room` )
    REFERENCES `myhotel`.`room` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE  TABLE IF NOT EXISTS `myhotel`.`room` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `number` INT NOT NULL ,
  `price` INT NOT NULL ,
  `capacity` INT NOT NULL ,
  `stars` INT NOT NULL ,
  `status` TINYINT(1) NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;

我得到的错误是:在服务器中执行 SQL 脚本

错误:错误 1005:无法创建表“myhotel.roomer”(错误号:150)

4

1 回答 1

1

根据 MySQL Docs(见这里):

如果 MySQL 从 CREATE TABLE 语句中报告错误号 1005,并且错误消息引用错误 150,则表创建失败,因为未正确形成外键约束。

我会说从您的脚本的外观来看,这可能是因为CREATE TABLE您使用的语句在myhotel.all_roomers创建表之前引用了它。我敢打赌,您需要先创建该表,然后再创建对其的外键引用。

Also, I'm not sure but it also looks as if you're using the id column as both a primary key as well as a foriegn key. I'm not sure this is allowed either but can't find anywhere in the documentation that says it's incorrect.

于 2012-06-26T03:22:19.743 回答