项目上需要整理目前数据库的数据字典文档。项目不规范,这种文档只要后期来补。这么多张表,每个字段都写到word文档里真心头大。就算前面写了个查询表结构的sql,但是最后整理到word里还是感觉有点麻烦。以前写过一个oracle直接生成表结构的html文档,所以现在也想再弄个postgresql 版本的。查了一番文档,发现pg9.4不支持写文件。无奈放弃。最后选了一个这种方案,利用sql脚本中打印消息的功能。把生成的html文档打印出来,最后拷贝html文档代码到文本文件中保存,虽然比oracle那个麻烦了点,总算能得到想要的html文档了。

slq脚本:

--1.0
--2015-11-30
--postgresql-9.4.5
--打印出数据字典html
--执行完毕,在pgAdmin的消息窗口,把打印内容拷贝到文本文件中,替换掉多余的输出:[PGSCRIPT ] ,删除头部的[QUERY ]及打印出的查询语句,
--最后把文件另存为.html文件。
--用浏览器打开保存的网页,然后拷贝页面内容到word文档中,下面整理格式就可以了
--注意:
--脚本里包含了详细版,和简版两个版本的数据字典,使用的时候注意切换到对应的标题
--'<tr><td>列名</td><td>类型</td><td>长度</td><td>主键约束</td><td>唯一约束</td><td>外键约束</td><td>可否为空</td><td>描述</td></tr>';
--'<tr><td>列名</td><td>类型</td><td>描述</td></tr>';
--2016-2-16 修正表字段注释(描述)为空,字段不打印的问题
begin
--查询表名
set @table = select distinct relname, relname||'('||(select description from pg_description where objoid = oid and objsubid = 0) ||'表'||')' as table_name
from pg_class c,pg_attribute a
where c.oid=a.attrelid
and attstattarget=-1
and attrelid in(select oid from pg_class where relname in (select relname as table_name from pg_class where relkind='r' and relname like 'exg_%' order by relname))
order by table_name;
--数据字典(详细版):列名 类型 长度 主键约束 唯一约束 外键约束 可否为空 描述
set @att = select (select relname from pg_class where oid=a.attrelid) as table_name,
a.attname,
format_type(a.atttypid,a.atttypmod),
(case when atttypmod-4>0 then atttypmod-4 else 0 end),
(case when (select count(*) from pg_constraint where conrelid=a.attrelid and conkey[]=attnum and contype='p')>0 then 'Y' else 'N' end),
(case when (select count(*) from pg_constraint where conrelid=a.attrelid and conkey[]=attnum and contype='u')>0 then 'Y' else 'N' end),
(case when (select count(*) from pg_constraint where conrelid=a.attrelid and conkey[]=attnum and contype='f')>0 then 'Y' else 'N' end),
(case when a.attnotnull=true then 'Y' else 'N' end),
col_description(a.attrelid,a.attnum)
from pg_attribute a where attstattarget=-1 and attrelid in(select oid from pg_class where relname in (select relname as table_name from pg_class where relkind='r' and relname like 'exg_%' order by relname))
order by table_name,attnum;
/*
--数据字典(简版):列名 类型 描述
set @att = select (select relname from pg_class where oid=a.attrelid) as table_name,
,a.attname
,format_type(a.atttypid,a.atttypmod)
,col_description(a.attrelid,a.attnum)
from pg_attribute a
where attstattarget=-1
and attrelid in(select oid from pg_class where relname in (select relname as table_name from pg_class where relkind='r' and relname like 'exg_%' order by relname))
order by table_name,attnum;
*/
--打印html文档
print '<!DOCTYPE html>';
print '<html>';
print '<head>';
print '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
print '<title>数据字典</title>';
print '<style type="text/css">';
print 'table { border-collapse: collapse; border-spacing: 0;}';
print 'table td {border: solid 1px #000;}';
print '</style>'; set @i=0;
while @i < lines(@table)
begin
set @table_name = @table[@i][];
print @table[@i][];
print '<table>';
print '<tr><td>列名</td><td>类型</td><td>长度</td><td>主键约束</td><td>唯一约束</td><td>外键约束</td><td>可否为空</td><td>描述</td></tr>';
--print '<tr><td>列名</td><td>类型</td><td>描述</td></tr>';
set @j=0;
while @j < lines(@att)
begin
if @att[@j][] = @table_name
begin
--详细
print '<tr><td>';
print @att[@j][];
print '</td><td>';
print @att[@j][];
print '</td><td>';
print @att[@j][];
print '</td><td>';
print @att[@j][];
print '</td><td>';
print @att[@j][];
print '</td><td>';
print @att[@j][];
print '</td><td>';
print @att[@j][];
print '</td><td>';
print @att[@j][];
print '</td></tr>';
--简版
/*print '<tr><td>';
print @att[@j][1];
print '</td><td>';
print @att[@j][2];
print '</td><td>';
print @att[@j][3];
print '</td></tr>';*/
end
set @j=@j+1;
end
print '</table>';
set @i=@i+1;
end
end --附:
/*
--数据字典--详细版
select
(select relname ||'--'||(select description from pg_description where objoid = oid and objsubid = 0) from pg_class where oid=a.attrelid) as 表名,
a.attname as 列名,
format_type(a.atttypid,a.atttypmod) as 类型,
(case when atttypmod-4>0 then atttypmod-4 else 0 end) as 长度,
(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 可否为空,
col_description(a.attrelid,a.attnum) as 描述
from pg_attribute a where attstattarget=-1 and attrelid in(select oid from pg_class where relname in (select relname as table_name from pg_class where relkind='r' and relname like 'exg_%' order by relname))
order by 表名,attnum; --数据字典--简版
select
(select relname from pg_class where oid=a.attrelid) as table_name,
(select (select description from pg_description where objoid = oid and objsubid = 0) ||'表'||'('||relname ||')' from pg_class where oid=a.attrelid) as 表名,
a.attname as 列名,
format_type(a.atttypid,a.atttypmod) as 类型,
col_description(a.attrelid,a.attnum) as 描述
from pg_attribute a where attstattarget=-1 and attrelid in(select oid from pg_class where relname in (select relname as table_name from pg_class where relkind='r' and relname like 'exg_%' order by relname))
order by table_name,attnum;
*/

postgresql 导出数据字典文档的更多相关文章

  1. Mysql数据库导出数据字典文档Word或者HTML的3个工具

    最近需要将Mysql的数据库导出一份Word的文档出来,本文记录调研后几个可用的工具和方法: 阿里云DMS工具导出 适用于存储在阿里云RDS服务中的Mysql数据库 导出格式支持:Word.Excel ...

  2. struts2中利用POI导出Excel文档并下载

    1.项目组负责人让我实现这个接口,因为以前做过类似的,中间并没有遇到什么太困难的事情.其他不说,先上代码: package com.tydic.eshop.action.feedback; impor ...

  3. .NET通过调用Office组件导出Word文档

    .NET通过调用Office组件导出Word文档 最近做项目需要实现一个客户端下载word表格的功能,该功能是用户点击"下载表格",服务端将该用户的数据查询出来并生成数据到Word ...

  4. 使用PHP导出Word文档的原理和实例

    PHP操作Word文档的方法有很多,这里再为大家提供一种方法. 原理   一般,有2种方法可以导出doc文档,一种是使用com,并且作为php的一个扩展库安装到服务器上,然后创建一个com,调用它的方 ...

  5. C# 导出word文档及批量导出word文档(1)

         这里用到了两个dll,一个是aspose.word.dll,另外一个是ICSharpCode.SharpZipLib.dll,ICSharpCode.SharpZipLib.dll是用于批量 ...

  6. 使用OpenXml实现生成数据字典文档(beta)

    最近项目在走验收流程,之前没有仔细看SOW文档,发现需要补好多份文档,其中就有数据字典,项目组不愿意花时间太多的时间弄这些文档,也不希望以后还要重复劳动力,最终决定做一个工具,方便自己生成数据字典文档 ...

  7. C# 导出word文档及批量导出word文档(4)

          接下来是批量导出word文档和批量打印word文件,批量导出word文档和批量打印word文件的思路差不多,只是批量打印不用打包压缩文件,而是把所有文件合成一个word,然后通过js来调用 ...

  8. 使导出excle文档实现ALT+Enter的效果()

    JAVA中输入什么转义字符,使导出excle文档实现ALT+Enter的效果?或者有没有其他方法可以实现. 20 JAVA中输入什么转义字符,使导出excle文档实现ALT+Enter的效果?或者有没 ...

  9. C#导出Word文档开源组件DocX

    1.帮助文档,这东西找了很久,而且它版本很旧,还是英文,W8.1系统上打不开 http://download.csdn.net/detail/zuofangyouyuan/7673573 2.开源网址 ...

随机推荐

  1. CentOS 安装OciLib 4.2.1 (Linux)

    项目要用oracle , Windows的 OciLib 好弄, 今天安装到linux下 ,编译老是出错,最后几行如下: checking for OCILIB install path... /us ...

  2. SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...

  3. 【Win 10 应用开发】手写识别

    记得前面(忘了是哪天写的,反正是前些天,请用力点击这里观看)老周讲了一个14393新增的控件,可以很轻松地结合InkCanvas来完成涂鸦.其实,InkCanvas除了涂鸦外,另一个大用途是墨迹识别, ...

  4. C++服务器开发之基于对象的编程风格

    Thread.h #ifndef _THREAD_H_ #define _THREAD_H_ #include <pthread.h> #include <boost/functio ...

  5. 前端MVC学习总结(二)——AngularJS验证、过滤器、指令

    一.验证 angularJS中提供了许多的验证指令,可以轻松的实现验证,只需要在表单元素上添加相应的ng属性,常见的如下所示: <input Type="text" ng-m ...

  6. 你真的会玩SQL吗?让人晕头转向的三值逻辑

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  7. docker对数据卷容器进行备份

    转载请注明出处 官网的数据以及各大博客都没有对这个的具体说明,本人也是理解了好久. 我们使用docker的过程中,使用共享的数据卷是经常的,那么.我们要怎么进行备份呢?   首先,我们得了解下面4个命 ...

  8. react初始(1)

    前言:react框架的出现是因为Facebook在建设Instagram交流平台的时候要处理大量的数据流,但是行业流行的MVC框架并不能适合Facebook公司的要求,他们就组织了自己的人力来开发re ...

  9. 一种开发模式:ajax + ashx + UserControl

    一.ajax+ashx模式的缺点     在web开发过程中,为了提高网站的用户体验,或多或少都会用到ajax技术,甚至有的网站全部采用ajax来实现,大量使用ajax在增强用户体验的同时会带来一些负 ...

  10. C# async/await 使用总结

    今天搞这两个关键字搞得有点晕,主要还是没有彻底理解其中的原理. 混淆了一个调用异步方法的概念: 在调用异步方法时,虽然方法返回一个 Task,但是其中的代码已经开始执行.该方法在调用时,即刻执行了一部 ...