How to create unique index on fields with possible null values (Oracle 11g)?

后端 未结 2 559
误落风尘
误落风尘 2021-02-04 12:44

Here is the sample table with 3 columns (ID, UNIQUE_VALUE, UNIQUE_GROUP_ID)

I want below records can be allowed:

(1, NULL, NULL)
(2, NULL, NULL)
<         


        
2条回答
  •  不要未来只要你来
    2021-02-04 13:10

    you can use the nvl function to avoid nulls and place a different value instead ,

    create unique index func_idx on TEST_TABLE (nvl(UNIQUE_VALUE,1), UNIQUE_GROUP_ID);
    

    the disadvantage is that your index will be larger and if you would like to search for null values you will have to use the nvl function in order to avoid table_access_full.

    also all of the null values will be located under one branch in the index , so make sure your histograms are updated.

    I Hope this will help you :)

提交回复
热议问题