以下メモ書きです。
Postgresにてテーブル一覧とかカラム一覧とか各種情報を取得するためのSQLです。
間違えていたら教えてください。m(_ _)m
[テーブル一覧取得]
select relname as TABLE_NAME from pg_stat_user_tables
[カラム一覧]
select * from information_schema.columns where table_catalog='データベース名' and table_name='テーブル名' order by ordinal_position;
[プライマリーキー情報取得]
select ccu.column_name as COLUMN_NAME from information_schema.table_constraints tc ,information_schema.constraint_column_usage ccu where tc.table_catalog='データベース名' and tc.table_name='テーブル名' and tc.constraint_type='PRIMARY KEY' and tc.table_catalog=ccu.table_catalog and tc.table_schema=ccu.table_schema and tc.table_name=ccu.table_name and tc.constraint_name=ccu.constraint_name
[テーブルのコメントを取得]
select psut.relname as TABLE_NAME ,pd.description as TABLE_COMMENT from pg_stat_user_tables psut ,pg_description pd where psut.relname='テーブル名' and psut.relid=pd.objoid and pd.objsubid=0
[カラムのコメントを取得]
select psat.relname as TABLE_NAME, pa.attname as COLUMN_NAME, pd.description as COLUMN_COMMENT from pg_stat_all_tables psat ,pg_description pd ,pg_attribute pa where psat.schemaname=(select schemaname from pg_stat_user_tables where relname = 'テーブル名') and psat.relname='テーブル名' and psat.relid=pd.objoid and pd.objsubid<>0 and pd.objoid=pa.attrelid and pd.objsubid=pa.attnum order by pd.objsubid
================================
過去の記事については、以下のページからご参照下さい。
- いろいろ備忘録日記まとめ