Conditional unique constraint with multiple fields in oracle db

后端 未结 3 753
一个人的身影
一个人的身影 2021-01-04 05:12

I have this table:

XPTO_TABLE (id, obj_x, date_x, type_x, status_x)

I wanna create a unique constraint that applies to the fields (ob

3条回答
  •  孤城傲影
    2021-01-04 06:09

    @jamesfrj: it looks like you are trying to ensure that your table should contain only one record for which status <>5.

    You can try creating a unique functional index by concatenating the columns, as given below

          create table XPTO_TABLE (id number, 
                                obj_x varchar2(20),
                                date_x date,
                                type_x varchar2(20),
                                status_x varchar2(20)                              
                               );
    
          create unique index xpto_table_idx1 on XPTO_TABLE(case when status_x <>'5'  THEN              obj_x||date_x||type_x||STATUS_x ELSE null END);
    

    Hope it helps

    Vishad

提交回复
热议问题