programing

특정 테이블에 연결된 모든 외부 키 제약 조건 사용 안 함

jooyons 2023. 10. 7. 10:44
반응형

특정 테이블에 연결된 모든 외부 키 제약 조건 사용 안 함

'MY_TAB' 테이블이 있습니다.기본 키 'CODE'를 가진 LE', 이 기본 키는 몇 가지 외래 키 제약 조건이 있어 모두 일시적으로 비활성화해야 합니다.

여기서 이 답변을 확장하려고 합니다. ORA-02273 수정:고유/기본 키는 일부 외부 키에서 참조되지만, constraint_name에서는 참조되지 않습니다.

'MY_TAB'의 'CODE'를 참조하는 모든 foreign_key 제약 조건을 선택하려고 합니다.LE' 및 비활성화(결국 활성화, 구문에서 활성화를 위해 비활성화로 전환한다고 가정합니다)

다음과 같은 테이블이 있다고 가정해 보겠습니다.

create table MY_TABLE ( CODE number primary key);
create table anotherTable ( code_ref number);
alter table  anotherTable add constraint ck1 foreign key ( code_ref) references my_table ( code);
create table yetAnotherTable ( code_ref number);
alter table  yetAnotherTable add constraint ck2 foreign key ( code_ref) references my_table ( code);

다음과 같은 것을 사용하여 테이블의 지정된 열을 참조하는 모든 제약 조건을 루프하고 다음과 같이 비활성화/활성화할 수 있습니다.

begin
    for s in (
                SELECT 'alter table ' || c2.table_name || ' modify constraint ' || c2.constraint_name || ' disable' as statement
                FROM all_constraints c
                       INNER JOIN all_constraints c2
                         ON ( c.constraint_name = c2.r_constraint_name AND c.owner = c2.owner)
                       INNER JOIN all_cons_columns col
                         ON ( c.constraint_name = col.constraint_name AND c.owner = col.owner) 
                WHERE c2.constraint_type = 'R'
                  AND c.table_name = 'MY_TABLE'
                  AND c.owner = 'ALEK'
                  AND col.column_name = 'CODE'
             )
    loop
        dbms_output.put_line(s.statement);
        execute immediate s.statement;
    end loop;
end;

이를 통해 다음을 얻을 수 있습니다.

alter table YETANOTHERTABLE modify constraint CK2 disable
alter table ANOTHERTABLE modify constraint CK1 disable

PL/SQL 코드와 여러 개의 동적으로 구성된 코드를 피할 수 있습니다.alter table진술들.특정 테이블의 기본 키에 의존하는 모든 외부 키를 비활성화하려면 다음과 같이 기본 키를 비활성화하면 됩니다.cascade절을 다시 활성화한 다음(필요한 경우) 다시 활성화합니다.

다음은 예입니다.

--drop table t3;
--drop table t2;
--drop table t1;
create table t1(c1 number primary key);
create table t2(c1 number references t1(c1));
create table t3(c1 number references t1(c1));

select table_name
     , constraint_type
     , status
  from user_constraints
  where table_name in ('T1','T2', 'T3')

TABLE C STATUS    
----- - ----------
T2    R ENABLED   
T1    P ENABLED   
T3    R ENABLED   

3 rows selected.

외부 키 사용 안 함:

alter table t1 disable primary key cascade;
alter table t1 enable  primary key;

결과:

select table_name
     , constraint_type
     , status
  from user_constraints
  where table_name in ('T1','T2', 'T3')

TABLE C STATUS    
----- - ----------
T2    R DISABLED  
T1    P ENABLED   
T3    R DISABLED  

3 rows selected.

참고: 캐스케이드 모드에서는 모든 외부 키 제약 조건을 다시 활성화할 수 없습니다.수동으로 해야 할 겁니다.

이 쿼리를 사용하여 필요한 모든 변경 사항을 생성합니다.

SELECT  'alter table ' || table_name || ' disable constraint ' ||  constraint_name || ';' from (
  select distinct a.table_name, a.constraint_name
  FROM all_cons_columns a
  JOIN all_constraints c ON a.owner = c.owner
                    AND a.constraint_name = c.constraint_name
  JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
                       AND c.r_constraint_name = c_pk.constraint_name
  WHERE c.constraint_type = 'R'
  AND c_pk.table_name = 'MY_TABLE');

테이블의 이름을 입력해야 합니다.

select 'alter table '||table_name||' disable constraint '|| constraint_name||'; 'from user_constraint
where r_constraint_name in
(select constraint_name
    from user_constraints
    where table_name='TCLIENTSALBARANS'
    and constraint_type='P');

언급URL : https://stackoverflow.com/questions/41265367/disable-all-foreign-key-constraints-associated-to-specific-table

반응형