SQL - 错误代码1005,错误号121

时间:2010-12-10 19:49:00

标签: mysql sql mysql-workbench mysql-error-1005

我正在运行以下MySQL脚本(已修剪),由 MySQL Workbench 自动生成,我收到以下错误:

  
    

错误代码:1005     无法创建表'regula.reservation'(错误号:121)

  

我对数据库不太熟悉,而且这个错误信息量不大。
这是什么问题?

-- -----------------------------------------------------
-- Table `regula`.`Users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `regula`.`Users` ;

CREATE  TABLE IF NOT EXISTS `regula`.`Users` (
  `idUsers` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `name` TEXT NOT NULL ,
  `type` TEXT NOT NULL ,
  `pwd` TEXT NOT NULL ,
  PRIMARY KEY (`idUsers`) ,
  UNIQUE INDEX `idUsers_UNIQUE` (`idUsers` ASC) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `regula`.`Projects`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `regula`.`Projects` ;

CREATE  TABLE IF NOT EXISTS `regula`.`Projects` (
  `idProjects` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `ownerId` INT UNSIGNED NOT NULL ,
  `name` TEXT NOT NULL ,
  `date` DATE NOT NULL ,
  `time` TIME NOT NULL ,
  `place` TEXT NOT NULL ,
  `itemType` INT NOT NULL ,
  PRIMARY KEY (`idProjects`) ,
  UNIQUE INDEX `idProjects_UNIQUE` (`idProjects` ASC) ,
  INDEX `ownerId` (`ownerId` ASC) ,
  CONSTRAINT `ownerId`
    FOREIGN KEY (`ownerId` )
    REFERENCES `regula`.`Users` (`idUsers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `regula`.`ItemTypes`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `regula`.`ItemTypes` ;

CREATE  TABLE IF NOT EXISTS `regula`.`ItemTypes` (
  `idItemTypes` INT UNSIGNED NOT NULL ,
  `prjId` INT UNSIGNED NOT NULL ,
  `parentId` INT UNSIGNED NULL DEFAULT NULL ,
  `name` TEXT NOT NULL ,
  PRIMARY KEY (`idItemTypes`) ,
  INDEX `prjId` (`prjId` ASC) ,
  INDEX `parentId` (`parentId` ASC) ,
  CONSTRAINT `prjId`
    FOREIGN KEY (`prjId` )
    REFERENCES `regula`.`Projects` (`idProjects` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `parentId`
    FOREIGN KEY (`parentId` )
    REFERENCES `regula`.`ItemTypes` (`idItemTypes` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `regula`.`Reservation`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `regula`.`Reservation` ;

CREATE  TABLE IF NOT EXISTS `regula`.`Reservation` (
  `idReservation` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `prjId` INT UNSIGNED NOT NULL ,
  `itemTypeId` INT UNSIGNED NOT NULL ,
  `userId` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`idReservation`) ,
  INDEX `prjId` (`prjId` ASC) ,
  INDEX `itemTypeId` (`itemTypeId` ASC) ,
  INDEX `userId` (`userId` ASC) ,
  CONSTRAINT `prjId`
    FOREIGN KEY (`prjId` )
    REFERENCES `regula`.`Projects` (`idProjects` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `itemTypeId`
    FOREIGN KEY (`itemTypeId` )
    REFERENCES `regula`.`ItemTypes` (`idItemTypes` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `userId`
    FOREIGN KEY (`userId` )
    REFERENCES `regula`.`Users` (`idUsers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

3 个答案:

答案 0 :(得分:38)

错误121表示存在外键约束错误。由于您使用的是InnoDB,因此在运行失败的查询后可以使用SHOW ENGINE INNODB STATUS来获取LATEST FOREIGN KEY ERROR部分中的说明。自己运行SQL后,我明白了:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
101210 14:55:50 Error in foreign key constraint creation for table `regula`.`Reservation`.
A foreign key constraint of name `regula`.`prjId`
already exists. (Note that internally InnoDB adds 'databasename'
in front of the user-defined constraint name.)
Note that InnoDB's FOREIGN KEY system tables store
constraint names as case-insensitive, with the
MySQL standard latin1_swedish_ci collation. If you
create tables or databases whose names differ only in
the character case, then collisions in constraint
names can occur. Workaround: name your constraints
explicitly with unique names.

基本上,您需要在最后一个表中为您的prjId约束名称指定唯一名称。约束/外键名称对数据库是全局的,因此它们不能在不同的表中重用。只需更改最后一个

  CONSTRAINT `prjId`

  CONSTRAINT `prjId2`

答案 1 :(得分:3)

当您尝试映射外键时会出现错误代码121.

基本上,当您的外键名称已存在于数据库中时。

例如: -

ALTER TABLE `photokiosk`.`kiosk_event` 
    ADD CONSTRAINT `event_booking_id` 
    FOREIGN KEY `event_booking_id` (`event_booking_id`)
    REFERENCES `event_booking` (`event_booking_id`)

如果名为 event_booking_id 的外键已与另一个表映射。

要解决此问题,请更改外键名称而非列名称。

答案 2 :(得分:0)

如果您尝试使用表中已存在的约束名称,则会收到此错误消息。这里'prjId'已存在于表regulaItemTypes。因此,您不能再次在表'regula.reservation'上使用相同的约束名称。