SQL Server:
SELECT TOP (100) PERCENT
CASE WHEN a.colorder = 1 THEN d .name ELSE '' END AS 表名, CASE WHEN a.colorder = 1 THEN isnull(f.value, '')
ELSE '' END AS 表说明, a.colorder AS 字段序号, a.name AS 字段名, CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity')
= 1 THEN '√' ELSE '' END AS 标识, CASE WHEN EXISTS
(SELECT 1 FROM dbo.sysindexes si INNER JOIN
dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid INNER JOIN
dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN
dbo.sysobjects so ON so.name = so.name AND so.xtype = 'PK'
WHERE sc.id = a.id AND sc.colid = a.colid) THEN '√' ELSE '' END AS 主键, b.name AS 类型, a.length AS 长度, COLUMNPROPERTY(a.id, a.name,
'PRECISION') AS 精度, ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS 小数位数,
CASE WHEN a.isnullable = 1 THEN '√' ELSE '' END AS 允许空, ISNULL(e.text, '') AS 默认值, ISNULL(g.value, '') AS 字段说明, d.crdate AS 创建时间,
CASE WHEN a.colorder = 1 THEN d .refdate ELSE NULL END AS 更改时间
FROM dbo.syscolumns AS a LEFT OUTER JOIN
dbo.systypes AS b ON a.xtype = b.xusertype INNER JOIN
dbo.sysobjects AS d ON a.id = d.id AND d.xtype = 'U' AND d.status >= 0 LEFT OUTER JOIN
dbo.syscomments AS e ON a.cdefault = e.id LEFT OUTER JOIN
sys.extended_properties AS g ON a.id = g.major_id AND a.colid = g.minor_id LEFT OUTER JOIN
sys.extended_properties AS f ON d.id = f.major_id AND f.minor_id = 0
ORDER BY d.name, 字段序号
MYSQL:
use `contacts`;
select distinct
case when a.ordinal_position=1 then a.TABLE_NAME else '' end 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='contacts'
order by a.table_name
use `contacts`;
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='contacts'
order by 1