mysql自动生成数据结构的语句(gym_%需替换成自己的表名前缀):
select distinct
a.TABLE_NAME as '表名',
TABLES_.TABLE_COMMENT as '表注释',
a.COLUMN_NAME as '字段名',
a.COLUMN_COMMENT as '字段注释',
a.COLUMN_TYPE as '类型长度',
case when a.IS_NULLABLE='yes' then '√'else '' end as '允许空值',
case when a.COLUMN_DEFAULT='' then '""' else a.COLUMN_DEFAULT end as '默认值',
a.CHARACTER_SET_NAME as '字符集',
a.COLLATION_NAME as '整理',
a.EXTRA as '自动递增',
case when b.CONSTRAINT_NAME='PRIMARY' then 'PRIMARY' else '' end as '主键',
c.CONSTRAINT_NAME as '外键名',
c.REFERENCED_TABLE_NAME as '关联父表',
c.REFERENCED_COLUMN_NAME as '父表字段',
d. CONSTRAINT_NAME as '索引名称'
from INFORMATION_SCHEMA.COLUMNS as a
left join (select CONSTRAINT_NAME,TABLE_NAME table_name2,COLUMN_NAME col_name2 from INFORMATION_SCHEMA .KEY_COLUMN_USAGE where CONSTRAINT_NAME='PRIMARY' and table_name like 'gym_%') as b
on a.TABLE_NAME=b.table_name2 and a.COLUMN_NAME=b.col_name2
left join (select CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME,TABLE_NAME table_name3,COLUMN_NAME col_name3 from INFORMATION_SCHEMA .KEY_COLUMN_USAGE where REFERENCED_COLUMN_NAME!='' and table_name like 'gym_%') as c
on a.TABLE_NAME=c.table_name3 and a.COLUMN_NAME=c.col_name3
left join (select CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME,TABLE_NAME table_name4,COLUMN_NAME col_name4 from INFORMATION_SCHEMA .KEY_COLUMN_USAGE where REFERENCED_COLUMN_NAME is null and CONSTRAINT_NAME!='PRIMARY' and table_name like 'gym_%') as d
on a.TABLE_NAME=d.table_name4 and a.COLUMN_NAME=d.col_name4
LEFT JOIN INFORMATION_SCHEMA.TABLES AS TABLES_ ON a.TABLE_NAME=TABLES_.TABLE_NAME
where a.table_name like 'gym_%'
下面这段写得比较规范些,但如果数据库数量比较多的情况下,性能有些差,我几十个数据库,200秒才出来结果:
use `fastadmin-ganentang`;
select distinct
a.TABLE_NAME as '表名',
TABLES_.TABLE_COMMENT as '表注释',
a.COLUMN_NAME as '字段名',
a.COLUMN_COMMENT as '字段注释',
a.COLUMN_TYPE as '类型长度',
case when a.IS_NULLABLE='yes' then '√'else '' end as '允许空值',
case when a.COLUMN_DEFAULT='' then '""' else a.COLUMN_DEFAULT end as '默认值',
a.CHARACTER_SET_NAME as '字符集',
a.COLLATION_NAME as '整理',
a.EXTRA as '自动递增',
case when b.CONSTRAINT_NAME='PRIMARY' then 'PRIMARY' else '' end as '主键',
c.CONSTRAINT_NAME as '外键名',
c.REFERENCED_TABLE_NAME as '关联父表',
c.REFERENCED_COLUMN_NAME as '父表字段',
d. CONSTRAINT_NAME as '索引名称'
from INFORMATION_SCHEMA.COLUMNS as a
left join INFORMATION_SCHEMA .KEY_COLUMN_USAGE as b
on a.TABLE_SCHEMA=b.TABLE_SCHEMA and a.TABLE_NAME=b.TABLE_NAME and a.COLUMN_NAME=b.COLUMN_NAME
and b.CONSTRAINT_NAME='PRIMARY'
left join INFORMATION_SCHEMA.KEY_COLUMN_USAGE as c
on a.TABLE_SCHEMA=c.TABLE_SCHEMA and a.TABLE_NAME=c.TABLE_NAME and a.COLUMN_NAME=c.COLUMN_NAME
and c.REFERENCED_COLUMN_NAME!=''
left join INFORMATION_SCHEMA .KEY_COLUMN_USAGE as d
on a.TABLE_SCHEMA=d.TABLE_SCHEMA and a.TABLE_NAME=d.TABLE_NAME and a.COLUMN_NAME=d.COLUMN_NAME
and d.REFERENCED_COLUMN_NAME is null and d.CONSTRAINT_NAME!='PRIMARY'
LEFT JOIN INFORMATION_SCHEMA.TABLES as TABLES_ ON a.TABLE_SCHEMA=TABLES_.TABLE_SCHEMA and a.TABLE_NAME=TABLES_.TABLE_NAME
where a.TABLE_SCHEMA='fastadmin-ganentang'