约束数据库表,以便只有一行可以在列中具有特定值

时间:2022-03-09 07:39:03

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.

我是SQL新手,我试图弄清楚当我尝试按照帖子中的建议在我的专栏中强制执行一个'是'时我收到的错误消息。

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

运行Oracle Database 11g企业版11.2.0.1.0版

1 个解决方案

#1


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

删除create index语句的case部分。这执行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.

#1


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

删除create index语句的case部分。这执行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.