programing

전체 MySQL 데이터베이스에 대한 모든 외부 키 제약 조건 보기

sourcejob 2022. 11. 14. 21:45
반응형

전체 MySQL 데이터베이스에 대한 모든 외부 키 제약 조건 보기

150개가 넘는 테이블이 있는 대용량 데이터베이스를 최근에 전달받았습니다.테이블 단위 대신 전체 DB에 대한 모든 외부 키 제약 조건을 쉽게 볼 수 있는 방법이 있는지 궁금할 뿐입니다.

이 테이블은 이용하실 수 있습니다.예를 들어 테이블입니다.

다음과 같은 방법으로 해결할 수 있습니다.

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

유용한 정보를 얻으려면 다음과 같이 하십시오.

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'

RedFilter 사용자가 현재 수락한 답변은 데이터베이스가 1개만 있는 경우 정상적으로 동작하지만 많은 경우 정상적으로 동작하지 않습니다.

입력 후use information_schema;이 쿼리를 사용하여 외부 키를 가져옵니다.name_of_db:

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

이 쿼리를 사용하여 외부 키를 가져옵니다.name_of_db쓰기 가능한 파일에 저장output_filepath_and_name:

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 '"';

이 코드 쿼리

select constraint_name,
   table_schema,
   table_name
from   information_schema.table_constraints

contraint_name 을 취득하여 table_schema 를 필터링 합니다.이것은, 다음의 리스트입니다.database.

이것 좀 봐요.

SQL:

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

출력:

+----------------------------+--------------+---------------------+
| 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           |
+----------------------------+--------------+---------------------+

언급URL : https://stackoverflow.com/questions/2684472/view-all-foreign-key-constraints-for-entire-mysql-database

반응형