Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL Constraint where clause

I have the table Tester on oracle with the following columns:

  • TesterID
  • TesterName
  • IsDefault
  • Application_ID

TesterID is the primary key. Now I want that there can only be one Default Tester, which means only one Tester can have the calues IsDefault =Y at an ApplicationID.

I tried it with a constraint:

alter table Tester add constraint Tester_ISDEFAULT UNIQUE(IsDefault,Application_ID);

Is it possible to make the unique key on where isdefault= Y?

Thanks for help!

like image 806
John Smithv1 Avatar asked Mar 09 '26 02:03

John Smithv1


1 Answers

Not with a UNIQUE constraint. However, you can use a UNIQUE INDEX instead:

CREATE UNIQUE INDEX ApplicationId_Default_Y ON tester (
  CASE WHEN IsDefault = 'Y'
       THEN ApplicationId
       ELSE NULL
  END
);

Here's a DEMO.

like image 152
João Silva Avatar answered Mar 11 '26 15:03

João Silva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!