0

I m trying to create a unique index with where clause like this:

CREATE UNIQUE INDEX cmt_unique_sid_yid_ct
          ON COMMENTARY (source_id, year_id)
       WHERE comment_type_id = (
         select comment_type_name 
         from comment_type 
         where comment_type_name = 'Final'
       );

Getting this error :

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.

I m basically trying restrict comments with type Final from comment_type table, to only one row per source/year. So scope to :

comment_type_name | source_id | year_id

How can I do this with SQL, specifically oracle?

3
  • This may be the model you want asktom.oracle.com/pls/apex/…. However, you may consider doing this with a view to do the logic as well. Commented Jan 13, 2017 at 15:55
  • Lookup function based index, that is what you want. Commented Jan 13, 2017 at 18:00
  • @Raj can you point me to the example, I found this oracle-base.com/articles/8i/function-based-indexes doesn't seem related, maybe I m not understanding this correctly Commented Jan 13, 2017 at 20:07

1 Answer 1

1

No such thing in Oracle, just some workaround with function based indexes:

CREATE UNIQUE INDEX cmt_unique_sid_yid_ct
ON COMMENTARY (
  case when comment_type_name = 'Final' then source_id end,
  case when comment_type_name = 'Final' then year_id end
);

Sample query that can use this index:

select * from commentary
where 
case when comment_type_name = 'Final' then source_id end = :B1
and case when comment_type_name = 'Final' then year_id end = :B2
;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.