table

create table user_table (
id bigserial primary key,
name varchar(255)
);

どんな sequence があるか

demo=# SELECT * FROM information_schema.sequences;
 sequence_catalog | sequence_schema |    sequence_name     | data_type | numeric_precision | numeric_precision_radix | numeric_scale | start_value | minimum_value |    maximum_value    | increment | cycle_option
------------------+-----------------+----------------------+-----------+-------------------+-------------------------+---------------+-------------+---------------+---------------------+-----------+--------------
 demo             | public          | user_table_id_seq    | bigint    |                64 |                       2 |             0 | 1           | 1             | 9223372036854775807 | 1         | NO
 demo             | public          | logging_event_id_seq | bigint    |                64 |                       2 |             0 | 1           | 1             | 9223372036854775807 | 1         | NO
(2 rows)

Sequence 値の操作

demo=# SELECT nextval('user_table_id_seq');
 nextval
---------
       1
(1 row)

demo=# SELECT currval('user_table_id_seq');
 currval
---------
       1
(1 row)

demo=# SELECT nextval('user_table_id_seq');
 nextval
---------
       2
(1 row)

demo=# SELECT setval('user_table_id_seq',123);
 setval
--------
    123
(1 row)

demo=# SELECT currval('user_table_id_seq');
 currval
---------
     123
(1 row)

demo=# SELECT nextval('user_table_id_seq');
 nextval
---------
     124
(1 row)

demo=# SELECT nextval('user_table_id_seq');
 nextval
---------
     125
(1 row)

demo=#

insert 文と sequence

postgresql では、oracle のように sequence を副問合せで生成する必要なし。単に bigserial 列に値を設定しなければ良い

demo=# SELECT nextval('user_table_id_seq');
 nextval
---------
     125
(1 row)

demo=# insert into user_table (name) values ('Hiromi');
INSERT 0 1
demo=# insert into user_table (name) values ('Kana');
INSERT 0 1
demo=# select * from user_table;
 id  |  name
-----+--------
 126 | Hiromi
 127 | Kana
(2 rows)

bigserial って id として足りるの?



GIS


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2014-04-16 (水) 01:19:25 (3656d)
Short-URL: https://at-sushi.com:443/pukiwiki/index.php?cmd=s&k=0119e8eaf4
ISBN10
ISBN13
9784061426061