好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > pgsql 實現用戶自定義表結構信息獲取

pgsql 實現用戶自定義表結構信息獲取

熱門標簽:辦公外呼電話系統 美容工作室地圖標注 地圖標注和圖片名稱的區別 海豐有多少商家沒有地圖標注 外呼調研系統 合肥公司外呼系統運營商 打電話智能電銷機器人授權 漯河外呼電話系統 重慶自動外呼系統定制

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.獲取表的觸發器

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);

補充:查詢PostgreSQL庫中所有表的表結構信息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;

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • pgsql之pg_stat_replication的使用詳解
  • pgsql 如何刪除仍有活動鏈接的數據庫
  • pgsql的UUID生成函數實例
  • pgsql 如何手動觸發歸檔
  • pgsql鎖表后kill進程的操作
  • PGSQL 實現把字符串轉換成double類型(to_number())
  • pgsql之create user與create role的區別介紹

標簽:株洲 來賓 珠海 錦州 烏海 衡陽 蚌埠 晉城

巨人網絡通訊聲明:本文標題《pgsql 實現用戶自定義表結構信息獲取》,本文關鍵詞  pgsql,實現,用戶,自定義,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《pgsql 實現用戶自定義表結構信息獲取》相關的同類信息!
  • 本頁收集關于pgsql 實現用戶自定義表結構信息獲取的相關信息資訊供網民參考!
  • 推薦文章