sedeh
sedeh

Reputation: 7313

Constraining a database table so only one row can have a particular value in a column

I am brand new to SQL and I am trying to figure out an error message I get when I try to follow the suggestions in the post here to enforce only one 'Yes' in my column.

DROP TABLE team  CASCADE CONSTRAINTS PURGE;
create table team (
  name varchar2(4) NOT NULL UNIQUE,
  isTeamLead char(3) 
    check (isTeamLead in ('Yes')));

create unique index only_one_yes on team(isTeamLead)
(case when col='YES' then 'YES' end);

The error report is as follows:

Error report -
SQL Error: ORA-02158: invalid CREATE INDEX option
02158. 00000 -  "invalid CREATE INDEX option"
*Cause:    An option other than COMPRESS, NOCOMPRESS, PCTFREE, INITRANS,
           MAXTRANS, STORAGE, TABLESPACE, PARALLEL, NOPARALLEL, RECOVERABLE,
           UNRECOVERABLE, LOGGING, NOLOGGING, LOCAL, or GLOBAL was specified.
*Action:   Choose one of the valid CREATE INDEX options.

Any thoughts?

Running Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

Upvotes: 0

Views: 1358

Answers (1)

Bohemian
Bohemian

Reputation: 425188

Remove the case part of the create index statement. This executes OK:

create table team (
  name varchar2(4) NOT NULL UNIQUE,
  isTeamLead char(3) check (isTeamLead in ('Yes'))
);

create unique index only_one_yes on team(isTeamLead);

insert into team values ('x', 'Yes');
insert into team values ('y', null);

See SQLFiddle.

Upvotes: 1

Related Questions