An identity column is a special column that is generated automatically from an implicit sequence. It can be used to generate key values. 識別列は暗黙的なシーケンスにより自動的に生成される特殊な列です。 これはキー値の生成に使用できます。
To create an identity column, use the <literal>GENERATED ...
AS IDENTITY</literal> clause in <command>CREATE TABLE</command>, for example:
識別列を作成するには、CREATE TABLE
のGENERATED ... AS IDENTITY
句を使用します。
例を示します。
CREATE TABLE people (
id bigint GENERATED ALWAYS AS IDENTITY,
...,
);
or alternatively あるいは
CREATE TABLE people (
id bigint GENERATED BY DEFAULT AS IDENTITY,
...,
);
See <xref linkend="sql-createtable"/> for more details. 詳細はCREATE TABLEを参照してください。
If an <command>INSERT</command> command is executed on the table with the
identity column and no value is explicitly specified for the identity
column, then a value generated by the implicit sequence is inserted. For
example, with the above definitions and assuming additional appropriate
columns, writing
INSERT
コマンドが識別列を持つテーブルで実行され、識別列に値が明示的に指定されていない場合、暗黙的なシーケンスによって生成された値が挿入されます。
例えば、上記の定義を使用し、追加の適切な列を想定して、次のように記述すると、
INSERT INTO people (name, address) VALUE ('A', 'foo'); INSERT INTO people (name, address) VALUE ('B', 'bar');
would generate values for the <literal>id</literal> column starting at 1
and result in the following table data:
1から始まるid
列の値を生成し、結果は次のテーブルデータになります。
id | name | address ----+------+--------- 1 | A | foo 2 | B | bar
Alternatively, the keyword <literal>DEFAULT</literal> can be specified in
place of a value to explicitly request the sequence-generated value, like
あるいは、値の代わりにキーワードDEFAULT
を指定して、以下のようにシーケンスによって生成された値を明示的に要求することもできます。
INSERT INTO people (id, name, address) VALUE (DEFAULT, 'C', 'baz');
Similarly, the keyword <literal>DEFAULT</literal> can be used in
<command>UPDATE</command> commands.
同様に、キーワードDEFAULT
はUPDATE
コマンドでも使用できます。
Thus, in many ways, an identity column behaves like a column with a default value. したがって、多くの点で、識別列はデフォルト値のある列のように動作します。
The clauses <literal>ALWAYS</literal> and <literal>BY DEFAULT</literal> in
the column definition determine how explicitly user-specified values are
handled in <command>INSERT</command> and <command>UPDATE</command>
commands. In an <command>INSERT</command> command, if
<literal>ALWAYS</literal> is selected, a user-specified value is only
accepted if the <command>INSERT</command> statement specifies
<literal>OVERRIDING SYSTEM VALUE</literal>. If <literal>BY
DEFAULT</literal> is selected, then the user-specified value takes
precedence. Thus, using <literal>BY DEFAULT</literal> results in a
behavior more similar to default values, where the default value can be
overridden by an explicit value, whereas <literal>ALWAYS</literal> provides
some more protection against accidentally inserting an explicit value.
識別列の定義のALWAYS
およびBY DEFAULT
句は、INSERT
およびUPDATE
コマンドでユーザが明示的に指定した値をどのように処理するかを決定します。
INSERT
コマンドでは、ALWAYS
が選択されている場合、INSERT
がOVERRIDING SYSTEM VALUE
を指定している場合にのみ、ユーザ指定の値が受け入れられます。
BY DEFAULT
を選択すると、ユーザ指定の値が優先されます。
したがって、BY DEFAULT
を使用すると、明示的な値によって上書きできるという、デフォルト値により似た動作になります。
一方、ALWAYS
では、誤って明示的な値を挿入することに対する保護が強化されます。
The data type of an identity column must be one of the data types supported by sequences. (See <xref linkend="sql-createsequence"/>.) The properties of the associated sequence may be specified when creating an identity column (see <xref linkend="sql-createtable"/>) or changed afterwards (see <xref linkend="sql-altertable"/>). 識別列のデータ型は、シーケンスでサポートされているデータ型の1つである必要があります。(CREATE SEQUENCEを参照。) 関連付けられたシーケンスの属性は、識別列の作成時に指定するか(CREATE TABLEを参照)、後で変更できます(ALTER TABLEを参照)。
An identity column is automatically marked as <literal>NOT NULL</literal>.
An identity column, however, does not guarantee uniqueness. (A sequence
normally returns unique values, but a sequence could be reset, or values
could be inserted manually into the identity column, as discussed above.)
Uniqueness would need to be enforced using a <literal>PRIMARY KEY</literal>
or <literal>UNIQUE</literal> constraint.
識別列は自動的にNOT NULL
とマークされます。
ただし、識別列は一意性を保証しません。
(通常、シーケンスは一意な値を戻しますが、シーケンスはリセットにするか、前述のように識別列に値を手動で挿入できます。)
一意性を強制するには、PRIMARY KEY
またはUNIQUE
制約を使用する必要があります。
In table inheritance hierarchies, identity columns and their properties in
a child table are independent of those in its parent tables. A child table
does not inherit identity columns or their properties automatically from
the parent. During <command>INSERT</command> or <command>UPDATE</command>,
a column is treated as an identity column if that column is an identity
column in the table named in the statement, and the corresponding identity
properties are applied.
テーブル継承階層では、子テーブルの識別列とその属性は、親テーブルの識別列とその属性から独立しています。
子テーブルは、親から自動的に識別列またはその属性を継承しません。
INSERT
またはUPDATE
のときにSQL文で書かれたテーブルの列が識別列である場合、その列は識別列として扱われ、対応する識別列の属性が適用されます。
Partitions inherit identity columns from the partitioned table. They cannot have their own identity columns. The properties of a given identity column are consistent across all the partitions in the partition hierarchy. パーティションは、パーティションテーブルから識別列を継承します。 各パーティションが独自の識別列を持つことはできません。 識別列の属性はパーティション階層内のすべてのパーティションで一貫しています。