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';