../../image/benilogo.gifPostgreSQL について


PostgreSQL の serial 型について。
serial型

   


実際に試してみればすぐわかる。
serial型の作成
beniya=# create table a_serial ( s_id serial primary key, s_text char(20));
NOTICE:  CREATE TABLE will create implicit sequence 'a_serial_s_id_seq' for SERIAL column 'a_serial.s_id'
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 'a_serial_pkey' for table 'a_serial'
CREATE

適当に serial の
テーブルを作る。




   
確かにマニュアルに書いてあるように colname integer DEFAULT nextval('tablename_colname_seq') UNIQUE NOT NULL
の sequence なる一行の特殊なテーブル? が出来ている。 シーケンス操作関数 nextval

create table a_serial (
    s_id  integer DEFAULT nextval('a_serial_s_id_seq') UNIQUE NOT NULL primary key,
       s_text char(20)
);
  • serial 型で定義すると言うのは
    ←と同じことのようです。

テーブル名_カラム名_seq この場合は a_serial_s_id_seq の type sequence って これマニュアルを探したけれど見つけられなかった。
シーケンス操作関数 -> CREATE SEQUENCE で 詳細が載っていた。
           List of relations
       Name        |   Type   | Owner
-------------------+----------+--------
 a_attr1           | table    | beniya
 a_serial          | table    | beniya
 a_serial_s_id_seq | sequence | beniya
 a_test1           | table    | beniya
 a_test2           | table    | beniya

  • a_serial Table と a_serial_s_id_seq のsequence が出来ている。
select * from a_serial_s_id_seq;

   sequence_name   | last_value | increment_by |      max_value      | min_value | cache_value | log_cnt | is_cycled | is_called
-------------------+------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
 a_serial_s_id_seq |          2 |            1 | 9223372036854775807 |         1 |           1 |      31 | f         | t
(1 row)

select \d a_serial_s_id_seq; で構造確認
Sequence "a_serial_s_id_seq"
    Column     |  Type
---------------+---------
 sequence_name | name
 last_value    | bigint
 increment_by  | bigint
 max_value     | bigint
 min_value     | bigint
 cache_value   | bigint
 log_cnt       | bigint
 is_cycled     | boolean
 is_called     | boolean







 ・・・・




目次に戻る

2005/09/29 start ********* Last Update 2006/05/30 16:28 JST

(C) Y.Kondou,2005,2006 All Rights, Reserved.
その他、お気づきの点がありましたら連絡先はこちらから