Cannot validate, with novalidate option

前端 未结 2 1690
再見小時候
再見小時候 2020-12-20 02:39

Hi I was inserting some date do my table. For some reasons I had to disable my constraint. The constraint was associated with index. I. ve used this line of code:

         


        
相关标签:
2条回答
  • 2020-12-20 03:18

    You cannot have non-unique values with a unique index. But you can have non-unique values with a unique constraint that is enforced by a non-unique index. Even if you initially created a non-unique index, the drop index and enable syntax will try to recreate a unique index unless you provide more details in the using index section.

    For example:

    SQL> create table my_table(my_column number,
      2     constraint my_constraint unique (my_column));
    
    Table created.
    
    SQL> alter table my_table disable constraint my_constraint drop index;
    
    Table altered.
    
    SQL> insert into my_table select 1 from dual union all select 1 from dual;
    
    2 rows created.
    
    SQL> alter table my_table enable novalidate constraint my_constraint;
    alter table my_table enable novalidate constraint my_constraint
    *
    ERROR at line 1:
    ORA-02299: cannot validate (USER.MY_CONSTRAINT) - duplicate keys found
    
    
    SQL> alter table my_table enable novalidate constraint my_constraint
      2     using index (create index my_index on my_table(my_column));
    
    Table altered.
    
    SQL> --The constraint is enforced, even though other rows violate it.
    SQL> insert into my_table values(1);
    insert into my_table values(1)
    *
    ERROR at line 1:
    ORA-00001: unique constraint (USER.MY_CONSTRAINT) violated
    
    0 讨论(0)
  • 2020-12-20 03:22

    When you inserted the rows into your table you violated the constraint, looks like it is a unique constraint based on the error message "duplicate keys found"

    check what columns the constraint is based on, and then do a quick query like the following to see if you have any rows with duplicates (columna and columnb are the columns in your unique constraint)

    SELECT columna, columnb COUNT() FROM mytable HAVING COUNT() > 1

    You won't be able to enable the unique constraint until all the rows in the table meet the rules of the constraint.

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