項目上需要整理目前數據庫的數據字典文檔。項目不規范,這種文檔只要后期來補。這么多張表,每個字段都寫到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[1]=attnum and contype='p')>0 then 'Y' else 'N' end), (case when (select count(*) from pg_constraint where conrelid=a.attrelid and conkey[1]=attnum and contype='u')>0 then 'Y' else 'N' end), (case when (select count(*) from pg_constraint where conrelid=a.attrelid and conkey[1]=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][0]; print @table[@i][1]; 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][0] = @table_name begin --詳細 print '<tr><td>'; print @att[@j][1]; print '</td><td>'; print @att[@j][2]; print '</td><td>'; print @att[@j][3]; print '</td><td>'; print @att[@j][4]; print '</td><td>'; print @att[@j][5]; print '</td><td>'; print @att[@j][6]; print '</td><td>'; print @att[@j][7]; print '</td><td>'; print @att[@j][8]; 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; */
