Mysql show create constraint?

二次信任 提交于 2019-12-07 00:09:20

问题


Is there a easy way to query a table for its constraints(foreignkeys specificaly) like show create table, but for the constraints only?

thanks,

pvgoddijn


回答1:


To show only the foreign key constraints you can check the constraint_type in information_schema.table_constraints and get the affected columns in information_schema.key_column_usage via a join

SELECT b.table_name, b.column_name, b.constraint_name,
       b.referenced_table_name, b.referenced_column_name
FROM information_schema.table_constraints a
JOIN information_schema.key_column_usage b
ON a.table_schema = b.table_schema AND a.constraint_name = b.constraint_name
WHERE a.table_schema=database() AND a.constraint_type='FOREIGN KEY'
ORDER BY b.table_name, b.constraint_name;



回答2:


select * from 
information_schema.KEY_COLUMN_USAGE 
where table_schema = <db_name> 
and table_name = <table_name>;



回答3:


SHOW TABLE STATUS FROM db_name LIKE 'tbl_name';

The foreign key constraints are listed in the Comment column of the output.




回答4:


MySQL 5.1 Manual

SHOW TABLE STATUS FROM db_name LIKE 'tbl_name';


来源:https://stackoverflow.com/questions/2470356/mysql-show-create-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!