我有一个SAMPLE_TABLE,它的列BIZ_ID在列活动不等于0时应该是唯一的。
在oracle数据库上,索引如下所示:
CREATE UNIQUE INDEX ACTIVE_ONLY_IDX ON SAMPLE_TABLE (CASE "ACTIVE" WHEN 0 THEN NULL ELSE "BIZ_ID" END );
这个唯一的索引在h2数据库上会是什么样子?
在H2中,可以使用具有唯一索引的计算列:
create table test(
biz_id int,
active int,
biz_id_active int as
(case active when 0 then null else biz_id end)
unique
);
--works
insert into test(biz_id, active) values(1, 0);
insert into test(biz_id, active) values(1, 0);
insert into test(biz_id, active) values(2, 1);
--fails
insert into test(biz_id, active) values(2, 1);