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

标签 oracle h2 unique-index

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

相关文章:

sql - Oracle 优化器提示 xmlagg 函数

database - 检查数据库表中是否有某些记录的最快方法?

java - 无法访问 Spring H2 控制台

java - 使用 H2 和 HSQL DB 时 Hibernate 查询返回重复结果集

h2 - 执行h2数据库中的脚本文件

sql - PostgreSQL 约束,在提交时检查,而不是更早

mysql - 删除具有唯一索引的重复项

SQL For Update 跳过锁定查询和 Java 多线程 - 如何解决此问题

java - 为什么从 Oracle 存储过程中检索结果集这么慢?

mysql - 当在唯一索引列中找到匹配项时,MySQL 会停止搜索吗?