How can I create a unique index in Oracle but ignore nulls?

后端 未结 2 1774
一个人的身影
一个人的身影 2021-01-07 17:24

I am trying to create a unique constraint on two fields in a table. However, there is a high likelihood that one will be null. I only require that they be unique if both a

2条回答
  •  失恋的感觉
    2021-01-07 17:55

    We can do this with a function-based index. The following makes use of NVL2() which, as you know, returns one value if the expression is not null and a different value if it is null. You could use CASE() instead.

    SQL> create table blah (name varchar2(10), email varchar2(20))
      2  /
    
    Table created.
    
    SQL> create unique index blah_uidx on blah
      2      (nvl2(email, name, null), nvl2(name, email, null))
      3  /
    
    Index created.
    
    SQL> insert into blah values ('APC', null)
      2  /
    
    1 row created.
    
    SQL> insert into blah values ('APC', null)
      2  /
    
    1 row created.
    
    SQL> insert into blah values (null, 'apc@example.com')
      2  /
    
    1 row created.
    
    SQL> insert into blah values (null, 'apc@example.com')
      2  /
    
    1 row created.
    
    SQL> insert into blah values ('APC', 'apc@example.com')
      2  /
    
    1 row created.
    
    SQL> insert into blah values ('APC', 'apc@example.com')
      2  /
    insert into blah values ('APC', 'apc@example.com')
    *
    ERROR at line 1:
    ORA-00001: unique constraint (APC.BLAH_UIDX) violated
    
    
    SQL>
    

    Edit

    Because in your scenario name will always be populated you will only need an index like this:

    SQL> create unique index blah_uidx on blah
      2      (nvl2(email, name, null), email)
      3  /
    
    Index created.
    
    SQL> 
    

提交回复
热议问题