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

China☆狼群 提交于 2021-01-27 13:05:40

问题


SQL Server 2008 apparently has filtered indices.

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


回答1:


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.



来源:https://stackoverflow.com/questions/43559840/h2-database-unique-constraint-only-over-non-null-values-filtered-index

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!