MySQL获取全部字段信息

1 根据库名获取所有的库和表字段的基本信息

SELECT
    C.TABLE_SCHEMA AS '库名',
    T.TABLE_NAME AS '表名',
    T.TABLE_COMMENT AS '表注释',
    C.COLUMN_NAME AS '列名',
    C.COLUMN_COMMENT AS '列注释',
    C.ORDINAL_POSITION AS '列的排列顺序',
    C.COLUMN_DEFAULT AS '默认值',
    C.IS_NULLABLE AS '是否为空',
    C.DATA_TYPE AS '数据类型',
    C.CHARACTER_MAXIMUM_LENGTH AS '字符最大长度',
    C.NUMERIC_PRECISION AS '数值精度(最大位数)',
    C.NUMERIC_SCALE AS '小数精度',
    C.COLUMN_TYPE AS 列类型,
    C.COLUMN_KEY 'KEY',
    C.EXTRA AS '额外说明'
FROM
    information_schema.`TABLES` T
LEFT JOIN information_schema.`COLUMNS` C ON T.TABLE_NAME = C.TABLE_NAME
AND T.TABLE_SCHEMA = C.TABLE_SCHEMA
WHERE
    T.TABLE_SCHEMA = 'zs' 
ORDER BY
    C.TABLE_NAME,
    C.ORDINAL_POSITION;

说明:过滤存在必填,默认值等。
重新生成SQL

SELECT GROUP_CONCAT(CONCAT('ALTER TABLE "public"."',T.TABLE_NAME,'"', 
 ' ALTER COLUMN "',C.COLUMN_NAME ,'" SET DEFAULT ', C.COLUMN_DEFAULT)  SEPARATOR ';' )

FROM
    information_schema.`TABLES` T
LEFT JOIN information_schema.`COLUMNS` C ON T.TABLE_NAME = C.TABLE_NAME
AND T.TABLE_SCHEMA = C.TABLE_SCHEMA
WHERE
    T.TABLE_SCHEMA = 'zs'  and c.COLUMN_DEFAULT is not null and c.COLUMN_DEFAULT!='' and C.TABLE_NAME like 'sys%'
ORDER BY
    C.TABLE_NAME,
    C.ORDINAL_POSITION;

参考:https://www.cnblogs.com/covet/p/10290894.html

2 获取全部自增长

SHOW TABLE STATUS where  auto_increment is not null;

3获取全部索引

select TABLE_NAME, INDEX_NAME, GROUP_CONCAT(COLUMN_NAME) as INDEX_COLUMN
from
information_schema.statistics
where
table_schema='zs' and COLUMN_NAME!='id' and INDEX_NAME!='PRIMARY' and (TABLE_NAME like 'f%' or   TABLE_NAME like 'sys%' )
GROUP BY TABLE_NAME, INDEX_NAME;

4获取特定类型列

select TABLE_NAME,column_name,column_comment,data_type 

from information_schema.columns 

where   table_schema='zs' and data_type='geometry';
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值