标签:des blog http io ar os 使用 for 文件
项目组要出表结构的文档,手写太麻烦,想用slq脚本导出一份。
--查询所有的表字段信息(带表名)
select
(select relname||‘--‘||(select description from pg_description where objoid=oid and objsubid=0) as comment from pg_class where oid=a.attrelid) as table_name,
a.attname as column_name,
format_type(a.atttypid,a.atttypmod) as data_type,
(case when atttypmod-4>0 then atttypmod-4 else 0 end)data_length,
(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype=‘p‘)>0 then ‘Y‘ else ‘N‘ end) as 主键约束,
(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype=‘u‘)>0 then ‘Y‘ else ‘N‘ end) as 唯一约束,
(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype=‘f‘)>0 then ‘Y‘ else ‘N‘ end) as 外键约束,
(case when a.attnotnull=true then ‘Y‘ else ‘N‘ end) as nullable,
col_description(a.attrelid,a.attnum) as comment
from pg_attribute a
where attstattarget=-1 and attrelid in (select oid from pg_class where relname in(select relname from pg_class where relkind =‘r‘ and relname like ‘exg_%‘))
order by table_name,a.attnum;
执行sql语句,然后将查询结果导出
使用英文逗号做分隔符,导出csv文件,可以直接在excle中编辑
使用本地字符集可以防止乱码
?
附:其它sql脚本
--查询表名和描述
select relname as table_name,(select description from pg_description where objoid=oid and objsubid=0) as comment from pg_class where relkind =‘r‘ and relname like ‘exg_%‘ order by table_name;
?
--查询表字段信息
select
a.attname as column_name,
format_type(a.atttypid,a.atttypmod) as data_type,
(case when atttypmod-4>0 then atttypmod-4 else 0 end)data_length,
(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype=‘p‘)>0 then ‘Y‘ else ‘N‘ end) as 主键约束,
(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype=‘u‘)>0 then ‘Y‘ else ‘N‘ end) as 唯一约束,
(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype=‘f‘)>0 then ‘Y‘ else ‘N‘ end) as 外键约束,
(case when a.attnotnull=true then ‘Y‘ else ‘N‘ end) as nullable,
col_description(a.attrelid,a.attnum) as comment
from pg_attribute a
where attstattarget=-1 and attrelid = (select oid from pg_class where relname =‘exg_ms_alarm‘);--表名
标签:des blog http io ar os 使用 for 文件
原文地址:http://www.cnblogs.com/nami/p/4112339.html