MySQL Workbench 重命名约束
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不知道你是如何结束的。我试用了 MySQL WorkBench,使用 FK 创建了两个表,并创建了
注意该约束有一个唯一的名称“fk_usersRoles_users”,该名称不会重复,因为它使用表名。
只是为了好玩,我在相同的表之间添加了另一个关系,默认情况下我得到了
这又不是问题(以上所有内容都是自动生成的 - 我只设置了表名称、引用表上的主键并添加了两个 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
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
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.
错误 121 是由于约束名称重复造成的。
通常,当使用 MYSQL 正向工程导出选项生成 SQL 脚本时,为了解决问题,我们只需确保“外键名称”在 SQL 脚本/架构中是唯一的。
Error 121 is due to constraint name duplication.
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.
当您为外键设置约束时,不会分配引用某个表的相同主键的不同名称。因此,我想说的是,检查所有生成的脚本中的所有索引名称是否存在重复。重命名为其他名称。然后你就可以继续...
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...