Skip to content
Advertisement

DB2 Unique Constraint over multiple Columns

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!

Advertisement

Answer

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.

Notice, that if the matching index does not exist, then a unique index is automatically created.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement