以下メモ書きです。
Postgresにてテーブル一覧とかカラム一覧とか各種情報を取得するためのSQLです。
間違えていたら教えてください。m(_ _)m
[テーブル一覧取得]
select relnameas TABLE_NAMEfrom pg_stat_user_tables
[カラム一覧]
select *from information_schema.columnswhere table_catalog='データベース名'and table_name='テーブル名'orderby ordinal_position;
[プライマリーキー情報取得]
selectccu.column_nameas COLUMN_NAMEfrom information_schema.table_constraints tc,information_schema.constraint_column_usage ccuwheretc.table_catalog='データベース名'andtc.table_name='テーブル名'andtc.constraint_type='PRIMARY KEY'andtc.table_catalog=ccu.table_catalogandtc.table_schema=ccu.table_schemaandtc.table_name=ccu.table_nameandtc.constraint_name=ccu.constraint_name
[テーブルのコメントを取得]
select psut.relnameas TABLE_NAME,pd.descriptionas TABLE_COMMENTfrom pg_stat_user_tables psut,pg_description pdwherepsut.relname='テーブル名'andpsut.relid=pd.objoidandpd.objsubid=0
[カラムのコメントを取得]
selectpsat.relnameas TABLE_NAME,pa.attnameas COLUMN_NAME,pd.descriptionas COLUMN_COMMENTfrom pg_stat_all_tables psat,pg_description pd,pg_attribute pawherepsat.schemaname=(select schemanamefrom pg_stat_user_tableswhere relname ='テーブル名')andpsat.relname='テーブル名'andpsat.relid=pd.objoidandpd.objsubid<>0andpd.objoid=pa.attrelidandpd.objsubid=pa.attnumorderbypd.objsubid
================================
過去の記事については、以下のページからご参照下さい。
引用をストックしました
引用するにはまずログインしてください
引用をストックできませんでした。再度お試しください
限定公開記事のため引用できません。