mysql数据字典自动生成语句

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'


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值