在 MySQL Workbench EER 图中的多列上创建 UNIQUE 约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11376413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:05:24  来源:igfitidea点击:

Creating UNIQUE constraint on multiple columns in MySQL Workbench EER diagram

mysqlmysql-workbenchunique-constraintmultiple-columnsworkbench

提问by zhenming

In MySQL Workbench's EER diagram, there is a checkbox to make each column in a table unique, not null, primary key etc.

在 MySQL Workbench 的 EER 图中,有一个复选框可以使表中的每一列都是唯一的,而不是 null、主键等。

However, I would like to have a UNIQUE constraint on multiple columns. Is it possible to add it in in MySQL Workbench's EER diagram?

但是,我想在多列上有一个 UNIQUE 约束。是否可以将其添加到 MySQL Workbench 的 EER 图中?

EDIT: Ok, I realised the unique checkbox, creates a UNIQUE INDEX, and not a UNIQUE CONSTRAINT

编辑:好的,我意识到了唯一的复选框,创建了一个唯一的索引,而不是一个唯一的约束

回答by Alex

In the Alter Table dialog of MySQL Workbench:

在 MySQL Workbench 的 Alter Table 对话框中:

  • Go to Indexes tab.
  • Double-click on a blank row to create a new index.
  • Choose 'UNIQUE' as the index type.
  • Check the columns that you want to be unique together.
  • 转到索引选项卡。
  • 双击空白行以创建新索引。
  • 选择“UNIQUE”作为索引类型。
  • 一起检查要唯一的列。

There's some discussionas to whether this is weird, since an index is not the same as a constraint. I certainly wouldn't have thought to look there. However, apparently the `unique index' enforces uniqueness in the same way as a unique constraint, and may improve performance. For example, if I try to insert a row that would break unique together after using this method, it throws an '1062 Duplicate entry' error.

有一些关于这是否很奇怪的讨论,因为索引与约束不同。我当然不会想到去那里看。然而,显然“唯一索引”以与唯一约束相同的方式强制唯一性,并且可以提高性能。例如,如果我尝试插入一行,在使用此方法后会破坏唯一性,则会引发“1062 重复条目”错误。

回答by zhenming

it does not seem to be available : http://bugs.mysql.com/bug.php?id=48468. it seems what you can is to create a multi column unique index on the indexes tab but for a multi column unique constraint, you need to run the creation command manually.

它似乎不可用:http: //bugs.mysql.com/bug.php?id=48468。似乎您可以在索引选项卡上创建多列唯一索引,但对于多列唯一约束,您需要手动运行创建命令。

回答by GeorgioA

With latest MWB (I'm on 6.0.8), it is possible to create composite keys

使用最新的 MWB(我在 6.0.8),可以创建复合键

If you wish to create a composite primary key you can select multiple columns and check the PK check box. However, there is an additional step that is required, you must click the Indexes tab, then in the Index Columns panel you must set the desired order of the primary keys.

如果您希望创建复合主键,您可以选择多个列并选中 PK 复选框。但是,还需要一个额外的步骤,您必须单击“索引”选项卡,然后在“索引列”面板中设置所需的主键顺序。