建表后先在主键上建一个分区unique索引,然后再把主键约束加上

解决方案 »

  1.   

    直接加主键是不行的:
    drop table parindex_test1;create table parindex_test1 
    (par_id number,
     value1 number,
     value2 number)
    partition by range (par_id)
    (partition part_200501 values less than (200502),
     partition part_200502 values less than (200503));
     
     insert into parindex_test1 values (200502,32,34343);
     
     insert into parindex_test1 values (200501,31,34343);
     
     commit;
     
     alter table parindex_test1 add primary key (par_id,value1);alter table parindex_test1 drop partition part_200501;insert into parindex_test1 values (200502,34,34343);
    结果:
    Table droppedTable created1 row inserted1 row insertedCommit completeTable alteredTable alteredinsert into parindex_test1 values (200502,34,34343)ORA-01502: 索引'BILL.SYS_C005335'或这类索引的分区处于不可用状态
      

  2.   

    先建一个分区unique索引就可以了
    drop table parindex_test1;create table parindex_test1 
    (par_id number,
     value1 number,
     value2 number)
    partition by range (par_id)
    (partition part_200501 values less than (200502),
     partition part_200502 values less than (200503));
     
     insert into parindex_test1 values (200502,32,34343);
     
     insert into parindex_test1 values (200501,31,34343);
     
     commit;
     
     /*------先建索引*/
     create unique index parpkidx on parindex_test1 (par_id,value1) local;
     
     alter table parindex_test1 add primary key (par_id,value1);alter table parindex_test1 drop partition part_200501;insert into parindex_test1 values (200502,34,34343);结果:
    Table droppedTable created1 row inserted1 row insertedCommit completeIndex createdTable alteredTable altered1 row inserted
      

  3.   

    如果不包括分区列,是无法建分区unique索引的