I have a pre-existing table structured similarly to this:
[Username] [Gateway] [Code] [ActiveTo] [OrgId]
=======================================================
user1 gateway 50 Null 100
user1 gateway 50 Null 101
user2 gateway2 51 Null 102
user3 gateway3 52 12/08/17 103
This table already has duplicate combinations of [Username]
, [Gateway]
, and [Code]
.
I want to add a constraint such that any new users added have a unique combination of [Username]
, [Gateway]
, and [Code]
but that ignores two pre-existing duplicates such as the two user1
entries above (not ignoring new entries that match pre-existing ones).
I only want this to be applied when the duplicate's [ActiveTo]
value is null (new entries have ActiveTo
as null by default). So:
- Applying the constraint to the table above would ignore the two
user1
duplicates. - Adding a row
[user2], [gateway2], [52], [Null]
would be fine. - Adding a row
[user2], [gateway2], [51], [Null]
would then throw an error. - Then adding a row
[user3], [gateway3], [52], [Null]
would be fine.
Any ideas?
CREATE TABLE ADDITIONAL_USER_ORGANISATIONS (Username ..., OrgId ..., primary key ( Username, OrgId ));
and move violating rows there.