0

I was trying to enforce only one active phone per client using a unique index in Oracle but can't make it work. This works flawlessly in PostgreSQL and DB2 but doesn't seem to work in Oracle.

Here's the example:

create table phone (
  client_id number(6) not null,
  active number(1) not null check (active in (0, 1)),
  value varchar2(15)
);

insert into phone (client_id, active, value) values (10, 0, '1111');
insert into phone (client_id, active, value) values (10, 1, '3333');
insert into phone (client_id, active, value) values (15, 0, '5555');
insert into phone (client_id, active, value) values (15, 1, '6666');
insert into phone (client_id, active, value) values (15, 0, '7777'); -- offending row

When I try to create the index:

create unique index ix1 on phone (client_id, case when active = 1 then active end);

It seems that Oracle doesn't like it because a duplicate index entry:

Error: ORA-01452: cannot CREATE UNIQUE INDEX; duplicate keys found
SQLState: 72000

If I remove the offending data row, the index can be created.

4
  • Your index has a case with no else. Is Oracle assigned a 0 to the result of the case when it's not 1? It seems that when the row isn't active, it'll be 0, resulting in two rows with a (15, 0). The title of your question asks about NULL, but I don't see NULL coming into the equation here(?).
    – HardCode
    Jul 30, 2021 at 14:12
  • 1
    I'd expect on phone (case when active=1 then client_id end) to work across database engines. I don't have time to verify that and write up an answer right now but will try to do so later if no one beats me to it. Jul 30, 2021 at 14:13
  • 1
    @HardCode - the default else value is null (not only in Oracle but, I believe, in the SQL Standard). So the OP's attempt was fine from that point of view. The issue was different - it's what MT0 explained in his answer.
    – user5683823
    Jul 30, 2021 at 14:30
  • null is never equal to null. Jul 30, 2021 at 15:48

1 Answer 1

4

You can use:

create unique index ix1 on phone (case active when 1 then client_id end);

sqlfiddle here

A UNIQUE index will not store rows when all the columns are NULL; however, in your case, the function-based index results in one-of-two values being NULL so the row will be indexed and this will cause a duplicate value in the index.

Changing it so that the output is a single value derived from both the active and client_id columns means that the rows where active = 0 will result in a NULL output from the CASE expression and those rows will not be included in the index and only the active rows will be checked for duplicates.

From the Oracle Unique constraint documentation:

To satisfy a unique constraint, no two rows in the table can have the same value for the unique key. However, the unique key made up of a single column can contain nulls. To satisfy a composite unique key, no two rows in the table or view can have the same combination of values in the key columns. Any row that contains nulls in all key columns automatically satisfies the constraint. However, two rows that contain nulls for one or more key columns and the same combination of values for the other key columns violate the constraint.

4
  • You are so right. Thank you. I should have implemented as a partial index (as you mention). Jul 30, 2021 at 14:24
  • However it still bothers me "...will cause a duplicate value in the index...". Well a (15, null) is not supposed to be equal to another (15, null). PostgreSQL seems to behave correctly: sqlfiddle.com/#!17/815f55. However, DB2 behaves the same way as Oracle: dbfiddle.uk/… Jul 30, 2021 at 14:30
  • @TheImpaler - In Oracle, null is sometimes equal to null. When you group by a column, all the rows where that column is null are grouped together. If you select distinct <something>, you will get a single null in the output even if the <something> is null more than once. Similarly, in determining "unique", null is equal to itself across rows. You don't see this when the index (or condition) is on a single column, because Oracle chose - for whatever reason - to exclude null from consideration altogether.
    – user5683823
    Jul 30, 2021 at 14:33
  • 1
    @TheImpaler Added a link to the Oracle documentation.
    – MT0
    Jul 30, 2021 at 14:35

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.