PostgreSQLいろいろ

主キーしゅきしゅき


CREATE TABLE hoge
(
hage integer NOT NULL,
PRIMARY KEY (hoge_column)
)

で適当な名前がついた主キーが出来る。
pgAdminからやるばあいは制約を右クリック。

SQLでやるのがイイネ。

アンバウンドカーソルを定義するけど開かない、という方法は無いのか。

open cur1 for select * from tables;
のopenが無い版。

関数の中で動的にSQLを生成してFORで回したいときにやり方がわかんない。

追記

これか。


for rec in execute ''select * from '' || s loop
raise debug ''key = % data1 = %'', rec.key, rec.data1;
end loop;

http://homepage2.nifty.com/sak/w_sak3/doc/sysbrd/psql_k22.htm
思えばくらいに社会に出てから15年SAK Streetのお世話になりっぱなしである。
BABAQ先生よりもお世話になっているかもしれん。

PL/pgSQLインタープリター的に使う方法はないのか

prepare とか create function とかせずに使い捨てのPL/pgSQLを使いたい。


LOOP
-- 何らかの演算
IF count > 100000 THEN
EXIT; -- ループの終了
END IF;
insert into customer(customer_id,customer_name
) values ( count , count );
count=count +1;
END LOOP;
だけで回したい。
無理なのかな。

=>

ERROR: operator does not exist: integer => integer
LINE 3: where hoge1 => hoge2

=> というオペレーターは無いらしい・・・
>= なら良いらしい。

https://www.postgresql.jp/document/8.0/html/functions-comparison.html