Disable all table constraints in Oracle

后端 未结 11 911
耶瑟儿~
耶瑟儿~ 2020-11-28 18:35

How can I disable all table constrains in Oracle with a single command? This can be either for a single table, a list of tables, or for all tables.

相关标签:
11条回答
  • 2020-11-28 18:43

    with cursor for loop (user = 'TRANEE', table = 'D')

    declare
        constr all_constraints.constraint_name%TYPE;
    begin
        for constr in
            (select constraint_name from all_constraints
            where table_name = 'D'
            and owner = 'TRANEE')
        loop
            execute immediate 'alter table D disable constraint '||constr.constraint_name;
        end loop;
    end;
    /
    

    (If you change disable to enable, you can make all constraints enable)

    0 讨论(0)
  • 2020-11-28 18:48

    It's not a single command, but here's how I do it. The following script has been designed to run in SQL*Plus. Note, I've purposely written this to only work within the current schema.

    set heading off
    
    spool drop_constraints.out
    
    select
        'alter table ' || 
        owner || '.' || 
        table_name || 
        ' disable constraint ' || -- or 'drop' if you want to permanently remove
        constraint_name || ';'
    from
        user_constraints;
    
    spool off
    
    set heading on
    
    @drop_constraints.out
    

    To restrict what you drop, filter add a where clause to the select statement:-

    • filter on constraint_type to drop only particular types of constraints
    • filter on table_name to do it only for one or a few tables.

    To run on more than the current schema, modify the select statement to select from all_constraints rather than user_constraints.

    Note - for some reason I can't get the underscore to NOT act like an italicization in the previous paragraph. If someone knows how to fix it, please feel free to edit this answer.

    0 讨论(0)
  • 2020-11-28 18:50

    This can be scripted in PL/SQL pretty simply based on the DBA/ALL/USER_CONSTRAINTS system view, but various details make not as trivial as it sounds. You have to be careful about the order in which it is done and you also have to take account of the presence of unique indexes.

    The order is important because you cannot drop a unique or primary key that is referenced by a foreign key, and there could be foreign keys on tables in other schemas that reference primary keys in your own, so unless you have ALTER ANY TABLE privilege then you cannot drop those PKs and UKs. Also you cannot switch a unique index to being a non-unique index so you have to drop it in order to drop the constraint (for this reason it's almost always better to implement unique constraints as a "real" constraint that is supported by a non-unique index).

    0 讨论(0)
  • 2020-11-28 18:50

    It doesn't look like you can do this with a single command, but here's the closest thing to it that I could find.

    0 讨论(0)
  • 2020-11-28 18:53

    Use following cursor to disable all constraint.. And alter query for enable constraints...

    DECLARE
    
    cursor r1 is select * from user_constraints;
    cursor r2 is select * from user_tables;
    
    BEGIN
      FOR c1 IN r1
      loop
        for c2 in r2
        loop
           if c1.table_name = c2.table_name and c1.status = 'ENABLED' THEN
            dbms_utility.exec_ddl_statement('alter table ' || c1.owner || '.' || c1.table_name || ' disable constraint ' || c1.constraint_name);
           end if;
        end loop;
      END LOOP;
    END;
    /
    
    0 讨论(0)
  • 2020-11-28 18:54

    In the "disable" script, the order by clause should be that:

    ORDER BY c.constraint_type DESC, c.last_change DESC
    

    The goal of this clause is disable the constraints in the right order.

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