查看整个MySQL数据库的所有外键约束
我有一个包含150多个表的大型数据库,我最近收到了这些表。我只是想知道是否有一种简单的方法可以查看整个数据库的所有外键约束,而不是基于每个表。您可以使用这些表来进行此操作。例如,表格 像这样的东西应该可以做到:查看整个MySQL数据库的所有外键约束,mysql,foreign-keys,database,Mysql,Foreign Keys,Database,我有一个包含150多个表的大型数据库,我最近收到了这些表。我只是想知道是否有一种简单的方法可以查看整个数据库的所有外键约束,而不是基于每个表。您可以使用这些表来进行此操作。例如,表格 像这样的东西应该可以做到: select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE = 'FOREIGN KEY' 如果您只有一个数据库,则用户RedFilter当前接受的答案可以正常工作,但如果您有多个数据库,则无法正常工
select *
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where CONSTRAINT_TYPE = 'FOREIGN KEY'
如果您只有一个数据库,则用户RedFilter当前接受的答案可以正常工作,但如果您有多个数据库,则无法正常工作 输入
后使用信息\u模式
使用此查询获取数据库的名称的外键:
select * from `table_constraints` where `table_schema` like `name_of_db` and `constraint_type` = 'FOREIGN KEY'
使用此查询获取保存到世界可写文件的\u db的name\u的外键output\u filepath\u和\u 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 '"';
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 |
+----------------------------+--------------+---------------------+
输出:
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 |
+----------------------------+--------------+---------------------+
查询此代码
select constraint_name,
table_schema,
table_name
from information_schema.table_constraints
您将获得constraint\u name,并过滤表\u模式,它是数据库的列表
这是我想要得到的有用信息:
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'
看起来这正是我需要的。谢谢还有什么方法可以列出外键的字段名吗?请看Andy的答案:这是最好的答案