I have a requirement where unique constraint should be based on certain condition. If the status column is "LIVE" it should not allow another live record for the same algo_id. When I put unique constraint on this two column it does not allow to add multiple "OLD" status record.
TABLE: ALGO
ID : Number(10) -- PK
Algo_id VARCHAR2(30) NOT NULL,
Status VARCHAR2(30) NOT NULL,
Algo_id Status
ALGO-123 OLD
ALGO-123 OLD
ALGO-123 LIVE
ALGO-234 REMOVED
ALGO-234 REMOVED
ALGO-234 LIVE
ALGO-234 LIVE <This should not allow as there is already live record for ALGO-234>
Thanks is advance.
I have tried below
ALTER TABLE ALGO
ADD CONSTRAINT unique_live_algo UNIQUE (algo_id, stats );
Is there way where I can put some condition like below:
ALTER TABLE ALGO
ADD CONSTRAINT unique_live_algo UNIQUE (algo_id, stats ) where/when Status = 'LIVE';