2010-04-21 3 views

답변

53

이 경우 INFORMATION_SCHEMA 테이블을 사용할 수 있습니다. 예를 들어 INFORMATION_SCHEMA TABLE_CONSTRAINTS 테이블입니다. 당신은 단지 하나의 데이터베이스가있는 경우 사용자 RedFilter에 의해

select * 
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
where CONSTRAINT_TYPE = 'FOREIGN KEY' 
+0

그게 내가 원하는 것을 가지고있는 것처럼 보입니다. 감사! –

+1

외래 키의 필드 이름을 실제로 나열하는 방법이 있습니까? – JoeTidee

5

현재 허용 대답은 잘 작동합니다,하지만 당신은 많은이없는 경우이 같은

뭔가를해야한다.

name_of_db에 대한 외래 키를 얻을 수 use information_schema; 사용이 쿼리를 입력 한 후 :

select * from `table_constraints` where `table_schema` like "name_of_db" and `constraint_type` = 'FOREIGN KEY' into outfile "output_filepath_and_name" FIELDS TERMINATED BY ',' ENCLOSED BY '"'; 
0

SQL : 세계 쓰기 가능한 파일 output_filepath_and_name에 저장 name_of_db에 대한 외부 키를 얻을 수

select * from `table_constraints` where `table_schema` like `name_of_db` and `constraint_type` = 'FOREIGN KEY' 

사용이 쿼리 :

select constraint_name, 
     table_schema, 
     table_name 
from information_schema.table_constraints 
where constraint_schema = 'astdb' 
,210

출력 :

+----------------------------+--------------+---------------------+ 
| constraint_name   | table_schema | table_name   | 
+----------------------------+--------------+---------------------+ 
| PRIMARY     | astdb  | asset_category  | 
| PRIMARY     | astdb  | asset_type   | 
| PRIMARY     | astdb  | asset_valuation  | 
| PRIMARY     | astdb  | assets    | 
| PRIMARY     | astdb  | com_mst    | 
| PRIMARY     | astdb  | com_typ    | 
| PRIMARY     | astdb  | ref_company_type | 
| PRIMARY     | astdb  | supplier   | 
| PRIMARY     | astdb  | third_party_company | 
| third_party_company_ibfk_1 | astdb  | third_party_company | 
| PRIMARY     | astdb  | user    | 
| PRIMARY     | astdb  | user_role   | 
+----------------------------+--------------+---------------------+ 
1

쿼리 코드

select constraint_name, 
    table_schema, 
    table_name 
from information_schema.table_constraints 

당신은 constraint_name을 얻을, 그리고 database의 목록입니다 TABLE_SCHEMA를 필터링합니다.

SELECT CONSTRAINT_NAME, 
     UNIQUE_CONSTRAINT_NAME, 
     MATCH_OPTION, 
     UPDATE_RULE, 
     DELETE_RULE, 
     TABLE_NAME, 
     REFERENCED_TABLE_NAME 
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
WHERE CONSTRAINT_SCHEMA = 'your_database_name' 
6

내가 유용한 정보를 얻을 선호하는 것입니다 모든 외래 키 - 테이블 또는 - 열