Oracle Unique Constraint based on column value

后端 未结 1 365
挽巷
挽巷 2021-01-03 00:21

I have the following unique constraint

dup_Checklist_QNum UNIQUE (QUESTION_NO, IS_ACTIVE)

I am trying to prevent two questions having the s

相关标签:
1条回答
  • 2021-01-03 00:47

    You can create a unique function-based index

    CREATE UNIQUE INDEX idx_dup_active
        ON <<table name>>( CASE WHEN is_active = 1
                                THEN question_no
                                ELSE NULL
                            END );
    

    This takes advantage of the fact that Oracle b-tree indexes do not store data where the leaf block data would be entirely NULL.

    0 讨论(0)
提交回复
热议问题