您需要的所有信息都通过MySQL 的 information_schema 视图公开。几乎所有这些都在“information_schema.columns”表中。
select table_schema, table_name, column_name,
data_type, character_maximum_length, column_key
from information_schema.columns
where table_schema in ( 'db-1-name', 'db-2-name')
order by table_schema, table_name, ordinal_position
“character_maximum_length”列仅适用于可变长度列,但扩展查询以显示整数、大整数、日期等的长度应该相当容易。
有关外键约束的信息在“information_schema.key_column_usage”中。您只需要将其加入“information_schema.columns”。
select c.table_schema, c.table_name, c.column_name,
c.data_type, c.character_maximum_length, c.column_key,
k.referenced_table_name
from information_schema.columns c
left join information_schema.key_column_usage k
on c.table_catalog = k.table_catalog
and c.table_schema = k.table_schema
and c.table_name = k.table_name
and c.column_name = k.column_name
and k.referenced_table_name is not null
where c.table_schema in ( 'db-1-name', 'db-2-name')
order by c.table_schema, c.table_name, c.ordinal_position