在MySQL Workbench EER图中为多个列创建UNIQUE约束

时间:2021-10-01 04:24:58

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图中,有一个复选框可以使表中的每一列都是唯一的,而不是空的,主键等。

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

编辑:好的,我意识到唯一的复选框,创建一个独特的索引,而不是一个独特的CONSTRAINT

3 个解决方案

#1


93  

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.
  • 选择“UNIQUE”作为索引类型。
  • Check the columns that you want to be unique together.
  • 检查要一起唯一的列。

There's some discussion as 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重复条目”错误。

#2


2  

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 = 48848。您似乎可以在索引选项卡上创建多列唯一索引,但对于多列唯一约束,您需要手动运行创建命令。

#3


2  

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复选框。但是,还有一个必需的步骤,您必须单击“索引”选项卡,然后在“索引列”面板中必须设置主键的所需顺序。

#1


93  

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.
  • 选择“UNIQUE”作为索引类型。
  • Check the columns that you want to be unique together.
  • 检查要一起唯一的列。

There's some discussion as 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重复条目”错误。

#2


2  

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 = 48848。您似乎可以在索引选项卡上创建多列唯一索引,但对于多列唯一约束,您需要手动运行创建命令。

#3


2  

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复选框。但是,还有一个必需的步骤,您必须单击“索引”选项卡,然后在“索引列”面板中必须设置主键的所需顺序。