There is much stuff around about 'unique' vs 'index' and I read lot of them, but still can't find explanation for my case.
In short, my database currently has a non-clustered index on two columns. We have some inconsistent state as a result of data duplication and I want to add a unique constraint on all four columns of the table. Since 'unique' will create an index under the hood does it still make sense to keep the old index around?
More specifically, it goes something like:
create table Stuff (
id PK,
col1 varchar not null,
col2 varchar not null,
col3 bigint not null,
col4 bigint not null);
create index for_search on Stuff (col3, col4);
alter table Stuff
add constraint data_integrity_above_all
UNIQUE (col1, col2, col3, col4);
and if the answer is 'yes', then would it be a good move to specify UNIQUE (col3, col4, col1, col2);
column order or is it too much implicit and not end well on maintenance?
More information about the nature of the data: col3
and col4
are another table ids (but not FK); col1
, col2
-- user data.
DB: SQL Server 2012
id
as a FK in any other table? If not, have you considered getting rid ofid
and definingcol3, col4, col1, col2
as the Clustered PK and also getting rid of thefor_search
index?id
also came to my mind. But even if there is no FK reference today, there might be one in future. Of course real complication is in code base itself. It has 10+ years of development behind and I'm the new guy who asks too many questions) Eventually though I suggested to apply changes that align with @mendosi answer, It was declined.