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

时间:2015-05-07 03:50:09

标签: sql oracle11g unique-index

我是SQL新手,我正在尝试找出当我尝试按照帖子here中的建议在我的列中仅强制执行一个'是'时收到的错误消息。

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);

错误报告如下:

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.

有什么想法吗?

运行Oracle Database 11g企业版11.2.0.1.0版

1 个答案:

答案 0 :(得分:1)

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

请参阅SQLFiddle