Quote:
Originally Posted by Garoad
Here's the situation:
We have a table containing a column that should have unique values in it at all times. BUT there's one exception: when a flag is set in another column (say a "deleted" flag), those "unique" values can be re-used within the non-"deleted" subset of items.
So the values in the "unique" column aren't unique if you count re-used values that were used in previous (deleted) rows.
|
You posted in the general database section, but this solution is highly DBMS dependent. It's called a partial index.
With Postgres you can do the following:
Code:
CREATE UNIQUE INDEX idx_partial_unique
ON your_table (unique_column)
WHERE flag <> 'deleted';
I think (but I'm not sure) that SQL Server 2008 has a similar syntax as Postgres.
With Oracle you could do something like this:
Code:
CREATE UNIQUE INDEX idx_partial_unique
ON your_table ( CASE
WHEN flag = 'deleted' THEN NULL
ELSE unique_column
END);