0

我目前正在尝试使用MySQL Workbench的Forward Engineer向导创建我的数据库,但出现此错误:

Executing SQL script in server
ERROR: Error 1005: Can't create table 'microweb.users' (errno: 121)


CREATE  TABLE IF NOT EXISTS `microweb`.`users` (
  `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `role_id` INT UNSIGNED NOT NULL ,
  `username` VARCHAR(45) NOT NULL ,
  `password` VARCHAR(45) NOT NULL ,
  `email` VARCHAR(45) NOT NULL ,
  `salt` VARCHAR(45) NOT NULL ,
  `first_name` VARCHAR(45) NOT NULL ,
  `middle_name` VARCHAR(45) NULL ,
  `last_name` VARCHAR(45) NOT NULL ,
  `address_id` INT UNSIGNED NULL ,
  `registration_id` INT UNSIGNED NOT NULL ,
  `active` TINYINT(1) NOT NULL ,
  `banned` TINYINT(1) NOT NULL ,
  PRIMARY KEY (`user_id`) ,
  UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC) ,
  UNIQUE INDEX `username_UNIQUE` (`username` ASC) ,
  UNIQUE INDEX `email_UNIQUE` (`email` ASC) ,
  UNIQUE INDEX `registration_id_UNIQUE` (`registration_id` ASC) ,
  CONSTRAINT `fk_user_role`
    FOREIGN KEY (`role_id` )
    REFERENCES `microweb`.`roles` (`role_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_user_registration`
    FOREIGN KEY (`registration_id` )
    REFERENCES `microweb`.`registrations` (`registration_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB

SQL script execution finished: statements: 10 succeeded, 1 failed

完整的脚本包含很多外键和 21 个表。问题是当Workbench说有10个成功时,执行后phpMyAdmin中只有2个。

如果有人需要完整的模型,请告诉我。

4

2 回答 2

1

我建议先在没有约束的情况下尝试它,然后使用ALTER TABLE.

CREATE  TABLE IF NOT EXISTS `microweb`.`users` (
  `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `role_id` INT UNSIGNED NOT NULL ,
  `username` VARCHAR(45) NOT NULL ,
  `password` VARCHAR(45) NOT NULL ,
  `email` VARCHAR(45) NOT NULL ,
  `salt` VARCHAR(45) NOT NULL ,
  `first_name` VARCHAR(45) NOT NULL ,
  `middle_name` VARCHAR(45) NULL ,
  `last_name` VARCHAR(45) NOT NULL ,
  `address_id` INT UNSIGNED NULL ,
  `registration_id` INT UNSIGNED NOT NULL ,
  `active` TINYINT(1) NOT NULL ,
  `banned` TINYINT(1) NOT NULL ,
  PRIMARY KEY (`user_id`) ,
  UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC) ,
  UNIQUE INDEX `username_UNIQUE` (`username` ASC) ,
  UNIQUE INDEX `email_UNIQUE` (`email` ASC) ,
  UNIQUE INDEX `registration_id_UNIQUE` (`registration_id` ASC)
)
ENGINE = InnoDB;

ALTER TABLE `microweb`.`users`
ADD CONSTRAINT `fk_user_role`
    FOREIGN KEY (`role_id` )
    REFERENCES `microweb`.`roles` (`role_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE;

ALTER TABLE `microweb`.`users`
ADD CONSTRAINT `fk_user_registration`
    FOREIGN KEY (`registration_id` )
    REFERENCES `microweb`.`registrations` (`registration_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE;
于 2013-03-14T20:54:24.497 回答
1

错误 121 表示存在重复的键名。您必须确保自己的所有 FK 名称在整个架构中都是唯一的。

于 2013-03-15T08:48:26.217 回答