pgsql 實(shí)現(xiàn)用戶自定義表結(jié)構(gòu)信息獲取
1. 獲取表中普通信息:如字段名,字段類型等
SELECT column_name, data_type, ordinal_position, is_nullable FROM information_schema."columns" WHERE "table_name"='TABLE-NAME' -- 將 'TABLE-NAME' 換成自己的表
2.獲取所有的表和視圖
SELECT table_name, table_type FROM INFORMATION_SCHEMA.tables WHERE table_schema='public' AND table_type IN ('BASE TABLE','VIEW')
3.獲取約束注釋
SELECT obj_description(oid, 'pg_constraint') AS d FROM pg_constraint WHERE conname = constraint_name;
4.獲取表的約束
-- conname 約束名稱 -- contype 約束類型(p:主鍵, f:外鍵, c: 檢查約束, u:唯一約束) -- conkey 約束字段 -- confkey 外鍵字段 -- consrc 檢查約束條件 -- confreltable 外鍵字段引用的表 SELECT conname, contype, conkey, consrc, (SELECT array_agg(column_name::text) FROM INFORMATION_SCHEMA.COLUMNS WHERE ordinal_position = any(conkey) AND table_name= 'TABLE-NAME') AS conkey, (SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE ordinal_position = any(confkey) AND table_name='TABLE-NAME') AS confkey, (SELECT relname FROM pg_class WHERE oid = confrelid) AS confreltable FROM pg_constraint WHERE conrelid=(SELECT oid FROM pg_class WHERE relname ='TABLE-NAME'); -- 將 'TABLE-NAME' 換成自己的表
5.獲取表的觸發(fā)器
SELECT trigger_name, event_manipulation, event_object_table, action_statement, action_orientation, action_timing FROM INFORMATION_SCHEMA.TRIGGERS;
6.獲取字段的注釋
--table_oid 表的oid --col_position 字段的位置 SELECT col_description(table_oid, col_position);
補(bǔ)充:查詢PostgreSQL庫中所有表的表結(jié)構(gòu)信息SQL
我就廢話不多說了,大家還是直接看代碼吧~
select (select relname as comment from pg_class where oid=a.attrelid) as table_name, row_number() over(partition by (select relname as comment from pg_class where oid=a.attrelid) order by a.attnum), 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 '是' else '否' end) as 主鍵約束, (case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='u')>0 then '是' else '否' end) as 唯一約束, (case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='f')>0 then '是' else '否' end) as 外鍵約束, (case when a.attnotnull=true then '是' else '否' 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 in (select tablename from pg_tables where tablename not like 'pg_%' and tablename not like 'sql_%' and schemaname not in(XXXX) and tablename not in(XXXX) )) order by table_name,a.attnum;
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
postgreSQL使用pgAdmin備份服務(wù)器數(shù)據(jù)的方法
這篇文章主要介紹了postgreSQL使用pgAdmin備份服務(wù)器數(shù)據(jù)的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
CVE-2019-9193之PostgreSQL?任意命令執(zhí)行漏洞的問題
這篇文章主要介紹了CVE-2019-9193:PostgreSQL?任意命令執(zhí)行漏洞,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
postgres 使用存儲過程批量插入數(shù)據(jù)的操作
這篇文章主要介紹了postgres 使用存儲過程批量插入數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Postgresql中json和jsonb類型區(qū)別解析
在我們的業(yè)務(wù)開發(fā)中,可能會因?yàn)樘厥狻練v史,偷懶,防止表連接】經(jīng)常會有JSON或者JSONArray類的數(shù)據(jù)存儲到某列中,這個時候再PG數(shù)據(jù)庫中有兩種數(shù)據(jù)格式可以直接一對多或者一對一的映射對象,接下來通過本文介紹Postgresql中json和jsonb類型區(qū)別,需要的朋友可以參考下2024-06-06
PostgreSQL之分區(qū)表(partitioning)
通過合理的設(shè)計(jì),可以將選擇一定的規(guī)則,將大表切分多個不重不漏的子表,這就是傳說中的partitioning。比如,我們可以按時間切分,每天一張子表,比如我們可以按照某其他字段分割,總之了就是化整為零,提高查詢的效能2016-11-11
PostgreSQL中的template0和template1庫使用實(shí)戰(zhàn)
這篇文章主要介紹了PostgreSQL中的template0和template1庫使用實(shí)戰(zhàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Linux CentOS 7安裝PostgreSQL9.3圖文教程
這篇文章主要為大家詳細(xì)介紹了Linux CentOS 7安裝PostgresSQL9.3圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11

