CREATE SEQUENCE

Name

CREATE SEQUENCE  --  Creates a new sequence number generator 新しいシーケンス番号生成機構を作成する

Synopsis

CREATE SEQUENCE seqname
    [ INCREMENT increment ]
    [ MINVALUE  minvalue ]
    [ MAXVALUE  maxvalue ]
    [ START     start ]
    [ CACHE     cache ]
    [ CYCLE ]
  

入力

seqname

The name of a sequence to be created.

作成されるシーケンスの名前です。

increment

The INCREMENT increment clause is optional. A positive value will make an ascending sequence, a negative one a descending sequence. The default value is one (1).

INCREMENT increment 句はオプションで、増加量を設定します。 正の値は昇順のシーケンス番号、負の値は降順のシーケンス番号を 作成します。これを設定しない場合のデフォルト値は 1 です。

minvalue

The optional clause MINVALUE minvalue determines the minimum value a sequence can generate. The defaults are 1 and -2147483647 for ascending and descending sequences, respectively.

MINVALUE minvalue 句もオプションで、シーケンス番号と して作成できる最小の値を決定します。 昇順と降順のシーケンスのデフォルト値は、それぞれ、 1 と -2147483647 です。

maxvalue

Use the optional clause MAXVALUE maxvalue to determine the maximum value for the sequence. The defaults are 2147483647 and -1 for ascending and descending sequences, respectively.

MAXVALUE maxvalue 句もオプションです。、シーケンス番号の最大値を 決定します。昇順と降順のそれぞれのシーケンス番号の最大値の デフォルトは、2147483647 と -1 です。

start

The optional START start clause enables the sequence to begin anywhere. The default starting value is minvalue for ascending sequences and maxvalue for descending ones.

オプションの START start clause で、シーケンスをどこからでも始めることが できます。 これを設定しない場合、シーケンス番号が始まる値は、 昇順の場合 minvalue の値 になり、降順の場合 maxvalue の値 になります。

cache

The CACHE cache option enables sequence numbers to be preallocated and stored in memory for faster access. The minimum value is 1 (only one value can be generated at a time, i.e. no cache) and this is also the default.

CACHE cache オプションは、あらかじめシーケンス番号を準備しておき、 それをメモリに格納しておくことでアクセスを速くします。 最小値は 1 (これは 1 回に付き 1 つの値だけを生成します、 つまりキャッシュは有りません) で、これがデフォルトにも なっています。

CYCLE

The optional CYCLE keyword may be used to enable the sequence to continue when the maxvalue or minvalue has been reached by an ascending or descending sequence respectively. If the limit is reached, the next number generated will be whatever the minvalue or maxvalue is, as appropriate.

オプションの CYCLE キーワードは、昇順、あるいは降順の シーケンス番号が、それぞれ、 maxvalue あるいは minvalue の値に達した時に、そのシーケンス番号を継続することができる ようにするために使われます。 もしシーケンス番号の限界値 まで行った場合、次に生成される番号は minvalue あるいは maxvalue のどちらか適切な値になります。

出力

CREATE

Message returned if the command is successful.

コマンドが成功した場合に返されるメッセージです。

ERROR: Relation 'seqname' already exists

If the sequence specified already exists.

指定したシーケンスが既に存在している場合のメッセージです。

ERROR: DefineSequence: MINVALUE (start) can't be >= MAXVALUE (max)

If the specified starting value is out of range.

範囲外の初期値が設定されました。 (最大値以上の値が設定されました。)

ERROR: DefineSequence: START value (start) can't be < MINVALUE (min)

If the specified starting value is out of range.

範囲外の初期値が設定されました。 (最小値より小さい値が設定されました。)

ERROR: DefineSequence: MINVALUE (min) can't be >= MAXVALUE (max)

If the minimum and maximum values are inconsistant.

最小値、最大値の設定が矛盾しています。 (最小値が、最大値より大きい値に設定されました。)

説明

CREATE SEQUENCE will enter a new sequence number generator into the current data base. This involves creating and initialising a new single-row table with the name seqname. The generator will be "owned" by the user issuing the command.

CREATE SEQUENCE は、新しいシーケンス番号 生成機構を現在のデータベースの中に登録します。 これは、 seqname という 名前を持った一列だけの新しいテーブルの作成と初期化を行います。 シーケンス番号生成機構は、CREATE SEQUENCE コマンドを実行したユーザによって "所有" されます。

After a sequence is created, you may use the function nextval(seqname) to get a new number from the sequence. The function currval('seqname') may be used to determine the number returned by the last call to nextval(seqname) for the specified sequence in the current session. The function setval('seqname', newvalue) may be used to set the current value of the specified sequence. The next call to nextval(seqname) will return the given value plus the sequence increment.

シーケンスが作られた後、シーケンスから新しい値を取得する ために、 nextval(seqname) という関数を使うことができます。 currval('seqname') という関数は、現在の接続の中で指定のシーケンスが最後に nextval(seqname) が呼び出された時の返り値を知るために使われます。 setval('seqname', newvalue) という関数は、指定のシーケンスの現在値を設定し直すために 使います。 この関数で設定を行った後、次に nextval(seqname) が呼び出された場合、設定した値に増加量を加えた値を返します。

Use a query like

シーケンステーブルに対する問い合わせは、以下のようにします。

SELECT * FROM sequence_name;
   
to get the parameters of a sequence. Aside from fetching the original parameters, you can use 上記コマンドで、シーケンスの全てのパラメータを得ることが できます。 パラメータの中からバックエンドに割り当て済みの値を取得 するには、以下のようにします。
SELECT last_value FROM sequence_name;
   
to obtain the last value allocated by any backend. parameters, you can use 使うことができるパラメータは、 # このあと、原文が抜けているような...。

Low-level locking is used to enable multiple simultaneous calls to a generator.

生成機構への多数の同時呼び出しを可能にするために Low-level locking (低レベルロック) が使われます。

Caution

Unexpected results may be obtained if a cache setting greater than one is used for a sequence object that will be used concurrently by multiple backends. Each backend will allocate "cache" successive sequence values during one access to the sequence object and increase the sequence object's last_value accordingly. Then, the next cache-1 uses of nextval within that backend simply return the preallocated values without touching the shared object. So, numbers allocated but not used in the current session will be lost. Furthermore, although multiple backends are guaranteed to allocate distinct sequence values, the values may be generated out of sequence when all the backends are considered. (For example, with a cache setting of 10, backend A might reserve values 1..10 and return nextval=1, then backend B might reserve values 11..20 and return nextval=11 before backend A has generated nextval=2.) Thus, with a cache setting of one it is safe to assume that nextval values are generated sequentially; with a cache setting greater than one you should only assume that the nextval values are all distinct, not that they are generated purely sequentially. Also, last_value will reflect the latest value reserved by any backend, whether or not it has yet been returned by nextval.

もし、複数のバックエンドによって同時に使用される可能性の 有るシーケンスオブジェクトに対しキャッシュ設定が 1 よりも 大きい場合、予想外の結果となることがあります。 それぞれのバックエンドは"キャッシュ"の連続したシーケンスの 値を 1 回のアクセスの間でシーケンスオブジェクトに与え、 それに応じて、シーケンスオブジェクトの last_value を 増やします。 次に、引き続く cache-1 は バックエンド内での nextval を 使用して、共有オブジェクトに触ることなく、事前に割り当てら れた値を返します。 従って、割り当てられても、指定されたセッションで 使われなかった(シーケンス)番号は破棄されます。 さらに、複数のバックエンドは別個のシーケンス番号を付加 して良いことになっていますが、その値は全てのバックエンド を対象にした場合、順列に付いては定かでありません。 (例えば、キャッシュ設定が 10 となっていて、 バックエンド A が 1...10 までの値を予約していて、 それに従って、バックエンド B が 11...20 の値を予約してバックエンド A が nextval=2 を生成する 前に nextval=11 を返すことがあります。) 従って、キャッシュ設定が 1 の場合、nextval の値は連続的に生成 されると想定することは安全ですが、キャッシュ設定が 1 より 大きい時は、nextval の値はすべて重複しない値になりますが、 それらは純粋に連続的に生成されると言うわけではありません。 同様に、last_value は、nextval によってどんな値が返されるか ということに関わらず、バックエンドによって一番最近に予約 された値を反映しています。 このようにして、ある一つの対象にキャッシュを設定する ことは、nextval が順序だって生成されることが重要です

注意

Refer to the DROP SEQUENCE statement to remove a sequence.

シーケンスを削除する方法は、 DROP SEQUENCE 文を参照してください。

Each backend uses its own cache to store allocated numbers. Numbers that are cached but not used in the current session will be lost, resulting in "holes" in the sequence.

各バックエンドは割り当てられた番号を保存するために自分用の キャッシュを使用します。キャッシュされただけで、現在の セッションで使われなかった(シーケンス)番号は失われ、 連続する番号の中に "抜け" ができます。

使用法

Create an ascending sequence called serial, starting at 101:

serial という名前で、開始番号が 101 の 昇順シーケンスを作るには、以下のようにします:

CREATE SEQUENCE serial START 101;
  

Select the next number from this sequence

上記シーケンスの次の番号を選択する方法です:

SELECT NEXTVAL ('serial');
    
nextval
-------
    114
   

Use this sequence in an INSERT:

INSERT 文の中で、このシーケンスを使う方法です:

INSERT INTO distributors VALUES (NEXTVAL('serial'),'nothing');
   

Set the sequence value after a COPY FROM:

"COPY FROM" を実行後、シーケンス値を設定するには 以下のようにします:

CREATE FUNCTION distributors_id_max() RETURNS INT4
    AS 'SELECT max(id) FROM distributors' 
    LANGUAGE 'sql';
BEGIN;
    COPY distributors FROM 'input_file';
    SELECT setval('serial', distributors_id_max());
END;
   

互換性

CREATE SEQUENCE is a Postgres language extension.

CREATE SEQUENCE は、 Postgres の拡張言語です。

SQL92

There is no CREATE SEQUENCE statement in SQL92.

SQL92CREATE SEQUENCE 文は有りません。