SAK 図書館
PostgreSQL 編17 - 問い合わせ、テーブルリスト、ユーザリスト、テーブル構造
■テーブルリスト
・テーブル一覧などを問い合わせることができる。
(すべてのテーブル、全てのテーブル、登録されているテーブル、表一覧、テーブル名表示)
(すべてのビュー、全てのビュー、登録されているビュー、ビュー一覧)
(すべてのパッケージ、全てのパッケージ、登録されているパッケージ、パッケージ一覧)
(すべてのストアドプロシジャ、全てのストアドプロシジャ)
(登録されているストアドプロシジャ、ストアドプロシジャ一覧)
select * from pg_tables where not tablename like 'pg%' order by tablename;
tablename | tableowner | hasindexes | hasrules | hastriggers
-----------+------------+------------+----------+-------------
test2m | sak | f | f | f
test3m | sak | f | f | f
testm | sak | f | f | f
select * from pg_tables where tableowner = 'sak' order by tablename;
・目的に合わせて次のカタログが利用できる。
pg_indexes
pg_rules
pg_tables
pg_user
pg_views
select * from pg_tables where tablename like 'pg%' order by tablename;
pg_aggregate
pg_am
pg_amop
pg_amproc
pg_attrdef
pg_attribute
pg_class
pg_database
pg_description
pg_group
pg_index
pg_inherits
pg_language
pg_largeobject
pg_listener
pg_opclass
pg_operator
pg_proc
pg_relcheck
pg_rewrite
pg_shadow
pg_statistic
pg_trigger
pg_type
pg_xactlock
■データベースリスト
・データベースリストを問い合わせることができる。
select datname from pg_database order by datname;
datname
-----------
sak
template0
template1
■ユーザリスト
・ユーザリストを問い合わせることができる。
select usename from pg_user order by usename;
usename
---------------
administrator
■テーブル構造リスト、テーブルレイアウト
・特定のテーブルの定義構造を問い合わせることができる。
(テーブル構造取得、表構造取得)
select attname, atttypid, attlen
from pg_attribute
where attnum > 0
and attrelid = (select relfilenode from pg_class where relname = 'testm')
order by attnum
;
attname | atttypid | attlen
---------+----------+--------
key | 1042 | -1
data1 | 20 | 8
data2 | 20 | 8
data3 | 20 | 8
■項目定義サーチ、テーブル列名参照(含まれる文字検索)
・同じ項目名に対する不揃いな定義をなくするには、項目でテーブル構造を
検索して、既存の項目定義を調べると良い。
この例では、「受注数」と言う文字列が含まれる項目定義の一覧が全て
小文字で得られる。
select attname, atttypid, attlen,
(select relname from pg_class where relfilenode = a.attrelid)
from pg_attribute a
where attnum > 0
and attname like 'key%'
order by attnum
;
attname | atttypid | attlen | ?column?
---------+----------+--------+----------
key | 1042 | -1 | testm
key | 1042 | -1 | test2m
key | 1042 | -1 | test3m
■PostgreSQL 編、JAVA Servlet、JSP 編資料
■MySQL 編資料
■SQL 基礎編資料
■SQL 基礎実地編資料
■SQL チューニング編資料
■Oracle PL/SQL 編資料