1

I have an table employees , and I want to add an constraint which makes me able to have ONLY one employee with id_manager null.. I have a sample from my code , but I think it's wrong..

ALTER TABLE employees ADD CONSTRAINT u_t UNIQUE (id_manager);

And this is the error I get.

SQL Error: ORA-02299: (OPREAC_IDD.U_T)
02299. 00000 - "cannot validate (%s.%s) - duplicate keys found"
*Cause:    an alter table validating constraint failed because the table has
       duplicate key values.
0

1 Answer 1

4

You say:

I want to add a constraint which makes me able to have ONLY one employee with id_manager null.

So if I understand this correctly, you're ok with having duplicates in the id_manager column. The only thing you're trying to restrict is making sure that no more than one row can have null as the value for id_manager.

If that's the case, you'll want to create a unique index with an expression so that only null values are considered and validated:

create unique index emp_idx
on employees (case when id_manager is null then 1 end)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.