Create Role
CREATE ROLE name [ WITH ] LOGIN PASSWORD 'password'
-- Example
create role saru with login password 'monkey';Create SCHEMA
CREATE SCHEMA schema_nameCreate table
CREATE TABLE test
(
a int PRIMARY KEY,
b int,
c int
);create table test
(
a int,
b int,
c int,
constraint pk_test primary key (
a
)
);Create tmp table
CREATE TEMP TABLE temp_sample1 ( i int);Drop primary key
ALTER TABLE test
DROP CONSTRAINT pk_test;-- 主キーを作成するときに自分で名前を付けていなければ、テーブル名_pkeyでOK
ALTER TABLE test
DROP CONSTRAINT test_pkey;Drop NOT NULL
ALTER TABLE test ALTER COLUMN a DROP NOT NULL;Add primary key
ALTER TABLE test ADD CONSTRAINT test_pkey PRIMARY KEY(a, b);メタコマンド
ユーザ一覧を表示
postgres=# \duデータベース一覧を表示
postgres=# \l接続中のデータベースの情報を表示
postgres=# \conninfoテーブル一覧を表示
postgres=# \zテーブル定義を確認
postgres=# \d tablenameカレントディレクトリ変更
postgres=# \cd directoryCSV 形式のファイルをテーブルに挿入
postgres=# \copy tablename from filename DELIMITER AS ','postgres=# \copy TABLE_NAME from 'CSV_FILE_NAME.csv' (format csv, header false, encoding utf8);ファイルからコマンドを実行
postgres=# \i filename.sqlシェル上のコマンドを使いたい場合
postgres=# \! commandベントリスト表示
postgres=# \dy関数リスト表示
postgres=# \dfインデックスリスト表示
postgres=# \diスキーマリスト表示
postgres=# \dn全スキーマのテーブルをリスト表示
postgres=# \dt *.*全データ型をリスト表示
postgres=# \dT+viewをリスト表示
postgres=# \dvパーティショニング
親テーブル
create table zenkoku_chiho_kokyodantai_code (
dantai_code varchar(6)
,todofuken_kanji varchar(4)
,shichoson_kanji varchar(10)
,todofuken_kana varchar(30)
,shichoson_kana varchar(30)
)
partition by list (todofuken_kanji)
;パティションテーブル作成
create table zenkoku_chiho_kokyodantai_code_1 partition of zenkoku_chiho_kokyodantai_code for values in ('北海道');
create table zenkoku_chiho_kokyodantai_code_2 partition of zenkoku_chiho_kokyodantai_code for values in ('青森県');
create table zenkoku_chiho_kokyodantai_code_3 partition of zenkoku_chiho_kokyodantai_code for values in ('岩手県');実行計画
EXPLAIN
sql文
--------------------------------------------
Seq Scan on <table_name> (cost=0.00..1.60 rows=60 width=28)- Scan
- Index Scan Index を使った実行
- Seq Scan フルスキャン
- cost 指標 相対的に評価する
- rows 実際の操作行数
- width 取得される行の平均サイズ
