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

て烟熏妆下的殇ゞ 提交于 2019-12-13 05:38:37

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/30091578/constraining-a-database-table-so-only-one-row-can-have-a-particular-value-in-a-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!