# 更改外表

ALTER FOREIGN TABLE — 更改外部表的定义

# 概要

ALTER FOREIGN TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
    action [, ... ]
ALTER FOREIGN TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
    RENAME [ COLUMN ] column_name TO new_column_name
ALTER FOREIGN TABLE [ IF EXISTS ] name
    RENAME TO new_name
ALTER FOREIGN TABLE [ IF EXISTS ] name
    SET SCHEMA new_schema

where action is one of:

    ADD [ COLUMN ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
    DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]
    ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ]
    ALTER [ COLUMN ] column_name SET DEFAULT expression
    ALTER [ COLUMN ] column_name DROP DEFAULT
    ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL
    ALTER [ COLUMN ] column_name SET STATISTICS integer
    ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] )
    ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] )
    ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
    ALTER [ COLUMN ] column_name OPTIONS ( [ ADD | SET | DROP ] option ['value'] [, ... ])
    ADD table_constraint [ NOT VALID ]
    VALIDATE CONSTRAINT constraint_name
    DROP CONSTRAINT [ IF EXISTS ]  constraint_name [ RESTRICT | CASCADE ]
    DISABLE TRIGGER [ trigger_name | ALL | USER ]
    ENABLE TRIGGER [ trigger_name | ALL | USER ]
    ENABLE REPLICA TRIGGER trigger_name
    ENABLE ALWAYS TRIGGER trigger_name
    SET WITHOUT OIDS
    INHERIT parent_table
    NO INHERIT parent_table
    OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
    OPTIONS ( [ ADD | SET | DROP ] option ['value'] [, ... ])

# 描述

更改外表更改现有外部表的定义。有几种子形式:

添加列

此表单使用与以下相同的语法向外表添加一个新列创建外表.与将列添加到常规表的情况不同,底层存储不会发生任何事情:此操作只是声明现在可以通过外表访问某些新列。

删除列 [如果存在]

此表单从外部表中删除一列。你需要说级联如果表外的任何内容取决于列;例如,视图。如果如果存在指定并且该列不存在,则不会引发错误。在这种情况下,将发出通知。

设置数据类型

这种形式改变了外表的一列的类型。同样,这对任何底层存储都没有影响:此操作只是更改 PostgreSQL 认为该列具有的类型。

/删除默认值

这些表单设置或删除列的默认值。默认值仅适用于后续插入要么更新命令;它们不会导致表中已经存在的行发生变化。

/删除不为空

将列标记为允许或不允许空值。

设置统计

这种形式为后续的每列统计收集目标设置分析操作。见类似的形式更改表更多细节。

放 ( *属性选项* = *价值* [, ... ] )
重启 ( *属性选项* [, ... ] )

此表单设置或重置每个属性选项。见类似的形式更改表更多细节。

设置存储

此表单设置列的存储模式。见类似的形式更改表更多细节。请注意,除非表的外部数据包装器选择注意存储模式,否则存储模式无效。

添加 *表约束* [ 无效 ]

此表单使用与以下相同的语法向外部表添加新约束创建外表.目前仅查看支持约束。

与将约束添加到常规表的情况不同,没有做任何事情来验证约束是否正确;相反,这个动作只是声明了一些新的条件应该被假定为外部表中的所有行都成立。(见讨论CREATE FOREIGN TABLE.) If the constraint is markedNOT VALID, then it isn't assumed to hold, but is only recorded for possible future use.

VALIDATE CONSTRAINT

This form marks as valid a constraint that was previously marked asNOT VALID. No action is taken to verify the constraint, but future queries will assume that it holds.

DROP CONSTRAINT [ IF EXISTS ]

This form drops the specified constraint on a foreign table. IfIF EXISTSis specified and the constraint does not exist, no error is thrown. In this case a notice is issued instead.

DISABLE/ENABLE [ REPLICA | ALWAYS ] TRIGGER

These forms configure the firing of trigger(s) belonging to the foreign table. See the similar form ofALTER TABLEfor more details.

SET WITHOUT OIDS

Backward compatibility syntax for removing theoidsystem column. Asoidsystem columns cannot be added anymore, this never has an effect.

INHERIT *parent_table*

This form adds the target foreign table as a new child of the specified parent table. See the similar form ofALTER TABLEfor more details.

没有继承 *父表*

此表单从指定父表的子表中删除目标外部表。

所有者

此表单将外部表的所有者更改为指定用户。

选项([添加|设置|删除] *选项* ['*价值*'] [, ... ] )

更改外部表或其列之一的选项。添加,, 和降低指定要执行的操作。添加如果没有明确指定操作,则假定。不允许重复的选项名称(尽管表选项和列选项具有相同的名称是可以的)。选项名称和值也使用外部数据包装库进行验证。

改名

改名表单更改外部表的名称或外部表中单个列的名称。

设置架构

这种形式将外部表移动到另一个模式中。

除了改名设置架构可以组合成多个更改的列表以并行应用。例如,可以在单个命令中添加几列和/或更改几列的类型。

如果命令写成ALTER FOREIGN TABLE IF EXISTS ...并且外表不存在,不会抛出错误。在这种情况下发出通知。

您必须拥有该表才能使用更改外表.要更改外部表的架构,您还必须具有创建新架构的特权。要更改所有者,您还必须是新所有者角色的直接或间接成员,并且该角色必须具有创建表架构的特权。(这些限制强制改变所有者不会做任何你不能通过删除和重新创建表来做的事情。但是,超级用户无论如何都可以改变任何表的所有权。)要添加列或更改列类型,您必须也有用法数据类型的特权。

# 参数

姓名

要更改的现有外部表的名称(可能是模式限定的)。如果只要在表名之前指定,仅更改该表。如果只要未指定时,该表及其所有后代表(如果有)将被更改。可选地,*可以在表名之后指定以明确指示包含后代表。

列名

新列或现有列的名称。

新列名

现有列的新名称。

新名字

表的新名称。

数据类型

新列的数据类型,或现有列的新数据类型。

table_constraint

New table constraint for the foreign table.

constraint_name

Name of an existing constraint to drop.

CASCADE

Automatically drop objects that depend on the dropped column or constraint (for example, views referencing the column), and in turn all objects that depend on those objects (seeSection 5.14).

RESTRICT

Refuse to drop the column or constraint if there are any dependent objects. This is the default behavior.

trigger_name

Name of a single trigger to disable or enable.

ALL

Disable or enable all triggers belonging to the foreign table. (This requires superuser privilege if any of the triggers are internally generated triggers. The core system does not add such triggers to foreign tables, but add-on code could do so.)

USER

Disable or enable all triggers belonging to the foreign table except for internally generated triggers.

parent_table

A parent table to associate or de-associate with this foreign table.

new_owner

The user name of the new owner of the table.

new_schema

The name of the schema to which the table will be moved.

# Notes

The key wordCOLUMNis noise and can be omitted.

Consistency with the foreign server is not checked when a column is added or removed withADD COLUMNorDROP COLUMN, 一种非空或者查看添加约束,或更改列类型设置数据类型.确保表定义与远程端匹配是用户的责任。

参考创建外表有关有效参数的进一步说明。

# 例子

要将列标记为非空:

ALTER FOREIGN TABLE distributors ALTER COLUMN street SET NOT NULL;

要更改外部表的选项:

ALTER FOREIGN TABLE myschema.distributors OPTIONS (ADD opt1 'value', SET opt2 'value2', DROP opt3 'value3');

# 兼容性

表格添加,降低, 和设置数据类型符合 SQL 标准。其他形式是 SQL 标准的 PostgreSQL 扩展。此外,能够在单个操作中指定多个操作更改外表命令是一个扩展。

ALTER FOREIGN TABLE DROP 列可用于删除外部表的唯一列,留下一个零列表。这是 SQL 的扩展,它不允许零列外部表。

# 也可以看看

创建外表,删除外国表