Ad

SQL (Custom) Unique Constraint For A Set Of Rows

- 1 answer

How to create a ( may be custom) unique constraint for a particular no.of rows.

I have table data as below

enter image description here

I want my itemFK column to be Unique for same productionsessionKey value. i.e for all yellow (101) itemFK should be Unique and for all blue (102) itemFK should be unique. Else they can be duplicate.

Ad

Answer

Add a unique constraint on a tupple for those 2 columns

F.e.

ALTER TABLE your_table
  ADD CONSTRAINT UC_Your_Table_ProdSession_Item UNIQUE (productionsessionKey, itemFK);
Ad
source: stackoverflow.com
Ad