DB2 Unique Constraint over multiple Columns

吃可爱长大的小学妹 提交于 2019-12-08 02:32:22

问题


Is there a way to create a unique constraint over multiple columns like in Mysql?

UNIQUE KEY `uniqueKey` (`Field1`,`Field2`),

The only thing i have found is the unique keyword directly after the field.

Thanks in advance!


回答1:


You should be able to add a unique constraint as:

alter table t add constraint unq_field1_field2 unique(field1, field2)

As with other databases, this is almost exactly equivalent to creating an index. The difference is that the constraint is named -- which can be convenient for tracking constraints and understanding error messages.




回答2:


You should be able to create a multi-column index

 CREATE UNIQUE INDEX myindex
                      ON mytable (col1, col2 desc, col3)


来源:https://stackoverflow.com/questions/36619300/db2-unique-constraint-over-multiple-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!