我們可以用一個基於函數的索引做到這一點。如您所知,以下使用NVL2()
,如果表達式不爲空,則返回一個值,如果爲空,則返回不同的值。您可以改用CASE()
。
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, '[email protected]')
2/
1 row created.
SQL> insert into blah values (null, '[email protected]')
2/
1 row created.
SQL> insert into blah values ('APC', '[email protected]')
2/
1 row created.
SQL> insert into blah values ('APC', '[email protected]')
2/
insert into blah values ('APC', '[email protected]')
*
ERROR at line 1:
ORA-00001: unique constraint (APC.BLAH_UIDX) violated
SQL>
編輯
因爲在你的方案名稱將永遠被組裝你只需要一個這樣的指標:
SQL> create unique index blah_uidx on blah
2 (nvl2(email, name, null), email)
3/
Index created.
SQL>
來源
2009-09-03 17:21:56
APC
+1,FBI救援;-) – DCookie 2009-09-03 17:25:14
謝謝男人,讚美你! – 2014-08-05 09:14:59