我有一个带有 BIZ_ID 列的 SAMPLE_TABLE,当列 active 不等于 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)