gpt4 book ai didi

oracle - h2 数据库上的条件唯一索引

转载 作者:行者123 更新时间:2023-12-04 23:47:53 24 4
gpt4 key购买 nike

我有一个带有 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);

关于oracle - h2 数据库上的条件唯一索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28836704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com