您好,登錄后才能下訂單哦!
怎么在postgresql中獲取所有的表名和字段名?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
獲取表名及注釋:
select relname as tabname,cast(obj_description(relfilenode,'pg_class') as varchar) as comment from pg_class c where relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%' order by relname
過濾掉分表:
加條件 and relchecks=0
即可
獲取字段名、類型、注釋、是否為空:
SELECT col_description(a.attrelid,a.attnum) as comment,format_type(a.atttypid,a.atttypmod) as type,a.attname as name, a.attnotnull as notnull FROM pg_class as c,pg_attribute as a where c.relname = '表名' and a.attrelid = c.oid and a.attnum>0
補充:PostgreSQL查詢表主鍵及注釋內容
網上關于pgSql獲取表主鍵的內容都是千篇一律,并且對于存在多主鍵的場景不支持。
附上測試后可獲取多個主鍵字段值的SQL
SELECT string_agg(DISTINCT t3.attname,',') AS primaryKeyColumn ,t4.tablename AS tableName , string_agg(cast(obj_description(relfilenode,'pg_class') as varchar),'') as comment FROM pg_constraint t1 INNER JOIN pg_class t2 ON t1.conrelid = t2.oid INNER JOIN pg_attribute t3 ON t3.attrelid = t2.oid AND array_position(t1.conkey,t3.attnum) is not null INNER JOIN pg_tables t4 on t4.tablename = t2.relname INNER JOIN pg_index t5 ON t5.indrelid = t2.oid AND t3.attnum = ANY (t5.indkey) LEFT JOIN pg_description t6 on t6.objoid=t3.attrelid and t6.objsubid=t3.attnum WHERE t1.contype = 'p' AND length(t3.attname) > 0 AND t2.oid = '表名' :: regclass group by t4.tablename
目前只找到了獲取指定表的主鍵信息,對于批量獲取沒有找到。
看完上述內容,你們掌握怎么在postgresql中獲取所有的表名和字段名的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。