MySQL Workbench 重命名约束

发布于 2024-10-05 04:53:22 字数 989 浏览 1 评论 0原文

我使用 MySQL Workbench 设计数据库,然后导出 SQL CREATE 脚本。但是当我运行这个脚本来创建数据库时,我收到一个错误 - errno: 121。

事实证明,MySQL Workbench 给出了两个同名的约束,因为这两个约束使用相同的键(我有一个主键为“roleID”的表) '并且我在另外两个表中引用了这个键)。

有什么方法可以直接在设计器中重命名约束,这样当我对 SQL CREATE 脚本进行正向设计时,它就不会给出错误?

我尝试双击设计器中的关系并给它一个新的标题,但它仍然生成具有原始名称的脚本。

生成的脚本的一部分会产生错误:


CREATE  TABLE IF NOT EXISTS users.roles (
  roleID INT NOT NULL AUTO_INCREMENT ,
  ...
  PRIMARY KEY (roleID) ,
  ...);

如果不存在则创建表 users.userRoles ( ... roleID INT NOT NULL , ... 约束角色ID 外键(roleID) 参考用户角色 (roleID));

如果不存在则创建表 users.resourcePrivileges ( roleID INT NOT NULL , ... 约束角色ID 外键(roleID) 参考用户角色 (roleID));

I use MySQL Workbench to design my database and then to export the SQL CREATE script. But when I run this script to create the database, I get an error - errno: 121.

It turns out that MySQL Workbench gives two constraints the same name, because both constraints use the same key (I have a table with primary key 'roleID' and I reference this key in two other tables).

Is there any way how I can rename the constraint directly in the designer, so when I forward engineer the SQL CREATE script, it will give no errors?

I tried double click the relation in the designer and give it a new caption, but it still generates the script with the original name.

Part of the generated script which creates the error:


CREATE  TABLE IF NOT EXISTS users.roles (
  roleID INT NOT NULL AUTO_INCREMENT ,
  ...
  PRIMARY KEY (roleID) ,
  ...);

CREATE TABLE IF NOT EXISTS users.userRoles ( ... roleID INT NOT NULL , ... CONSTRAINT roleID FOREIGN KEY (roleID ) REFERENCES users.roles (roleID ));

CREATE TABLE IF NOT EXISTS users.resourcePrivileges ( roleID INT NOT NULL , ... CONSTRAINT roleID FOREIGN KEY (roleID ) REFERENCES users.roles (roleID ));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

污味仙女 2024-10-12 04:53:22

不知道你是如何结束的。我试用了 MySQL WorkBench,使用 FK 创建了两个表,并创建了

-- -----------------------------------------------------
-- Table `mydb`.`users`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`users` (
  `idusers` INT NULL ,
  PRIMARY KEY (`idusers`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`usersRoles`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`usersRoles` (
  `users_idusers` INT NOT NULL ,
  PRIMARY KEY (`users_idusers`) ,
  CONSTRAINT `fk_usersRoles_users`
    FOREIGN KEY (`users_idusers` )
    REFERENCES `mydb`.`users` (`idusers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

注意该约束有一个唯一的名称“fk_usersRoles_users”,该名称不会重复,因为它使用表名。
只是为了好玩,我在相同的表之间添加了另一个关系,默认情况下我得到了

-- -----------------------------------------------------
-- Table `mydb`.`usersRoles`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`usersRoles` (
  `users_idusers` INT NOT NULL ,
  `users_idusers1` INT NOT NULL ,
  PRIMARY KEY (`users_idusers`, `users_idusers1`) ,
  INDEX `fk_usersRoles_users1` (`users_idusers1` ASC) ,
  CONSTRAINT `fk_usersRoles_users`
    FOREIGN KEY (`users_idusers` )
    REFERENCES `mydb`.`users` (`idusers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_usersRoles_users1`
    FOREIGN KEY (`users_idusers1` )
    REFERENCES `mydb`.`users` (`idusers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

这又不是问题(以上所有内容都是自动生成的 - 我只设置了表名称、引用表上的主键并添加了两个 1:N关系)

注释:版本 5.2.30。

编辑
也许你的喜好发生了一些变化。 fk 约束的默认名称在 模型 选项卡。

Not sure how you ended up with that. I took MySQL WorkBench for a spin, created two tables with a FK and it created

-- -----------------------------------------------------
-- Table `mydb`.`users`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`users` (
  `idusers` INT NULL ,
  PRIMARY KEY (`idusers`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`usersRoles`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`usersRoles` (
  `users_idusers` INT NOT NULL ,
  PRIMARY KEY (`users_idusers`) ,
  CONSTRAINT `fk_usersRoles_users`
    FOREIGN KEY (`users_idusers` )
    REFERENCES `mydb`.`users` (`idusers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Notice that the constraint has a unique name 'fk_usersRoles_users' that would not get duplicated since it uses table names.
Just for fun I added another relationship between the same tables and by default I get

-- -----------------------------------------------------
-- Table `mydb`.`usersRoles`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`usersRoles` (
  `users_idusers` INT NOT NULL ,
  `users_idusers1` INT NOT NULL ,
  PRIMARY KEY (`users_idusers`, `users_idusers1`) ,
  INDEX `fk_usersRoles_users1` (`users_idusers1` ASC) ,
  CONSTRAINT `fk_usersRoles_users`
    FOREIGN KEY (`users_idusers` )
    REFERENCES `mydb`.`users` (`idusers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_usersRoles_users1`
    FOREIGN KEY (`users_idusers1` )
    REFERENCES `mydb`.`users` (`idusers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Which again is a non problem (all of the above is auto generated - I have only set the table names, primary key on referenced table and added two 1:N relationships)

NOTES: Version 5.2.30.

EDIT
Maybe something happened with your preferences. The default name for the fk constraints is defined on the model tab.

苦妄 2024-10-12 04:53:22

错误 121 是由于约束名称重复造成的。解决方案示例
通常,当使用 MYSQL 正向工程导出选项生成 SQL 脚本时,为了解决问题,我们只需确保“外键名称”在 SQL 脚本/架构中是唯一的。

Error 121 is due to constraint name duplication.solution example
Generally when generating your SQL script with MYSQL forward engineering export option, to resolve the issue we need to just ensure that the "Foreign Key Names" are unique in SQL script/ schema.

压抑⊿情绪 2024-10-12 04:53:22

当您为外键设置约束时,不会分配引用某个表的相同主键的不同名称。因此,我想说的是,检查所有生成的脚本中的所有索引名称是否存在重复。重命名为其他名称。然后你就可以继续...

When you set a constraints for Foreign keys, will not assign different name that referring same primary key of some table. So, what am trying to say is that check all your index names in all the generated scripts if there any duplication. Rename to some other. Then you can proceed...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文