alter_table 中文man页面

系统
ALTER TABLE 变更一个现存表的定义。

NAME

ALTER TABLE - 修改表的定义

SYNOPSIS

ALTER TABLE [ ONLY ] name [ * ]
    ADD [ COLUMN ] column type [ column_constraint [ ... ] ]
ALTER TABLE [ ONLY ] name [ * ]
    DROP [ COLUMN ] column [ RESTRICT | CASCADE ]
ALTER TABLE [ ONLY ] name [ * ]
    ALTER [ COLUMN ] column { SET DEFAULT expression | DROP DEFAULT }
ALTER TABLE [ ONLY ] name [ * ]
    ALTER [ COLUMN ] column { SET | DROP } NOT NULL
ALTER TABLE [ ONLY ] name [ * ]
    ALTER [ COLUMN ] column SET STATISTICS integer
ALTER TABLE [ ONLY ] name [ * ]
    ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
ALTER TABLE [ ONLY ] name [ * ]
    SET WITHOUT OIDS
ALTER TABLE [ ONLY ] name [ * ]
    RENAME [ COLUMN ] column TO new_column
ALTER TABLE name
    RENAME TO new_name
ALTER TABLE [ ONLY ] name [ * ]
    ADD table_constraint
ALTER TABLE [ ONLY ] name [ * ]
    DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ]
ALTER TABLE name
    OWNER TO new_owner
ALTER TABLE name
    CLUSTER ON index_name

DESCRIPTION 描述

ALTER TABLE 变更一个现存表的定义。它有好几种子形式:

ADD COLUMN

 这种形式用和 CREATE TABLE [create_table(7)]  里一样的语法向表中增加一个新的字段。
DROP COLUMN

 这种形式从表中删除一个字段。请注意,和这个字段相关的索引和表约束也会被自动删除。 如果任何表之外的对象依赖于这个字段, 你必须说 CASCADE,比如,外键参考,视图等等。
SET/DROP DEFAULT

 这种形式为一个字段设置或者删除缺省值。请注意缺省值只应用于随后的 INSERT 命令; 它们不会导致已经在表中的行的数值的修改。我们也可以为视图创建缺省, 这个时候它们是在视图的 ON INSERT 规则应用之前插入 INSERT 语句中去的。
SET/DROP NOT NULL

 这些形式修改一个字段是否标记为允许 NULL 值或者是拒绝 NULL 值。 如果表在字段中包含非空值,那么你只可以 SET NOT NULL。
SET STATISTICS
This form
 这个形式为随后的 ANALYZE [analyze(7)] 操作设置每字段的统计收集目标。 目标的范围可以在 0 到 1000 之内设置;另外,把他设置为 -1 则表示重新恢复到使用系统缺省的统计目标。
SET STORAGE

 这种形式为一个字段设置存储模式。这个设置控制这个字段是内联保存还是保存在一个附属的表里,以及数据是否要压缩。 PLAIN 必需用于定长的数值,比如 integer,并且是内联的,不压缩的。 MAIN 用于内联,可压缩的数据。 EXTERNAL 用于外部保存,不压缩的数据, 而 EXTENDED 用于外部的压缩数据。 EXTENDED 是所有支持它的数据的缺省。 使用 EXTERNAL 将令在 text 字段上的子字串操作更快, 付出的代价是增加了存储空间。
SET WITHOUT OIDS

 从表中删除 oid 字段。从表中删除(设置为没有)oid 同样不会立即发生。 OID 使用的空间将在元组被更新的时候回收。不更新元组的时候, OID 的空间和数值的维护都是不确定的。这个过程语义上类似 DROP COLUMN  过程。
RENAME
RENAME 形式改变一个表的名字(或者是一个索引,一个序列,或者一个视图)或者是表中一个独立字段的名字。 它对存储的数据没有任何影响。
ADD table_constraint

 这个形式给表增加一个新的约束,用的语法和 CREATE TABLE [create_table(7)] 一样。
DROP CONSTRAINT

 这个形式删除一个表上的约束。 目前,在表上的约束不要求有唯一的名字,因此可能有多个约束匹配声明的名字。 所有这样的约束都将被删除。
OWNER

 这个形式改变表,索引,序列或者视图的所有者为指定所有者。
CLUSTER

 这种形式为将来对表进行的 CLUSTER [cluster(7)] 操作做标记。


 要使用 ALTER TABLE,你必需拥有该表; 除了 ALTER TABLE OWNER 之外,它只能由超级用户执行。  

PARAMETERS 参数

name

 试图更改的现存表(可能有模式修饰)的名称。 如果声明了 ONLY,则只更改该表。 如果没有声明 ONLY,则该表及其所有后代表(如果有)都被更新。 我们可以在表名字后面附加一个 * 表示后代表都被扫描,但是在目前的版本里,这是缺省行为。 (在7.1之前的版本,ONLY 是缺省的行为。)缺省可以通过改变配置选项 SQL_INHERITANCE 来改变。
column

 现存或新的字段名称。
type

 新字段的类型。
new_column

 新字段的类型。
new_name

 表的新名称。
table_constraint

 表的新的约束定义。
constraint_name

 要删除的现有约束的名字。
new_owner

 该表的新所有者的用户名。
index_name

 要标记为建簇的表上面的索引名字。
CASCADE

 自动删除依赖于被依赖字段或者约束的对象(比如,引用该字段的视图)。
RESTRICT

 如果字段或者约束还有任何依赖的对象,则拒绝删除该字段。 这是缺省行为。

NOTES 注意


 COLUMN 关键字是多余的,可以省略。


 在目前的 ADD COLUMN实现里还不支持新列/字段的缺省(值)和 NOT NULL 子句。 新字段开始存在时所有值都是 NULL。 不过你可以随后用 ALTER TABLE 的 SET DEFAULT  形式设置缺省(值)。(你可能还想用 UPDATE [update(7)] 把已存在行更新为缺省值。) 如果你想标记该字段为非 null,在你为该字段的所有行输入非 null 值之后用 SET NOT NULL。

DROP COLUMN 命令并不是物理上把字段删除, 而只是简单地把它标记为 SQL 操作中不可见的。随后对该表的插入和更新将在该字段存储一个 NULL。 因此,删除一个字段是很快的,但是它不会立即缩减你的表在磁盘上的大小,因为被删除了的字段占据的空间还没有回收。 这些空间将随着现有的行的更新而得到回收。要立即回收空间, 我们可以做一个UPDATE所有行的假动作,然后立即 vacuum, 象这样:

UPDATE table SET col = col;
VACUUM FULL table;


 如果表有任何后代表,那么如果不在后代表上做同样的修改的话, 就不允许在父表上增加或者重命名一个字段,也就是说, ALTER TABLE ONLY将被拒绝。这样就保证了后代表总是有和父表匹配的字段。


 一个递归DROP COLUMN  操作将只有在后代表并不从任何其它父表中继承该字段并且从来没有独立定义该字段的时候才能删除一个后代表的字段。 一个非递归的DROP COLUMN(也就是,ALTER TABLE ONLY ... DROP COLUMN)从来不会删除任何后代字段, 而是把他们标记为独立定义的,而不是继承的。


 不允许更改系统表结构的任何部分。


 请参考CREATE TABLE 部分获取更多有效参数的描述。 Chapter 5 ``Data Definition'' 里有更多有关继承的信息。  

EXAMPLES 例子


 向表中增加一个 varchar 列:

ALTER TABLE distributors ADD COLUMN address varchar(30);


 从表中删除一个字段:

ALTER TABLE distributors DROP COLUMN address RESTRICT;


 对现存列改名:

ALTER TABLE distributors RENAME COLUMN address TO city;


 更改现存表的名字∶

ALTER TABLE distributors RENAME TO suppliers;


 给一个字段增加一个非空约束:

ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;


 从一个字段里删除一个非空约束:

ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;


 给一个表增加一个检查约束:

ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);


 删除一个表和它的所有子表的监查约束:

ALTER TABLE distributors DROP CONSTRAINT zipchk;


 向表中增加一个外键约束:

ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;


 给表增加一个(多字段)唯一约束:

ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);


 给一个表增加一个自动命名的主键约束,要注意的是一个表只能有一个主键:

ALTER TABLE distributors ADD PRIMARY KEY (dist_id);

#p#

NAME

ALTER TABLE - change the definition of a table

SYNOPSIS

ALTER TABLE [ ONLY ] name [ * ]
    ADD [ COLUMN ] column type [ column_constraint [ ... ] ]
ALTER TABLE [ ONLY ] name [ * ]
    DROP [ COLUMN ] column [ RESTRICT | CASCADE ]
ALTER TABLE [ ONLY ] name [ * ]
    ALTER [ COLUMN ] column { SET DEFAULT expression | DROP DEFAULT }
ALTER TABLE [ ONLY ] name [ * ]
    ALTER [ COLUMN ] column { SET | DROP } NOT NULL
ALTER TABLE [ ONLY ] name [ * ]
    ALTER [ COLUMN ] column SET STATISTICS integer
ALTER TABLE [ ONLY ] name [ * ]
    ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
ALTER TABLE [ ONLY ] name [ * ]
    SET WITHOUT OIDS
ALTER TABLE [ ONLY ] name [ * ]
    RENAME [ COLUMN ] column TO new_column
ALTER TABLE name
    RENAME TO new_name
ALTER TABLE [ ONLY ] name [ * ]
    ADD table_constraint
ALTER TABLE [ ONLY ] name [ * ]
    DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ]
ALTER TABLE name
    OWNER TO new_owner
ALTER TABLE name
    CLUSTER ON index_name

DESCRIPTION

ALTER TABLE changes the definition of an existing table. There are several subforms:

ADD COLUMN
This form adds a new column to the table using the same syntax as CREATE TABLE [create_table(7)].
DROP COLUMN
This form drops a column from a table. Indexes and table constraints involving the column will be automatically dropped as well. You will need to say CASCADE if anything outside the table depends on the column, for example, foreign key references or views.
SET/DROP DEFAULT
These forms set or remove the default value for a column. The default values only apply to subsequent INSERT commands; they do not cause rows already in the table to change. Defaults may also be created for views, in which case they are inserted into INSERT statements on the view before the view's ON INSERT rule is applied.
SET/DROP NOT NULL
These forms change whether a column is marked to allow null values or to reject null values. You can only use SET NOT NULL when the column contains no null values.
SET STATISTICS
This form sets the per-column statistics-gathering target for subsequent ANALYZE [analyze(7)] operations. The target can be set in the range 0 to 1000; alternatively, set it to -1 to revert to using the system default statistics target.
SET STORAGE
This form sets the storage mode for a column. This controls whether this column is held inline or in a supplementary table, and whether the data should be compressed or not. PLAIN must be used for fixed-length values such as integer and is inline, uncompressed. MAIN is for inline, compressible data. EXTERNAL is for external, uncompressed data, and EXTENDED is for external, compressed data. EXTENDED is the default for all data types that support it. The use of EXTERNAL will, for example, make substring operations on a text column faster, at the penalty of increased storage space.
SET WITHOUT OIDS
This form removes the oid column from the table. Removing OIDs from a table does not occur immediately. The space that the OID uses will be reclaimed when the row is updated. Without updating the row, both the space and the value of the OID are kept indefinitely. This is semantically similar to the DROP COLUMN process.
RENAME
The RENAME forms change the name of a table (or an index, sequence, or view) or the name of an individual column in a table. There is no effect on the stored data.
ADD table_constraint
This form adds a new constraint to a table using the same syntax as CREATE TABLE [create_table(7)].
DROP CONSTRAINT
This form drops constraints on a table. Currently, constraints on tables are not required to have unique names, so there may be more than one constraint matching the specified name. All such constraints will be dropped.
OWNER
This form changes the owner of the table, index, sequence, or view to the specified user.
CLUSTER
This form marks a table for future CLUSTER [cluster(7)] operations.

You must own the table to use ALTER TABLE; except for ALTER TABLE OWNER, which may only be executed by a superuser.  

PARAMETERS

name
The name (possibly schema-qualified) of an existing table to alter. If ONLY is specified, only that table is altered. If ONLY is not specified, the table and all its descendant tables (if any) are updated. * can be appended to the table name to indicate that descendant tables are to be altered, but in the current version, this is the default behavior. (In releases before 7.1, ONLY was the default behavior. The default can be altered by changing the configuration parameter sql_inheritance.)
column
Name of a new or existing column.
type
Data type of the new column.
new_column
New name for an existing column.
new_name
New name for the table.
table_constraint
New table constraint for the table.
constraint_name
Name of an existing constraint to drop.
new_owner
The user name of the new owner of the table.
index_name
The index name on which the table should be marked for clustering.
CASCADE
Automatically drop objects that depend on the dropped column or constraint (for example, views referencing the column).
RESTRICT
Refuse to drop the column or constraint if there are any dependent objects. This is the default behavior.

NOTES

The key word COLUMN is noise and can be omitted.

In the current implementation of ADD COLUMN, default and NOT NULL clauses for the new column are not supported. The new column always comes into being with all values null. You can use the SET DEFAULT form of ALTER TABLE to set the default afterward. (You may also want to update the already existing rows to the new default value, using UPDATE [update(7)].) If you want to mark the column non-null, use the SET NOT NULL form after you've entered non-null values for the column in all rows.

The DROP COLUMN form does not physically remove the column, but simply makes it invisible to SQL operations. Subsequent insert and update operations in the table will store a null value for the column. Thus, dropping a column is quick but it will not immediately reduce the on-disk size of your table, as the space occupied by the dropped column is not reclaimed. The space will be reclaimed over time as existing rows are updated. To reclaim the space at once, do a dummy UPDATE of all rows and then vacuum, as in:

UPDATE table SET col = col;
VACUUM FULL table;

If a table has any descendant tables, it is not permitted to add or rename a column in the parent table without doing the same to the descendants. That is, ALTER TABLE ONLY will be rejected. This ensures that the descendants always have columns matching the parent.

A recursive DROP COLUMN operation will remove a descendant table's column only if the descendant does not inherit that column from any other parents and never had an independent definition of the column. A nonrecursive DROP COLUMN (i.e., ALTER TABLE ONLY ... DROP COLUMN) never removes any descendant columns, but instead marks them as independently defined rather than inherited.

Changing any part of a system catalog table is not permitted.

Refer to CREATE TABLE for a further description of valid parameters. The chapter called ``Data Definition'' in the documentation has further information on inheritance.  

EXAMPLES

To add a column of type varchar to a table:

ALTER TABLE distributors ADD COLUMN address varchar(30);

To drop a column from a table:

ALTER TABLE distributors DROP COLUMN address RESTRICT;

To rename an existing column:

ALTER TABLE distributors RENAME COLUMN address TO city;

To rename an existing table:

ALTER TABLE distributors RENAME TO suppliers;

To add a not-null constraint to a column:

ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;

To remove a not-null constraint from a column:

ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;

To add a check constraint to a table:

ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);

To remove a check constraint from a table and all its children:

ALTER TABLE distributors DROP CONSTRAINT zipchk;

To add a foreign key constraint to a table:

ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;

To add a (multicolumn) unique constraint to a table:

ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);

To add an automatically named primary key constraint to a table, noting that a table can only ever have one primary key:

ALTER TABLE distributors ADD PRIMARY KEY (dist_id);

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-24 09:02:10

ALTER AGGRE中文man

2011-08-24 09:42:15

alter_seque中文man

2011-08-24 09:51:53

alter_user中文man

2011-08-24 09:14:47

alter_conve中文man

2011-08-24 09:48:46

alter_trigg中文man

2011-08-24 09:26:14

alter_funct中文man

2011-08-24 09:22:30

alter_domai中文man

2011-08-24 09:29:18

alter_group中文man

2011-08-24 09:32:13

alter_langu中文man

2011-08-24 09:39:10

alter_schem中文man

2011-08-24 09:18:45

alter_datab中文man

2011-08-24 13:29:20

CREATE TABL中文man

2011-08-24 13:32:56

CREATE TABL中文man

2011-08-24 09:36:00

alter_opera中文man

2011-08-24 14:49:13

drop_table中文man

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-11 15:03:21

ACCESS中文man
点赞
收藏

51CTO技术栈公众号