Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 database: unique constraint only over non-NULL values? (filtered index)

Tags:

sql

h2

SQL Server 2008 apparently has filtered indices.

What is the cleanest way to achieve the same in H2?

like image 699
Reto Höhener Avatar asked Oct 15 '25 15:10

Reto Höhener


1 Answers

I believe you can use a computed column for this purpose . . . assuming you have a unique id. Let me assume the unique id is numeric and never negative. Then:

alter table t add col (case when <condition> then -1 else uniqueid end);

create unique index unq_t_col on t(col);

I believe that H2 supports unique indexes, computed columns, and indexes on computed columns, so this should work.

like image 125
Gordon Linoff Avatar answered Oct 17 '25 06:10

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!