Truncate table in Oracle getting errors

后端 未结 9 1265
忘了有多久
忘了有多久 2021-02-02 08:11

I got the problem is when I run following command in Oracle, I encounter the error.

Truncate table mytable;

Errors:

         


        
相关标签:
9条回答
  • 2021-02-02 08:51

    Oracle 12c introduced a feature to truncate a table that is a parent of a referential integrity constraint having ON DELETE rule.

    Instead of truncate table tablename; use:

    TRUNCATE TABLE tablename CASCADE;
    

    From Oracle truncate table documentation:

    If you specify CASCADE, then Oracle Database truncates all child tables that reference table with an enabled ON DELETE CASCADE referential constraint. This is a recursive operation that will truncate all child tables, granchild tables, and so on, using the specified options.

    0 讨论(0)
  • 2021-02-02 08:52

    You have to swap the TRUNCATE statement to DELETE statements, slower and logged but that's the way to do it when constraints are in place.

    DELETE mytablename;
    

    Either that or you can find the foreign keys that are referencing the table in question and disable them temporarily.

    select 'ALTER TABLE '||TABLE_NAME||' DISABLE CONSTRAINT '||CONSTRAINT_NAME||';'
    from user_constraints
    where R_CONSTRAINT_NAME='<pk-of-table>';
    

    Where pk-of-table is the name of the primary key of the table being truncated

    Run the output of the above query. When this has been done, remember to enable them again, just change DISABLE CONSTRAINT into ENABLE CONSTRAINT

    0 讨论(0)
  • 2021-02-02 08:53

    Issue:

    Error “ORA-02266: unique/primary keys in table referenced by enabled foreign keys” when trying to truncate a table.
    

    Error Message:

    SQL> truncate table TABLE_NAME;  
    
    truncate table TABLE_NAME
               *
    ERROR at line 1:
    ORA-02266: unique/primary keys in table referenced by enabled foreign keys
    

    Solution: -- Find the referenced foreign key constraints.

     SQL> select 'alter table '||a.owner||'.'||a.table_name||' disable constraint '||a.constraint_name||';'
      2  from all_constraints a, all_constraints b
      3  where a.constraint_type = 'R'
      4  and a.r_constraint_name = b.constraint_name
      5  and a.r_owner  = b.owner
      6  and b.table_name = 'TABLE_NAME';
    
        'ALTER TABLE'||A.OWNER||'.'||A.TABLE_NAME||'DISABLE CONSTRAINT'||A.CONSTRAINT_NAME||';'
        ---------------------------------------------------------------------------------------------------------
        alter table SCHEMA_NAME.TABLE_NAME_ATTACHMENT disable constraint CONSTRAINT_NAME;    
    alter table SCHEMA_NAME.TABLE_NAME_LOCATION disable constraint CONSTRAINT_NAME;
    

    -- Disable them

    alter table SCHEMA_NAME.TABLE_NAME_ATTACHMENT disable constraint CONSTRAINT_NAME;
    alter table SCHEMA_NAME.TABLE_NAME_LOCATION disable constraint CONSTRAINT_NAME;
    

    -- Run the truncate

    SQL> truncate table TABLE_NAME;
    
    Table truncated.
    

    -- Enable the foreign keys back

     SQL> select 'alter table '||a.owner||'.'||a.table_name||' enable constraint '||a.constraint_name||';'
      2  from all_constraints a, all_constraints b
      3  where a.constraint_type = 'R'
      4  and a.r_constraint_name = b.constraint_name
      5  and a.r_owner  = b.owner
      6  and b.table_name = 'TABLE_NAME';
    
    'ALTER TABLE'||A.OWNER||'.'||A.TABLE_NAME||'ENABLE CONSTRAINT'||A.CONSTRAINT_NAME||';'
    --------------------------------------------------------------------------------
    
    alter table SCHEMA_NAME.TABLE_NAME_ATTACHMENT enable constraint CONSTRAINT_NAME;
    alter table SCHEMA_NAME.TABLE_NAME_LOCATION enable constraint CONSTRAINT_NAME;
    

    -- Enable them

    alter table SCHEMA_NAME.TABLE_NAME_ATTACHMENT enable constraint CONSTRAINT_NAME;
    alter table SCHEMA_NAME.TABLE_NAME_LOCATION enable constraint CONSTRAINT_NAME;
    
    0 讨论(0)
  • 2021-02-02 08:55

    this page offers a very good solution ...

    ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    I'm here copying from it the Solution:

    • Find the referenced ENABLED foreign key constraints and disable them.
    • truncate/delete from the table .
    • using any text editor .. just change disable to enable in the output you get from the query , then run it.

      select 'alter table '||a.owner||'.'||a.table_name||' disable constraint '||a.constraint_name||';'
      from all_constraints a, all_constraints b
      where a.constraint_type = 'R' and a.status='ENABLED'
      and a.r_constraint_name = b.constraint_name
      and a.r_owner  = b.owner
      and b.table_name = upper('YOUR_TABLE');
      
    0 讨论(0)
  • 2021-02-02 08:56

    The error message is telling you that there are other table(s) with a foreign key constraint referring to your table.

    According to the Oracle docs

    You cannot truncate the parent table of an enabled foreign key constraint. You must disable the constraint before truncating the table.

    The syntax for disabling a foreign key is:

    ALTER TABLE table_name disable CONSTRAINT constraint_name;

    0 讨论(0)
  • 2021-02-02 09:03
    TRUNCATE TABLE TEST2 DROP ALL STORAGE;
    

    This statement Actually works when there is an foreign key constraint applied on a .table

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