테이블의 외부 키 관계 쿼리
주어진 테이블 'foo'에 대해 foo를 가리키는 외부 키를 가진 테이블 집합을 생성하기 위한 쿼리가 필요합니다.Oracle 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>'));
데이터 사전 보기를 탐색할 수 있습니다.접두사는 다음과 같습니다.
- 사용자
- 모든.
- 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;
데이터 사전 테이블을 설명하는 Oracle Reference Guide for 10G를 다운로드합니다.
위의 답변은 좋지만 제약 조건과 관련이 있을 수 있는 다른 표를 확인하십시오.
SELECT * FROM DICT WHERE TABLE_NAME LIKE '%CONS%';
마지막으로 Toad 또는 SQL Developer와 같은 도구를 사용하여 UI에서 이 항목을 탐색할 수 있습니다. 테이블 사용법을 배워야 하지만 UI도 사용해야 합니다.
select acc.table_name, acc.constraint_name
from all_cons_columns acc
inner join all_constraints ac
on acc.constraint_name = ac.constraint_name
where ac.r_constraint_name in (
select constraint_name
from all_constraints
where table_name='yourTable'
);
한 테이블에 대한 모든 제약 조건
select
uc.OWNER,
uc.constraint_name as TableConstraint1,
uc.r_constraint_name as TableConstraint2,
uc.constraint_type as constrainttype1,
us.constraint_type as constrainttype2,
uc.table_name as Table1,us.table_name as Table2,
ucc.column_name as TableColumn1,
uccs.column_name as TableColumn2
from user_constraints uc
left outer join user_constraints us on uc.r_constraint_name = us.constraint_name
left outer join USER_CONS_COLUMNS ucc on ucc.constraint_name = uc.constraint_name
left outer join USER_CONS_COLUMNS uccs on uccs.constraint_name = us.constraint_name
where uc.OWNER ='xxxx' and uc.table_name='xxxx'
여기에 내 2센트를 더하면 됩니다.
이 쿼리는 하위 및 상위 열이 있는 모든 외부 키를 반환하며, 여러 열에 외부 키가 있는 경우에도 완벽하게 일치합니다.
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 AND b.position = a.position
WHERE c.constraint_type = 'R'
(@arvinq aswer에서 영감을 받았습니다)
언급URL : https://stackoverflow.com/questions/85978/query-a-tables-foreign-key-relationships
'prosource' 카테고리의 다른 글
오라클 데이터베이스 문에 ENABLE 키워드를 사용하는 것이 유용한 이유입니다. (0) | 2023.07.07 |
---|---|
분기 git를 삭제하면 기록에서 제거됩니까? (0) | 2023.07.07 |
Python을 공유 라이브러리에 연결하기 위해 Cython 사용 (0) | 2023.07.07 |
.NET MySql 커넥터를 사용하여 mariadb에서 DELMITER를 사용하는 방법은 무엇입니까? (0) | 2023.07.07 |
Apache POI, 새 셀 작성이 행 스타일을 재정의합니다. (0) | 2023.07.07 |