Conditional Unique index on h2 database

前端 未结 1 1415
一生所求
一生所求 2021-01-06 03:27

I have a SAMPLE_TABLE with a column BIZ_ID which should be unique when the column active is not equal to 0.

On an oracle database the index looks like this:

相关标签:
1条回答
  • 2021-01-06 04:16

    In H2, you could use a computed column that has a unique index:

    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);
    
    0 讨论(0)
提交回复
热议问题