I'm trying for days to disable the constraints of all my tables, insert data and enable the constraints. It works but I did some tests and when I insert a row that doesnt respect a foreign key, there's no error message when I enable the constraints. All I can see is that that constraint is not validated.
This is the code to disable
begin
BEGIN
FOR rec IN ( SELECT constraint_name, table_name FROM user_constraints WHERE constraint_type = 'R' OR constraint_type = 'P' )
LOOP
BEGIN
EXECUTE IMMEDIATE 'alter table '||rec.table_name||' disable constraint '||rec.constraint_name;
EXCEPTION WHEN OTHERS THEN
dbms_output.put_line( 'ERROR: alter table '||rec.table_name||' DISABLE constraint '||rec.constraint_name||';' );
END;
END LOOP;
DBMS_OUTPUT.PUT_LINE('CONSTRAINTS DISABLED');
END;
BEGIN
FOR rec IN ( SELECT trigger_name FROM user_triggers WHERE TRIGGER_NAME NOT LIKE 'BIN$%' )
LOOP
EXECUTE IMMEDIATE 'alter trigger '||rec.trigger_name||' disable';
END LOOP;
END;
DBMS_OUTPUT.PUT_LINE('TRIGGERS DISABLED');
end;
/
This is the code to enable
begin
BEGIN
FOR rec IN ( SELECT constraint_name, table_name FROM user_constraints where status = 'DISABLED' and constraint_type = 'R' OR constraint_type = 'P' )
LOOP
BEGIN
EXECUTE IMMEDIATE 'alter table '||rec.table_name||' enable constraint '||rec.constraint_name;
dbms_output.put_line('alter table '||rec.table_name||' enable constraint '||rec.constraint_name);
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('ERROR: alter table '||rec.table_name||' enable constraint '||rec.constraint_name||' ;' );
END;
END LOOP;
END;
DBMS_OUTPUT.PUT_LINE('CONSTRAINTS ENABLED');
BEGIN
FOR rec IN ( SELECT trigger_name FROM user_triggers WHERE TRIGGER_NAME NOT LIKE 'BIN$%' )
LOOP
EXECUTE IMMEDIATE 'alter trigger '||rec.trigger_name||' enable';
END LOOP;
END;
DBMS_OUTPUT.PUT_LINE('TRIGGERS ENABLED');
END;
I don't know how to check all constraints at the end and do a rollback if it doesn't work.