本篇文章給大家分享的是有關(guān) 怎么在pgsql中利用自定義表結(jié)構(gòu)獲取信息,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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;
以上就是 怎么在pgsql中利用自定義表結(jié)構(gòu)獲取信息,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
標(biāo)題名稱:怎么在pgsql中利用自定義表結(jié)構(gòu)獲取信息-創(chuàng)新互聯(lián)
文章鏈接:http://www.rwnh.cn/article44/ceishe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、企業(yè)網(wǎng)站制作、外貿(mào)建站、App開發(fā)、服務(wù)器托管、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)