ALTER TABLE

Name

ALTER TABLE  --  Modifies table properties 表のプロパティを更新する

Synopsis

ALTER TABLE table
    [ * ] ADD [ COLUMN ] column type
ALTER TABLE table
    [ * ] RENAME [ COLUMN ] column TO newcolumn
ALTER TABLE table
    RENAME TO newtable
  

入力

table

The name of an existing table to alter.

更新対象の、既存の表の名前

column

Name of a new or existing column.

新規または既存の列の名前

type

Type of the new column.

新しい列の型

newcolumn

New name for an existing column.

既存の列の新しい名前

newtable

New name for an existing column.

既存の表の新しい名前

出力

ALTER

Message returned from column or table renaming.

列または表のリネームの結果返されるメッセージ。

NEW

Message returned from column addition.

列の追加の結果返されるメッセージ。

ERROR

Message returned if table or column is not available.

表または列が得られなかった場合に返されるメッセージ。

説明

ALTER TABLE changes the definition of an existing table. The new columns and their types are specified in the same style and with the the same restrictions as in CREATE TABLE. The RENAME clause causes the name of a table or column to change without changing any of the data contained in the affected table. Thus, the table or column will remain of the same type and size after this command is executed.

ALTER TABLE は、既存の表の定義を変更します。 新しい列とそれらの型が、CREATE TABLE の同じ様式 および同じ制限で指定されます。 RENAME 句は当該表に含まれるいかなるデータをも変更することなく、 表もしくは列の名前の変更を引き起こします。 したがって、その表または列は、このコマンドが実行された後、同じ 型とサイズで存続します。

You must own the table in order to change its schema.

表のスキーマを変更するためには、その表を所有して いなければなりません。

注意事項

The keyword COLUMN is noise and can be omitted.

キーワードCOLUMN はノイズであり、省略可能です。

"[*]" following a name of a table indicates that statement should be run over that table and all tables below it in the inheritance hierarchy.

表の名前に続く"[*]" は、文がその表と 継承階層の中でのその下の全ての表に上書きされること を表します。 The PostgreSQL User's Guide has further information on inheritance. PostgreSQL User's Guide に、継承についての 更なる情報があります。

Refer to CREATE TABLE for a further description of valid arguments.

有効な引数の更なる説明は、CREATE TABLE 参照。

使用法

To add a column of type VARCHAR to a table:

VARCHAR 型の列を表に追加する:

ALTER TABLE distributors ADD COLUMN address VARCHAR(30);
   

To rename an existing column:

既存の列の名前を変える:

ALTER TABLE distributors RENAME COLUMN address TO city;
   

To rename an existing table:

既存の表の名前を変える:

ALTER TABLE distributors RENAME TO suppliers;
   

互換性

SQL92

ALTER TABLE/RENAME is a Postgres language extension.

ALTER TABLE/RENAMEPostgres の言語拡張です。

SQL92 specifies some additional capabilities for ALTER TABLE statement which are not yet directly supported by Postgres:

SQL92 は、Postgres でまだ直接サポート していないALTER TABLE 文に対するいくつかの 付加機能を規定します。

ALTER TABLE table ALTER [
       COLUMN ] column
    SET DEFAULT default
ALTER TABLE table ALTER [
       COLUMN ] column
    ADD [ CONSTRAINT >constrain> ] table-constraint
      

Puts the default value or constraint specified into the definition of column in the table. See CREATE TABLE for the syntax of the default and table-constraint clauses. If a default clause already exists, it will be replaced by the new definition. If any constraints on this column already exist, they will be retained using a boolean AND with the new constraint.

デフォルトの値や制約を表の列の定義に設定します。 default と table-constraint 句の文法については CREATE TABLE を見て下さい。 もしdefault 句が既に存在していると、新しい定義に置き換えられます。 もし何か制約がこの列にすでに存在していると、それらは ブール代数のAND を用いて新しい制約とともに保持されます。

Currently, to set new default constraints on an existing column the table must be recreated and reloaded:

現在、新しいdefault 制約を既存の列に設定するためには、 その表は再作成、リロードされなければなりません。

CREATE TABLE temp AS SELECT * FROM distributors;
DROP TABLE distributors;
CREATE TABLE distributors (
    did      DECIMAL(3) DEFAULT 1,
    name     VARCHAR(40) NOT NULL,
    city     VARCHAR(30)
);
INSERT INTO distributors SELECT * FROM temp;
DROP TABLE temp;
       

ALTER TABLE table
    DROP DEFAULT default
ALTER TABLE table
    DROP CONSTRAINT constraint { RESTRICT | CASCADE }
      

Removes the default value specified by default or the rule specified by constraint from the definition of a table. If RESTRICT is specified only a constraint with no dependent constraints can be destroyed. If CASCADE is specified, Any constraints that are dependent on this constraint are also dropped.

default で設定されたデフォルト値やconstraint で指定された 規則を表の制約から除きます。 もしRESTRICT が指定されたら、この制約に従属したどの制約も伴わない ただ一つの制約が除かれます。 もしCASCADE が指定されたら、この制約に従属したどの制約も 除かれます。

Currently, to remove a default value or constraints on an existing column the table must be recreated and reloaded:

現在、デフォルト値や制約を、既存の列から除くためには、 その表は再作成、リロードされなければなりません。

CREATE TABLE temp AS SELECT * FROM distributors;
DROP TABLE distributors;
CREATE TABLE distributors AS SELECT * FROM temp;
DROP TABLE temp;
       

ALTER TABLE table
    DROP [ COLUMN ] column { RESTRICT | CASCADE }
      

Removes a column from a table. If RESTRICT is specified only a column with no dependent objects can be destroyed. If CASCADE is specified, all objects that are dependent on this column are also dropped.

表から列を除きます。 もしRESTRICT が指定されたら、この列に従属したどのオブジェクト も伴わないただ一つの列が除かれます。 もしCASCADE が指定されたら、この列に従属したどのオブジェクトも 除かれます。

Currently, to remove an existing column the table must be recreated and reloaded:

現在、既存の列を除くためには、 その表は再作成、リロードされなければなりません。

CREATE TABLE temp AS SELECT did, city FROM distributors;    
DROP TABLE distributors;
CREATE TABLE distributors (
    did      DECIMAL(3)  DEFAULT 1,
    name     VARCHAR(40) NOT NULL,
);
INSERT INTO distributors SELECT * FROM temp;
DROP TABLE temp;