CREATE TABLE

Name

CREATE TABLE  --  Creates a new table 新しいテーブルを作成します。

Synopsis

CREATE [ TEMPORARY | TEMP ] TABLE table (
    column type
    [ NULL | NOT NULL ] [ UNIQUE ] [ DEFAULT value ]
    [column_constraint_clause | PRIMARY KEY } [ ... ] ]
    [, ... ]
    [, PRIMARY KEY ( column [, ...] ) ]
    [, CHECK ( condition ) ]
    [, table_constraint_clause ]
    ) [ INHERITS ( inherited_table [, ...] ) ]
  

入力

TEMPORARY

The table is created only for this session, and is automatically dropped on session exit. Existing permanent tables with the same name are not visible while the temporary table exists.

このセッションに対してのみテーブルが作成され、セッション 終了と同時に自動的に削除されます。 同じ名前で存在している恒久的なテーブルは暫定的テーブルが 存在している間は非可視です。

table

The name of a new table to be created.

作成される新しいテーブルの名前です。

column

The name of a column.

カラムの名前です。

type

The type of the column. This may include array specifiers. Refer to the PostgreSQL User's Guide for further information about data types and arrays.

カラムの型です。配列の指定を含むこともあります。 データ型と配列についてより詳しくは PostgreSQL ユーザガイド を 参照して下さい。

DEFAULT value

A default value for a column. See the DEFAULT clause for more information.

カラムのデフォルト値です。 より詳しくは DEFAULT 句を参照して下さい。

column_constraint_clause

The optional column constraint clauses specify a list of integrity constraints or tests which new or updated entries must satisfy for an insert or update operation to succeed. Each constraint must evaluate to a boolean expression. Although SQL92 requires the column_constraint_clause to refer to that column only, Postgres allows multiple columns to be referenced within a single column constraint. See the column constraint clause for more information.

オプションのカラム制約条件句は、新規または更新された項目が、 挿入または更新の操作がうまく行なわれるために満足しなければ ならない、完全性保証制約条件、あるい一連の評価の明細を指定 します。それぞれの制約条件はブール式を評価しなければなりません。 SQL92 ではそのカラムだけを参照する column_constraint_clause を必要としますが、Postgres では一つの カラム制約条件で複数のカラムを参照して構いません。 詳しい情報はカラム CONSTRAINT 句を参照して下さい。

table_constraint_clause

The optional table CONSTRAINT clause specifies a list of integrity constraints which new or updated entries must satisfy for an insert or update operation to succeed. Each constraint must evaluate to a boolean expression. Multiple columns may be referenced within a single constraint. Only one PRIMARY KEY clause may be specified for a table; PRIMARY KEY column (a table constraint) and PRIMARY KEY (a column constraint) are mutually exclusive.. See the table constraint clause for more information.

オプションのテーブル CONSTRAINT 句は、新規または更新された項目が、 挿入または更新の操作がうまく行なわれるために満足しなければ ならない、完全性制約条件の明細を指定します。 それぞれの制約条件はブール式を評価しなければなりません。 一つの制約条件の中で複数のカラムが参照されても構いません。 テーブルに対して、PRIMARY KEY 句は一つだけ指定することが 出来ます。 PRIMARY KEY column (テーブルの制約) と PRIMARY KEY (カラムの制約)は基本的に排他的です。 より詳しくは、テーブル CONSTRAINT 句を参照して下さい。

INHERITS inherited_table

The optional INHERITS clause specifies a collection of table names from which this table automatically inherits all fields. If any inherited field name appears more than once, Postgres reports an error. Postgres automatically allows the created table to inherit functions on tables above it in the inheritance hierarchy.

オプションの INHERITS 句は、全てのフィールドをそのテーブルから 自動的に継承するテーブル名の集合を指定します。 継承されたフィールド名が1つ以上あるときには、 Postgres はエラーを出します。 Postgres は、継承の階層の 中でその上位にあるテーブル上の継承関数を作成されたテーブルが 継承することを無条件で認めています。

余談: Inheritance of functions is done according to the conventions of the Common Lisp Object System (CLOS).

関数の継承は Common Lisp Object System (CLOS) の 取り決めに従って行われます。

出力

CREATE

Message returned if table is successfully created.

テーブルが正常に作成されたときに戻るメッセージです。

ERROR

Message returned if table creation failed. This is usually accompanied by some descriptive text, such as:

テーブルの作成に失敗したときに戻るメッセージです。 通常以下のような説明文が付いて来ます: ERROR: Relation 'table' already exists which occurs at runtime, if the table specified already exists in the database. データベースに既に指定したテーブルがある場合で、 実行時に発生します。

ERROR: DEFAULT: type mismatched

If data type of default value doesn't match the column definition's data type.

カラム定義のデータ型とデフォルト値のデータ型が 一致しない場合です。

説明

CREATE TABLE will enter a new table into the current data base. The table will be "owned" by the user issuing the command.

CREATE TABLE は指定したデータベースの 中に新しいテーブルを挿入します。 テーブルはこのコマンドを発行したユーザの"所有"となります。

The new table is created as a heap with no initial data. A table can have no more than 1600 columns (realistically, this is limited by the fact that tuple sizes must be less than 8192 bytes), but this limit may be configured lower at some sites. A table cannot have the same name as a system catalog table.

新しいテーブルは初期データのない堆積(ヒープ)として作成 されます。テーブルには 1600 以上のカラムを作れません。 (実際は、タプルの大きさが 8192 バイト以下に制限されて いるという事実によります。)しかしサイトによってはより 少なく設定されている所もあるでしょう。テーブルはシステム カタログと同じ名前を持つことは出来ません。

DEFAULT 句

DEFAULT value
   

入力

value

The possible values for the default value expression are:

デフォルト値の式に対して、有効な値は以下のものです。

  • リテラル値

  • ユーザ関数

  • ニラディック(無引数の)関数

出力

None.

ありません。

説明

The DEFAULT clause assigns a default data value to a column (via a column definition in the CREATE TABLE statement). The data type of a default value must match the column definition's data type.

DEFAULT 句は (CREATE TABLE 文のカラム定義経由)カラムに デフォルトのデータ値を割り当てます。 デフォルト値のデータ型はカラム定義のデータ型に一致しなければ なりません。

An INSERT operation that includes a column without a specified default value will assign the NULL value to the column if no explicit data value is provided for it. Default literal means that the default is the specified constant value. Default niladic-function or user-function means that the default is the value of the specified function at the time of the INSERT.

指定したデフォルト値の無いカラムのあるテーブルに対する INSERT 操作は、明示的にそのデータ値が与えられていない限り そのカラムに NULL 値を割り当てます。 デフォルトの literal は、デフォルトが指定された定数値であることを意味します。 デフォルトの niladic-function または user-function は デフォルトが INSERT 時における指定された関数の値であることを 意味します。

There are two types of niladic functions:

ニラディック関数には二つの型があります。

ニラディック USER

CURRENT_USER / USER

See CURRENT_USER function

CURRENT_USER 関数を参照して下さい。

SESSION_USER

not yet supported

まだサポートされていません。

SYSTEM_USER

not yet supported

まだサポートされていません。

ニラディック datetime

CURRENT_DATE

See CURRENT_DATE function

CURRENT_DATE 関数を参照して下さい。

CURRENT_TIME

See CURRENT_TIME function

CURRENT_TIME 関数を参照して下さい。

CURRENT_TIMESTAMP

See CURRENT_TIMESTAMP function

CURRENT_TIMESTAMP 関数を参照して下さい。

In the current release (v6.5), Postgres evaluates all default expressions at the time the table is defined. Hence, functions which are "non-cacheable" such as CURRENT_TIMESTAMP may not produce the desired effect. For the particular case of date/time types, one can work around this behavior by using "DEFAULT TEXT 'now'" instead of "DEFAULT 'now'" or "DEFAULT CURRENT_TIMESTAMP". This forces Postgres to consider the constant a string type and then to convert the value to timestamp at runtime.

現在のリリース (v6.5) の Postgres は、 テーブル定義の時に全てのデフォルト式を評価します。 ですから、 CURRENT_TIMESTAMP のような"キャッシュ できない"関数は期待するような効果をもたらさないことがあります。 date/time 型の特別な場合についての、この振舞を以下のようにして確かめ ることが出来ます。 "DEFAULT 'now'" または "DEFAULT CURRENT_TIMESTAMP" の代わりとして、 "DEFAULT TEXT 'now'" を使います。 これにより、Postgres は実行時に 定数を文字型と判断し、その値を timestamp に変換 するようになります。

使用法

To assign a constant value as the default for the columns did and number, and a string literal to the column did:

定数値をカラム didnumber のデフォルトとして割り当て、リテラル文字列をカラム did に割り当てるには以下のようにします:

CREATE TABLE video_sales (
    did      VARCHAR(40) DEFAULT 'luso films',
    number   INTEGER DEFAULT 0,
    total    CASH DEFAULT '$0.0'
);
    

To assign an existing sequence as the default for the column did, and a literal to the column name:

カラム did に既存のシーケンスを デフォルトとして割り当て、カラム name に リテラルを割り当てるには以下のようにします:

CREATE TABLE distributors (
    did      DECIMAL(3)  DEFAULT NEXTVAL('serial'),
    name     VARCHAR(40) DEFAULT 'luso films'
);
    

カラム CONSTRAINT 句

[ CONSTRAINT name ] { [
    NULL | NOT NULL ] | UNIQUE | PRIMARY KEY | CHECK constraint } [, ...]
   

入力

name

An arbitrary name given to the integrity constraint. If name is not specified, it is generated from the table and column names, which should ensure uniqueness for name.

完全性保証制約条件に与えられる任意の名前です。 name が 指定されていない場合、それはテーブル名とカラム名から 生成され、その name の一意性は保証されます。

NULL

The column is allowed to contain NULL values. This is the default.

カラムには NULL 値があっても構いません。そしてこれはデフォルトです。

NOT NULL

The column is not allowed to contain NULL values. This is equivalent to the column constraint CHECK (column NOT NULL).

カラムに NULL 値があってはいけません。 これはカラム制約 CHECK (column NOT NULL) と等価です。

UNIQUE

The column must have unique values. In Postgres this is enforced by an implicit creation of a unique index on the table.

カラムに一意の値がなければなりません。 Postgres では、テーブル上に UNIQUE インデックスを明示的に作成することで、適用されます。

PRIMARY KEY

This column is a primary key, which implies that uniqueness is enforced by the system and that other tables may rely on this column as a unique identifier for rows. See PRIMARY KEY for more information.

このカラムは主キーで、システムにより一意性が適用されて、 他のテーブルが、このカラムを行に対する一意な識別子として参照 することが出来るようになります。 より詳しくは PRIMARY KEY を参照して下さい。

constraint

The definition of the constraint.

制約条件の定義です。

説明

A Constraint is a named rule: an SQL object which helps define valid sets of values by putting limits on the results of INSERT, UPDATE or DELETE operations performed on a Base Table.

制約条件とは、名前の付いているルールで、 基本テーブル上で実行された INSERT、UPDATE または DELETE 操作 の結果に制限を加えることによって有効な値の一群の定義を手助け する SQL オブジェクトです。

There are two ways to define integrity constraints: table constraints, covered later, and column constraints, covered here.

完全性保証制約条件を定義する方法は、後で述べるテーブル制約条件と、 ここで述べるカラム制約条件の二つがあります。

A column constraint is an integrity constraint defined as part of a column definition, and logically becomes a table constraint as soon as it is created. The column constraints available are:

カラム制約条件はカラム定義の一部として定義される、 データの完全性を保証する制約条件で、理論上、作成された時点で テーブル制約条件となります。カラム制約条件として有効なものは 以下のものです:

PRIMARY KEY
REFERENCES
UNIQUE
CHECK
NOT NULL

Note: Postgres does not yet (at release 6.5) support REFERENCES integrity constraints. The parser accepts the REFERENCES syntax but ignores the clause.

Postgres は(リリース 6.5) の 時点でまだ REFERENCES の完全性を保証する制約条件をサポート していません。パーサは REFERENCES の構文を受け付けるとは いっても、その句を無視します。

NOT NULL 制約条件

[ CONSTRAINT name ] NOT NULL 
   

The NOT NULL constraint specifies a rule that a column may contain only non-null values. This is a column constraint only, and not allowed as a table constraint.

NOT NULL 制約条件は、カラムが 非 NULL 値のみというルールを 指定します。これは、カラム制約条件のみに対するものであって、 テーブル制約条件としては認められていません。

出力

status

ERROR: ExecAppend: Fail to add null value in not null attribute "column".

This error occurs at runtime if one tries to insert a null value into a column which has a NOT NULL constraint.

実行時、NOT NULL 制約事項のあるカラムに NULL 値を挿入しよう とした時に起こるエラーです。

説明

使用法

Define two NOT NULL column constraints on the table distributors, one of which being a named constraint:

一つが制約条件対象とされているテーブル distributors 上に 二つの NOT NULL カラム制約条件を設定します:

CREATE TABLE distributors (
    did      DECIMAL(3) CONSTRAINT no_null NOT NULL,
    name     VARCHAR(40) NOT NULL
);
     

UNIQUE 制約条件

[ CONSTRAINT name ] UNIQUE
   

入力

CONSTRAINT name

An arbitrary label given to a constraint.

制約条件に与えられる任意のラベルです。

出力

status

ERROR: Cannot insert a duplicate key into a unique index.

This error occurs at runtime if one tries to insert a duplicate value into a column.

一つのカラムに重複した値を挿入しようとした時の 実行時に出力されるエラーです。

説明

The UNIQUE constraint specifies a rule that a group of one or more distinct columns of a table may contain only unique values.

UNIQUE 制約条件は、テーブルの一つ以上の独立したそれぞれの カラムのグループが一意の値しか持つことが出来ないてないとい うルールを指定します。

The column definitions of the specified columns do not have to include a NOT NULL constraint to be included in a UNIQUE constraint. Having more than one null value in a column without a NOT NULL constraint, does not violate a UNIQUE constraint. (This deviates from the SQL92 definition, but is a more sensible convention. See the section on compatibility for more details.).

指定されたカラムのカラム定義は、UNIQUE 制約条件に含まれる NOT NULL 制約条件を持ってはなりません。NOT NULL 制約条件 のないカラムに一つ以上の NULL 値があっても UNIQUE 制約条件 に違反しません。(このことは SQL92 定義 から逸脱しますが、より意味のある取り決めです。より詳細は 互換性の節を参照して下さい。

Each UNIQUE column constraint must name a column that is different from the set of columns named by any other UNIQUE or PRIMARY KEY constraint defined for the table.

それぞれの UNIQUE カラム制約条件は、そのテーブルに対して 指名された他のいかなる UNIQUE または PRIMARY KEY 制約条件 によって指名されたカラムの一群と異なるカラムを指名しな ければなりません。

Note: Postgres automatically creates a unique index for each UNIQUE constraint, to assure data integrity. See CREATE INDEX for more information.

データの完全性を保証するため、Postgres はそれぞれの UNIQUE 制約条件に対して自動的に一意の インデックスを作成します。 より詳しくは、CREATE INDEX を参照して下さい。

使用法

Defines a UNIQUE column constraint for the table distributors. UNIQUE column constraints can only be defined on one column of the table:

テーブル distributors に対し UNIQUE カラム制約条件を定義します。 UNIQUE カラム制約条件はテーブルの一つのカラムに対してのみ 定義可能です:

CREATE TABLE distributors (
    did      DECIMAL(3),
    name     VARCHAR(40) UNIQUE
);
  
- which is equivalent to the following specified as a table constraint: 上記は、以下のテーブル制約条件を指定したことと等価です。
CREATE TABLE distributors (
    did      DECIMAL(3),
    name     VARCHAR(40),
    UNIQUE(name)
);
     

CHECK 制約条件

[ CONSTRAINT name ] CHECK
    ( condition [, ...] ) 
   

入力

name

An arbitrary name given to a constraint.

制約条件に与えられる任意の名前です。

condition

Any valid conditional expression evaluating to a boolean result.

ブール値の結果を評価する有効な任意の条件式です。

出力

status

ERROR: ExecAppend: rejected due to CHECK constraint "table_column".

This error occurs at runtime if one tries to insert an illegal value into a column subject to a CHECK constraint.

実行時に違法の値が、CHECK 制約条件となるカラムに 挿入されようとしたときに出力されるエラーです。

説明

The CHECK constraint specifies a restriction on allowed values within a column. The CHECK constraint is also allowed as a table constraint.

CHECK 制約条件はカラム内で有効な値に対しての制約を指定します。 CHECK 制約条件はテーブル制約条件としても有効です。

The SQL92 CHECK column constraints can only be defined on, and refer to, one column of the table. Postgres does not have this restriction.

SQL92 の CHECK カラム制約条件はテーブルの一つのカラム に対してのみ定義され、そのカラムを参照します。 Postgres にはこの制約がありません。

PRIMARY KEY 制約条件

[ CONSTRAINT name ] PRIMARY KEY 
   

入力

CONSTRAINT name

An arbitrary name for the constraint.

その制約条件に対する任意の名前です。

Outputs

ERROR: Cannot insert a duplicate key into a unique index.

This occurs at run-time if one tries to insert a duplicate value into a column subject to a PRIMARY KEY constraint.

実行時に重複した値が、 PRIMARY KEY 制約条件となるカラムに 挿入されようとしたときに出力されるエラーです。

説明

The PRIMARY KEY column constraint specifies that a column of a table may contain only unique (non-duplicate), non-NULL values. The definition of the specified column does not have to include an explicit NOT NULL constraint to be included in a PRIMARY KEY constraint.

PRIMARY KEY カラム制約条件は、テーブルのカラムには 一意の(重複しない)、NULL でない値のみであることを指定します。 指定されたカラムの定義では、PRIMARY KEY 制約条件に含まれる 暗黙的な NOT NULL 制約条件を持つ必要はありません。

Only one PRIMARY KEY can be specified for a table.

テーブルに対し一つの PRIMARY KEY のみが指定できます。

注意事項

Postgres automatically creates a unique index to assure data integrity. (See CREATE INDEX statement)

データの完全性を保証するため、Postgres は自動的に一意のインデックスを作成します。 (CREATE INDEX 文を参照して下さい。)

The PRIMARY KEY constraint should name a set of columns that is different from other sets of columns named by any UNIQUE constraint defined for the same table, since it will result in duplication of equivalent indexes and unproductive additional runtime overhead. However, Postgres does not specifically disallow this.

PRIMARY KEY では、同じテーブルに定義された UNIQUE 制約条件に よって指定されたカラムの他の一群と異なったカラムの一群を指定 しなければなりません。というのは、同じインデックスを複製したり、 余分な、役に立たない実行時における作業のオーバヘッドを排除 するためです。 とは言っても、Postgres は特別に このことを認めていないわけではありません。

テーブル CONSTRAINT 句

[ CONSTRAINT name ] { PRIMARY KEY |  UNIQUE } ( column [, ...] )
[ CONSTRAINT name ] CHECK ( constraint )
   

入力

CONSTRAINT name

An arbitrary name given to an integrity constraint.

完全性保証制約条件に与えられる任意の名前です。

column [, ...]

The column name(s) for which to define a unique index and, for PRIMARY KEY, a NOT NULL constraint.

一意のインデックス、 PRIMARY KEY、NOT NULL 制約条件 を定義するカラム(複数可)の名前です。

CHECK ( constraint )

A boolean expression to be evaluated as the constraint.

制約条件として評価されるブール式です。

出力

The possible outputs for the table constraint clause are the same as for the corresponding portions of the column constraint clause.

テーブル制約条件句に対して、可能性のある出力は、カラム制約条件での 対応する部分によって出力と同一です。

説明

A table constraint is an integrity constraint defined on one or more columns of a base table. The four variations of "Table Constraint" are:

テーブル制約条件は、基本テーブルの一つ以上の カラムに定義されたデータの完全性を保証する制約条件です。 "テーブル制約条件"には次の四つがあります。

UNIQUE
CHECK
PRIMARY KEY
FOREIGN KEY

Note: Postgres does not yet (as of version 6.5) support FOREIGN KEY integrity constraints. The parser understands the FOREIGN KEY syntax, but only prints a notice and otherwise ignores the clause. Foreign keys may be partially emulated by triggers (See the CREATE TRIGGER statement).

Postgres は (6.5 の段階で)まだ FOREIGN KEY 完全性保証制約条件をサポートしていません。 パーサは FOREIGN KEY の構文を理解はしますが、警告を表示 するかその句を無視するかいずれかです。 外部キーはトリガによって一部エミュレートされることが あります。(CREATE TRIGGER 文を参照して下さい。)

UNIQUE 制約条件

[ CONSTRAINT name ] UNIQUE ( column [, ...] )
    

入力

CONSTRAINT name

An arbitrary name given to a constraint.

制約条件に与えられる任意の名前です。

column

A name of a column in a table.

テーブル内のカラムの名前です。

出力

status

ERROR: Cannot insert a duplicate key into a unique index

This error occurs at runtime if one tries to insert a duplicate value into a column.

実行時に、ひとつのカラムに重複した値を挿入しようと した場合に出力されるエラーです。

説明

The UNIQUE constraint specifies a rule that a group of one or more distinct columns of a table may contain only unique values. The behavior of the UNIQUE table constraint is the same as that for column constraints, with the additional capability to span multiple columns.

UNIQUE 制約条件は、テーブルの一つ以上の独立したそれぞれの カラムのグループが一意の値しか持つことが出来ないというルール を指定します。

See the section on the UNIQUE column constraint for more details.

より詳しくは UNIQUE カラム制約条件の節を参照して下さい。

使用法

Define a UNIQUE table constraint for the table distributors:

テーブル distributors に UNIQUE テーブル制約条件を定義します:

CREATE TABLE distributors (
    did      DECIMAL(03),
    name     VARCHAR(40),
    UNIQUE(name)
);
     

PRIMARY KEY 制約条件

[ CONSTRAINT name ] PRIMARY KEY ( column [, ...] ) 
    

入力

CONSTRAINT name

An arbitrary name for the constraint.

この制約条件の任意の名前です。

column [, ...]

The names of one or more columns in the table.

テーブル内の一つまたはそれ以上のカラムの名前です。

出力

status

ERROR: Cannot insert a duplicate key into a unique index.

This occurs at run-time if one tries to insert a duplicate value into a column subject to a PRIMARY KEY constraint.

実行時に重複した値が、 PRIMARY KEY 制約条件となるカラムに 挿入されようとしたときに出力されるエラーです。

説明

The PRIMARY KEY constraint specifies a rule that a group of one or more distinct columns of a table may contain only unique, (non duplicate), non-null values. The column definitions of the specified columns do not have to include a NOT NULL constraint to be included in a PRIMARY KEY constraint.

PRIMARY KEY 制約条件は、テーブルの一つ以上の独立したそれぞれ のカラムのグループが一意で、(重複しないで)、なお NULL でない値のみが許されるというルールを指定します。 指定されたカラムのカラム定義では、PRIMARY KEY 制約条件に含まれる NOT NULL 制約条件を含んではいけません。

The PRIMARY KEY table constraint is similar to that for column constraints, with the additional capability of encompassing multiple columns.

PRIMARY KEY テーブル制約条件は、カラム制約条の件の PRIMARY KEY 制約条件と類似したものですが、複数のカラムに拡張した追加機能が あります。

Refer to the section on the PRIMARY KEY column constraint for more information.

より詳しくは、PRIMARY KEY カラム制約条件の節を参照して下さい。

使用法

Create table films and table distributors:

テーブル films とテーブル distributors を 作成します:

CREATE TABLE films (
     code      CHARACTER(5) CONSTRAINT firstkey PRIMARY KEY,
     title     CHARACTER VARYING(40) NOT NULL,
     did       DECIMAL(3) NOT NULL,
     date_prod DATE,
     kind      CHAR(10),
     len       INTERVAL HOUR TO MINUTE
);
   
CREATE TABLE distributors (
     did      DECIMAL(03) PRIMARY KEY DEFAULT NEXTVAL('serial'),
     name     VARCHAR(40) NOT NULL CHECK (name <> '')
);
   

Create a table with a 2-dimensional array:

二次元配列のテーブルを作成します:

   CREATE TABLE array (
          vector INT[][]
          );
   

Define a UNIQUE table constraint for the table films. UNIQUE table constraints can be defined on one or more columns of the table:

テーブル films に UNIQUE テーブル制約条件を定義します。 UNIQUE テーブル制約条件はテーブルの一つ以上のカラムに 定義することが出来ます。

CREATE TABLE films (
    code      CHAR(5),
    title     VARCHAR(40),
    did       DECIMAL(03),
    date_prod DATE,
    kind      CHAR(10),
    len       INTERVAL HOUR TO MINUTE,
    CONSTRAINT production UNIQUE(date_prod)
);
   

Define a CHECK column constraint:

CHECK カラム制約条件を定義します:

CREATE TABLE distributors (
    did      DECIMAL(3) CHECK (did > 100),
    name     VARCHAR(40)
);
   

Define a CHECK table constraint:

CHECK テーブル制約条件を定義します:

CREATE TABLE distributors (
    did      DECIMAL(3),
    name     VARCHAR(40)
    CONSTRAINT con1 CHECK (did > 100 AND name > '')
);
   

Define a PRIMARY KEY table constraint for the table films. PRIMARY KEY table constraints can be defined on one or more columns of the table:

テーブル films に PRIMARY KEY テーブル制約条件を定義します。 PRIMARY KEY テーブル制約条件はテーブルの一つ以上のカラムに 定義することが出来ます。

CREATE TABLE films (
    code      CHAR(05),
    title     VARCHAR(40),
    did       DECIMAL(03),
    date_prod DATE,
    kind      CHAR(10),
    len       INTERVAL HOUR TO MINUTE,
    CONSTRAINT code_title PRIMARY KEY(code,title)
);
   

Defines a PRIMARY KEY column constraint for table distributors. PRIMARY KEY column constraints can only be defined on one column of the table (the following two examples are equivalent):

テーブル distributors の PRIMARY KEY カラム制約条件を定義 します。PRIMARY KEY カラム制約条件はテーブルの一つのカラム に対してのみ定義できます。(以下の二つの例は等価です。)

CREATE TABLE distributors (
    did      DECIMAL(03),
    name     CHAR VARYING(40),
    PRIMARY KEY(did)
); 
   
CREATE TABLE distributors (
    did      DECIMAL(03) PRIMARY KEY,
    name     VARCHAR(40)
);
   

注意事項

CREATE TABLE/INHERITS is a Postgres language extension.

CREATE TABLE/INHERITS は Postgres の拡張言語です。

互換性

SQL92

In addition to the locally-visible temporary table, SQL92 also defines a CREATE GLOBAL TEMPORARY TABLE statement, and optionally an ON COMMIT clause:

ローカルに可視的な暫定テーブルに加え、SQL92 では CREATE GLOBAL TEMPORARY TABLE 文と、オプションでの ON COMMIT 句も同時に定義しています。

CREATE GLOBAL TEMPORARY TABLE table ( column type [
    DEFAULT value ] [ CONSTRAINT column_constraint ] [, ...] )
    [ CONSTRAINT table_constraint ] [ ON COMMIT { DELETE | PRESERVE } ROWS ] 
   

For temporary tables, the CREATE GLOBAL TEMPORARY TABLE statement names a new table visible to other clients and defines the table's columns and constraints.

暫定テーブルに対し、CREATE GLOBAL TEMPORARY TABLE 文は ほかのクライアントに可視的な新しいテーブルを指名し、テーブルの カラムと制約条件を定義します。

The optional ON COMMIT clause of CREATE TEMPORARY TABLE specifies whether or not the temporary table should be emptied of rows whenever COMMIT is executed. If the ON COMMIT clause is omitted, the default option, ON COMMIT DELETE ROWS, is assumed.

CREATE TEMPORARY TABLE のオプションの ON COMMIT 句は、 COMMIT が実行された時、暫定テーブルの行を空にするかどう かを指定します。 ON COMMIT 句が省略された場合は、デフォルトのオプションである ON COMMIT DELETE ROWS が候補となります。

To create a temporary table:

暫定テーブルを作ります:

CREATE TEMPORARY TABLE actors (
    id         DECIMAL(03),
    name       VARCHAR(40),
    CONSTRAINT actor_id CHECK (id < 150)
) ON COMMIT DELETE ROWS;
    

UNIQUE 句

SQL92 specifies some additional capabilities for UNIQUE:

SQL92 は UNIQUE にいくつかの追加的機能を明記しています。

Table Constraint definition:

テーブル制約条件の定義:

[ CONSTRAINT name ] UNIQUE ( column [, ...] )
    [ { INITIALLY DEFERRED | INITIALLY IMMEDIATE } ]
    [ [ NOT ] DEFERRABLE ]
     

Column Constraint definition:

カラム制約条件の定義:

[ CONSTRAINT name ] UNIQUE
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
     

NULL 句

The NULL "constraint" (actually a non-constraint) is a Postgres extension to SQL92 is included for symmetry with the NOT NULL clause. Since it is the default for any column, its presence is simply noise.

NULL "制約条件"(実際には制約条件では有りませんが)は、 SQL92 に対する Postgres の 拡張で、NOT NULL 句と対象的に実装されています。 どのカラムに対してもデフォルトであるため、この "制約条件"は単に余分なものです。

[ CONSTRAINT name ] NULL 
     

NOT NULL 句

SQL92 specifies some additional capabilities for NOT NULL:

SQL92 は NOT NULL にいくつかの追加的機能を明記しています。

[ CONSTRAINT name ] NOT NULL 
    [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
    [ [ NOT ] DEFERRABLE ]
     

CONSTRAINT 句

SQL92 specifies some additional capabilities for constraints, and also defines assertions and domain constraints.

SQL92 は制約条件にいくつかの追加的機能を明記していて、 主張 (assertion) 制約条件とドメイン制約条件も定義しています。

Note: Postgres does not yet support either domains or assertions.

Postgres は未だ、ドメインも 主張 (assertion) もサポートしていません。

An assertion is a special type of integrity constraint and share the same namespace as other constraints. However, an assertion is not necessarily dependent on one particular base table as constraints are, so SQL-92 provides the CREATE ASSERTION statement as an alternate method for defining a constraint:

主張 (assertion) は完全性保証制約条件の特殊な形態で、他の制約条件同様 おなじ制約条件グループに属しています。 とはいっても、主張 (assertion) は制約条件がそうであるように一つの特定の 基本テーブルに依存する必要がないため、SQL-92 は制約条件を 定義する別の方法として CREATE ASSERTION 文を用意しています。


CREATE ASSERTION name CHECK ( condition )
CREATE ASSERTION name CHECK ( 条件 )
    

Domain constraints are defined by CREATE DOMAIN or ALTER DOMAIN statements:

ドメイン制約条件は CREATE DOMAIN または ALTER DOMAIN 文 で定義されます。

Domain constraint:

ドメイン制約条件:

[ CONSTRAINT name ] CHECK constraint 
    [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
    [ [ NOT ] DEFERRABLE ]
     

Table constraint definition:

テーブル制約条件の定義:

[ CONSTRAINT name ] { PRIMARY KEY ( column, ... ) | FOREIGN KEY constraint | UNIQUE constraint | CHECK constraint }
    [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
    [ [ NOT ] DEFERRABLE ]
     

Column constraint definition:

カラム制約条件の定義:

[ CONSTRAINT name ] { NOT NULL | PRIMARY KEY | FOREIGN KEY constraint | UNIQUE | CHECK constraint }  
    [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
    [ [ NOT ] DEFERRABLE ]
     

A CONSTRAINT definition may contain one deferment attribute clause and/or one initial constraint mode clause, in any order.

CONSTRAINT 定義は一つの猶予属性句そして/または一つの初期 制約条件モード句を順序に関係なく含むことが出来ます。

NOT DEFERRABLE

means that the Constraint must be checked for violation of its rule after the execution of every SQL statement.

の意味は、それぞれの SQL 文が実行された後、ルールに 違反していないかどうかチェックされなければならない と言うことです。

DEFERRABLE

means that checking of the Constraint may be deferred until some later time, but no later than the end of the current transaction.

の意味は、制約条件のチェックが指定されたトランザクション が終了する前までであれば、後まで猶予しても構わないと言う ことです。

The constraint mode for every Constraint always has an initial default value which is set for that Constraint at the beginning of a transaction.

それぞれの制約条件に対する制約モードは、トランザクションが 開始した時点でその制約条件に対して設定されるデフォルトの初期値 を常に所有しています。

INITIALLY IMMEDIATE

means that, as of the start of the transaction, the Constraint must be checked for violation of its rule after the execution of every SQL statement.

の意味は、トランザクション開始時点で、それぞれの SQL 文 が実行された後、ルールに違反していないかどうかチェック されなければならいと言うことです。

INITIALLY DEFERRED

means that, as of the start of the transaction, checking of the Constraint may be deferred until some later time, but no later than the end of the current transaction.

の意味は、トランザクション開始時点で、制約条件のチェックが 指定されたトランザクションが終了する前までであれば、後に延期さ れても構わないと言うことです。

CHECK 句

SQL92 specifies some additional capabilities for CHECK in either table or column constraints.

SQL92 はテーブルまたはカラム制約条件の CHECK に いくつかの追加的機能を明記しています。

原文によるコメント Constraints associated with domains do not need to be mentioned here, even though it is the case that a domain constraint may possibly affect a column or a table. - Thomas 1998-08-16

A CHECK constraint is either a table constraint, a column constraint or a domain constraint.

table constraint definition:

テーブル制約条件の定義:

[ CONSTRAINT name ] CHECK ( VALUE condition ) 
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
     

column constraint definition:

カラム制約条件の定義:

[ CONSTRAINT name ] CHECK ( VALUE condition ) 
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
    

<!-- domain constraint definition: --> ドメイン制約条件の定義:

     [ CONSTRAINT name ] 
      CHECK ( VALUE condition ) 
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
    

<!-- CHECK domain constraints can be defined in either a CREATE DOMAIN statement or an ALTER DOMAIN statement: --> CHECK ドメイン制約条件は CREATE DOMAIN 文でも ALTER DOMAIN 文でも定義可能です:

CREATE DOMAIN duration AS SMALLINT 
    CONSTRAINT minutes CHECK (VALUE IN (90,120,180,240)); 

ALTER DOMAIN cities 
    ADD CONSTRAINT new_city CHECK (VALUE LIKE 'L%');
   

PRIMARY KEY 句

SQL92 specifies some additional capabilities for PRIMARY KEY:

SQL92 は PRIMARY KEY にいくつかの追加的機能を明記しています。

Table Constraint definition:

テーブル制約条件の定義:

[ CONSTRAINT name ] PRIMARY KEY ( column [, ...] ) 
    [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
    [ [ NOT ] DEFERRABLE ]
     

Column Constraint definition:

カラム制約条件の定義:

[ CONSTRAINT name ] PRIMARY KEY 
    [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
    [ [ NOT ] DEFERRABLE ]