您可以創建一個獨特的基於函數的索引執行的唯一性(品牌,型號),對僅時許=「福特」:
create table cars (make varchar2(255) not null, model varchar2(255) not null);
create unique index ford_index on cars (
case make
when 'Ford' then make
else null
end,
case make
when 'Ford' then model
else null
end );
insert into cars (make, model) values ('Ford', 'F150');
insert into cars (make, model) values ('Ford', 'Fiesta');
insert into cars (make, model) values ('Toyota', 'Camry');
insert into cars (make, model) values ('Toyota', 'Camry');
insert into cars (make, model) values ('Ford', 'F150'); --- FAILS with unique constraint violation
ford_index指數(品牌,型號)只有化妝=」福特' - 其他所有東西都不在索引中,因此不受唯一性限制。
但是,如果有二十八個必須執行唯一性的製造商,以及另外三十六個不需要唯一性的製造商,那麼您會如何編寫這個?如果需要唯一性的製作清單隨着時間而改變,它將如何維持? – mathguy
@mathguy - 可能我會爲每個需要模型唯一性的make創建一個索引。既然我們在談論汽車製造商,我預計製造數量會相當低。 – user1041892
謝謝!在這種情況下,我沒有想過使用病例陳述。這當然是我見過的最好的方式。 對我來說不幸的是,我不得不將這添加到已經有重複項的表中(以防止未來的重複),但是對於約束上的'DEFFERED NOVALIDATE'選項,似乎沒有任何索引等效項。 – JKuester