Is it possible to create a unique constraint/unique index on a column which already has duplicate values?
Answer is "YES" with the help of filtered index.
In general, if we create a index every single data on a column will be moved to index pages whereas in the case of filtered index(since we are using where clause to filter the data) only the data which matches the filtered condition will be moved to index pages.
create unique index IX_UNIQUE_TABLE_TEST_COL1 on TABLE_TEST(COL1)
where id > 1000;
The above syntax will create unique index only on the ID's which is greater than 1000(assume that we have duplicate values till ID = 1000).
In case if we are already aware of duplicate values then we can do something like below
create unique index IX_UNIQUE_TABLE_TEST_COL1 on TABLE_TEST(COL1)
where ID not in (<list of duplicate values>)
Answer is "YES" with the help of filtered index.
In general, if we create a index every single data on a column will be moved to index pages whereas in the case of filtered index(since we are using where clause to filter the data) only the data which matches the filtered condition will be moved to index pages.
create unique index IX_UNIQUE_TABLE_TEST_COL1 on TABLE_TEST(COL1)
where id > 1000;
The above syntax will create unique index only on the ID's which is greater than 1000(assume that we have duplicate values till ID = 1000).
In case if we are already aware of duplicate values then we can do something like below
create unique index IX_UNIQUE_TABLE_TEST_COL1 on TABLE_TEST(COL1)
where ID not in (<list of duplicate values>)