项目用了Postgresql 数据库,项目组要出表结构的文档,手写太麻烦,想用slq脚本导出一份。查了一番资料,似乎没有多好的方法。dump方式导出的脚本太乱,没法直接写在word文档里。只能自己写sql查询出表结构,然后利用pgadmin的导出查询结果的功能,能最快的获取一份整个数据库的表结构信息。虽然不能实现全自动的导出文档,但是对整理文档来说,还是节省了不少时间。

--查询所有的表字段信息(带表名)

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中编辑

使用本地字符集可以防止乱码

用excle打开导出的csv文件,因为导出的是所有的表结构信息,所以要先分表,拷贝第一行表头信息,分别插入到每张表的前面(插入行的快捷键自己网上找吧)。

做完上面的步骤,就可以把表结构信息粘帖到word文档里了;

在粘贴完毕后,要选择使用目标格式

表格出来了

这个时候所有的表结构是一张大表格,可以用Ctrl+shift+enter把表格分开

附:其它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');--表名

Postgresql 导出表结构信息的更多相关文章

  1. oracle 导出表结构信息

    直接贴sql: select cols.table_name 表名, cols.column_name 列名, cols.data_type 字段类型, cols.data_length 长度, co ...

  2. 【数据泵】EXPDP导出表结构

    [数据泵]EXPDP导出表结构(真实案例) BLOG文档结构图         因工作需要现需要把一个生产库下的元数据(表定义,索引定义,函数定义,包定义,存储过程)导出到测试库上,本来以为很简单的, ...

  3. MS SQL SERVER导出表结构到Excel

    通过sql语句导出表结构 SELECT 表名 Then D.name Else '' End, 表说明 Then isnull(F.value,'') Else '' End, 字段序号 = A.co ...

  4. PowerDesigner连接Oracle并导出表结构

    环境:Oracle 11G(远程) + win32_11gR2_client + PowerDesigner 15 一.下载.安装.配置 1.下载地址 win32_11gR2_client客户端下载地 ...

  5. sql server 导出表结构到 word

    ------导出表结构语句1.执行以下查询 SELECT    表名       = case when a.colorder=1 then d.name else '' end,    表说明    ...

  6. SQL 导出表结构到Excel

    SQL 导出表结构到Excel SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号 = a ...

  7. SQLServer查询所有库表结构信息

    1.查询数据库中的所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.查询某个数据库中所有的表名: SELECT Name FR ...

  8. c#通过oledb获取excel文件表结构信息

    这个问题来自论坛提问,同理可以获得access等数据库的表结构信息. using System; namespace ConsoleApplication11 { class Program { pu ...

  9. 从Mysql数据库中导入导出表结构

    1.从Mysql数据库中导入sql表 很简单,只需要一个命令即可搞定:[root@localhost ~]# mysql -uroot -piweb_xxx_mysql iweb < modif ...

随机推荐

  1. 微信小程序开发初体验

    微信小程序上线几天了,趁着周末补了一下JS,然后今天参照文档和教程写了个小demo 文档地址       教程地址 看文档就看了一点时间,因为以前没接触过JS框架,但是接触过PHP框架= = ,所以理 ...

  2. Scala 中下划线的用途

    转载自:https://my.oschina.net/leejun2005/blog/405305 Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之 ...

  3. Mac OSX中的@executable_path, @load_path和@rpath的理解

    本文转载自:https://wincent.com/wiki/@executable_path,_@load_path_and_@rpath.个人觉得写的很不错,简洁明了. Absolute path ...

  4. 7.10 数据注解特性--NotMapped

    NotMapped特性可以应用到领域类的属性中,Code-First默认的约定,是为所有带有get,和set属性选择器的属性创建数据列.. NotManpped特性打破了这个约定,你可以使用NotMa ...

  5. DDL/DML是什么?

    DDL:(Data Definition Language)数据库定义语言 它是定义数据库的语言, 里面包含: CREATE ALTER DROP TRUNCATE COMMENT RENAME DM ...

  6. httpCookie与Cookie安全

    Web 应用程序使用的 Cookie 个人认为这里设置的cookie与访问cookie的安全性关联大一点,配置节如下 <httpCookies domain="String" ...

  7. 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(一)

    (一)前言 本文主要介绍了实践部署AzurePack的Website Cloud的过程.在部署之前, 首先要对AzurePack有个基本的了解.   Azure Pack是微软的私有云方案,具有弹性. ...

  8. Eclipse中Python开发环境搭建

    Eclipse中Python开发环境搭建  目 录  1.背景介绍 2.Python安装 3.插件PyDev安装 4.测试Demo演示 一.背景介绍 Eclipse是一款基于Java的可扩展开发平台. ...

  9. c#模拟js escape方法

    public static string Escape(string s) { StringBuilder sb = new StringBuilder(); byte[] ba = System.T ...

  10. HTTP 协议整理(转)

    HTTP 协议 作为web开发人员,了解一些http协议的知识很有必要.本文简单介绍了HTTP协议的知识,若有错误的地方,望大家斧正. 1.HTTP协议是什么? http协议是一个应用层的协议.规定了 ...