테이블의 외래 키 관계 쿼리
문제
주어진 테이블 'foo'에 대해 foo를 가리키는 외래 키가 있는 테이블 집합을 생성하는 쿼리가 필요합니다.오라클 10G를 사용하고 있습니다.
해결책
이것은 작동합니다 (또는 가까운 것):
select table_name
from all_constraints
where constraint_type='R'
and r_constraint_name in
(select constraint_name
from all_constraints
where constraint_type in ('P','U')
and table_name='<your table here>');
다른 팁
다음 명령문은 어린이와 모든 자손에게 제공되어야 합니다.Oracle 10 데이터베이스에서 테스트했습니다.
SELECT level, main.table_name parent,
link.table_name child
FROM user_constraints main, user_constraints link
WHERE main.constraint_type IN ('P', 'U')
AND link.r_constraint_name = main.constraint_name
START WITH main.table_name LIKE UPPER('&&table_name')
CONNECT BY main.table_name = PRIOR link.table_name
ORDER BY level, main.table_name, link.table_name
다음은 Mike의 쿼리를 한 단계 더 발전시켜 다음을 얻는 방법입니다. 열 이름 제약 조건 이름에서:
select * from user_cons_columns
where constraint_name in (
select constraint_name
from all_constraints
where constraint_type='R'
and r_constraint_name in
(select constraint_name
from all_constraints
where constraint_type in ('P','U')
and table_name='<your table name here>'));
다음으로 연결 Oracle 데이터베이스 온라인 설명서
당신은 탐험하고 싶을 수도 있습니다 데이터 사전 보기.접두어가 있습니다.
- 사용자
- 모두
- DBA
견본:
select * from dictionary where table_name like 'ALL%'
계속해서 Mike의 예를 들어 제약 조건을 활성화/비활성화하는 스크립트를 생성할 수 있습니다.첫 번째 행의 '선택'만 수정했습니다.
select 'alter table ' || TABLE_NAME || ' disable constraint ' || CONSTRAINT_NAME || ';'
from all_constraints
where constraint_type='R'
and r_constraint_name in
(select constraint_name
from all_constraints
where constraint_type in ('P','U')
and table_name='<your table here>');
답변하기가 다소 늦었다는 것을 알고 있지만 어쨌든 답변해 드리겠습니다. 위의 답변 중 일부는 상당히 복잡하므로 여기서는 훨씬 더 간단하게 설명하겠습니다.
`SELECT a.table_name child_table, a.column_name child_column, a.constraint_name, b.table_name parent_table, b.column_name parent_column FROM all_cons_columns a JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name join all_cons_columns b on c.owner = b.owner and c.r_constraint_name = b.constraint_name WHERE c.constraint_type = 'R' AND a.table_name = 'your table name'`
select distinct table_name, constraint_name, column_name, r_table_name, position, constraint_type
from (
SELECT uc.table_name,
uc.constraint_name,
cols.column_name,
(select table_name from user_constraints where constraint_name = uc.r_constraint_name)
r_table_name,
(select column_name from user_cons_columns where constraint_name = uc.r_constraint_name and position = cols.position)
r_column_name,
cols.position,
uc.constraint_type
FROM user_constraints uc
inner join user_cons_columns cols on uc.constraint_name = cols.constraint_name
where constraint_type != 'C'
)
start with table_name = '&&tableName' and column_name = '&&columnName'
connect by nocycle
prior table_name = r_table_name
and prior column_name = r_column_name;
데이터 사전 테이블을 설명하는 10G용 Oracle 참조 가이드를 다운로드하세요.
위의 답변은 좋지만 제약 조건과 관련된 다른 표를 확인하세요.
SELECT * FROM DICT WHERE TABLE_NAME LIKE '%CONS%';
마지막으로 UI에서 이러한 항목을 탐색할 수 있는 Toad 또는 SQL Developer와 같은 도구를 얻으십시오. 테이블 사용 방법을 배워야 하지만 UI도 사용해야 합니다.
제휴하지 않습니다 StackOverflow