alter_table 中文man頁面
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 變更一個(gè)現(xiàn)存表的定義。它有好幾種子形式:
- ADD COLUMN
這種形式用和 CREATE TABLE [create_table(7)] 里一樣的語法向表中增加一個(gè)新的字段。- DROP COLUMN
這種形式從表中刪除一個(gè)字段。請(qǐng)注意,和這個(gè)字段相關(guān)的索引和表約束也會(huì)被自動(dòng)刪除。 如果任何表之外的對(duì)象依賴于這個(gè)字段, 你必須說 CASCADE,比如,外鍵參考,視圖等等。- SET/DROP DEFAULT
這種形式為一個(gè)字段設(shè)置或者刪除缺省值。請(qǐng)注意缺省值只應(yīng)用于隨后的 INSERT 命令; 它們不會(huì)導(dǎo)致已經(jīng)在表中的行的數(shù)值的修改。我們也可以為視圖創(chuàng)建缺省, 這個(gè)時(shí)候它們是在視圖的 ON INSERT 規(guī)則應(yīng)用之前插入 INSERT 語句中去的。- SET/DROP NOT NULL
這些形式修改一個(gè)字段是否標(biāo)記為允許 NULL 值或者是拒絕 NULL 值。 如果表在字段中包含非空值,那么你只可以 SET NOT NULL。- SET STATISTICS
- This form
這個(gè)形式為隨后的 ANALYZE [analyze(7)] 操作設(shè)置每字段的統(tǒng)計(jì)收集目標(biāo)。 目標(biāo)的范圍可以在 0 到 1000 之內(nèi)設(shè)置;另外,把他設(shè)置為 -1 則表示重新恢復(fù)到使用系統(tǒng)缺省的統(tǒng)計(jì)目標(biāo)。 - SET STORAGE
這種形式為一個(gè)字段設(shè)置存儲(chǔ)模式。這個(gè)設(shè)置控制這個(gè)字段是內(nèi)聯(lián)保存還是保存在一個(gè)附屬的表里,以及數(shù)據(jù)是否要壓縮。 PLAIN 必需用于定長(zhǎng)的數(shù)值,比如 integer,并且是內(nèi)聯(lián)的,不壓縮的。 MAIN 用于內(nèi)聯(lián),可壓縮的數(shù)據(jù)。 EXTERNAL 用于外部保存,不壓縮的數(shù)據(jù), 而 EXTENDED 用于外部的壓縮數(shù)據(jù)。 EXTENDED 是所有支持它的數(shù)據(jù)的缺省。 使用 EXTERNAL 將令在 text 字段上的子字串操作更快, 付出的代價(jià)是增加了存儲(chǔ)空間。- SET WITHOUT OIDS
從表中刪除 oid 字段。從表中刪除(設(shè)置為沒有)oid 同樣不會(huì)立即發(fā)生。 OID 使用的空間將在元組被更新的時(shí)候回收。不更新元組的時(shí)候, OID 的空間和數(shù)值的維護(hù)都是不確定的。這個(gè)過程語義上類似 DROP COLUMN 過程。- RENAME
- RENAME 形式改變一個(gè)表的名字(或者是一個(gè)索引,一個(gè)序列,或者一個(gè)視圖)或者是表中一個(gè)獨(dú)立字段的名字。 它對(duì)存儲(chǔ)的數(shù)據(jù)沒有任何影響。
- ADD table_constraint
這個(gè)形式給表增加一個(gè)新的約束,用的語法和 CREATE TABLE [create_table(7)] 一樣。- DROP CONSTRAINT
這個(gè)形式刪除一個(gè)表上的約束。 目前,在表上的約束不要求有唯一的名字,因此可能有多個(gè)約束匹配聲明的名字。 所有這樣的約束都將被刪除。- OWNER
這個(gè)形式改變表,索引,序列或者視圖的所有者為指定所有者。- CLUSTER
這種形式為將來對(duì)表進(jìn)行的 CLUSTER [cluster(7)] 操作做標(biāo)記。
要使用 ALTER TABLE,你必需擁有該表; 除了 ALTER TABLE OWNER 之外,它只能由超級(jí)用戶執(zhí)行。
PARAMETERS 參數(shù)
- name
試圖更改的現(xiàn)存表(可能有模式修飾)的名稱。 如果聲明了 ONLY,則只更改該表。 如果沒有聲明 ONLY,則該表及其所有后代表(如果有)都被更新。 我們可以在表名字后面附加一個(gè) * 表示后代表都被掃描,但是在目前的版本里,這是缺省行為。 (在7.1之前的版本,ONLY 是缺省的行為。)缺省可以通過改變配置選項(xiàng) SQL_INHERITANCE 來改變。- column
現(xiàn)存或新的字段名稱。- type
新字段的類型。- new_column
新字段的類型。- new_name
表的新名稱。- table_constraint
表的新的約束定義。- constraint_name
要?jiǎng)h除的現(xiàn)有約束的名字。- new_owner
該表的新所有者的用戶名。- index_name
要標(biāo)記為建簇的表上面的索引名字。- CASCADE
自動(dòng)刪除依賴于被依賴字段或者約束的對(duì)象(比如,引用該字段的視圖)。- RESTRICT
如果字段或者約束還有任何依賴的對(duì)象,則拒絕刪除該字段。 這是缺省行為。
NOTES 注意
COLUMN 關(guān)鍵字是多余的,可以省略。
在目前的 ADD COLUMN實(shí)現(xiàn)里還不支持新列/字段的缺省(值)和 NOT NULL 子句。 新字段開始存在時(shí)所有值都是 NULL。 不過你可以隨后用 ALTER TABLE 的 SET DEFAULT 形式設(shè)置缺?。ㄖ担?。(你可能還想用 UPDATE [update(7)] 把已存在行更新為缺省值。) 如果你想標(biāo)記該字段為非 null,在你為該字段的所有行輸入非 null 值之后用 SET NOT NULL。
DROP COLUMN 命令并不是物理上把字段刪除, 而只是簡(jiǎn)單地把它標(biāo)記為 SQL 操作中不可見的。隨后對(duì)該表的插入和更新將在該字段存儲(chǔ)一個(gè) NULL。 因此,刪除一個(gè)字段是很快的,但是它不會(huì)立即縮減你的表在磁盤上的大小,因?yàn)楸粍h除了的字段占據(jù)的空間還沒有回收。 這些空間將隨著現(xiàn)有的行的更新而得到回收。要立即回收空間, 我們可以做一個(gè)UPDATE所有行的假動(dòng)作,然后立即 vacuum, 象這樣:
UPDATE table SET col = col; VACUUM FULL table;
如果表有任何后代表,那么如果不在后代表上做同樣的修改的話, 就不允許在父表上增加或者重命名一個(gè)字段,也就是說, ALTER TABLE ONLY將被拒絕。這樣就保證了后代表總是有和父表匹配的字段。
一個(gè)遞歸DROP COLUMN 操作將只有在后代表并不從任何其它父表中繼承該字段并且從來沒有獨(dú)立定義該字段的時(shí)候才能刪除一個(gè)后代表的字段。 一個(gè)非遞歸的DROP COLUMN(也就是,ALTER TABLE ONLY ... DROP COLUMN)從來不會(huì)刪除任何后代字段, 而是把他們標(biāo)記為獨(dú)立定義的,而不是繼承的。
不允許更改系統(tǒng)表結(jié)構(gòu)的任何部分。
請(qǐng)參考CREATE TABLE 部分獲取更多有效參數(shù)的描述。 Chapter 5 ``Data Definition'' 里有更多有關(guān)繼承的信息。
EXAMPLES 例子
向表中增加一個(gè) varchar 列:
ALTER TABLE distributors ADD COLUMN address varchar(30);
從表中刪除一個(gè)字段:
ALTER TABLE distributors DROP COLUMN address RESTRICT;
對(duì)現(xiàn)存列改名:
ALTER TABLE distributors RENAME COLUMN address TO city;
更改現(xiàn)存表的名字∶
ALTER TABLE distributors RENAME TO suppliers;
給一個(gè)字段增加一個(gè)非空約束:
ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
從一個(gè)字段里刪除一個(gè)非空約束:
ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
給一個(gè)表增加一個(gè)檢查約束:
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
刪除一個(gè)表和它的所有子表的監(jiān)查約束:
ALTER TABLE distributors DROP CONSTRAINT zipchk;
向表中增加一個(gè)外鍵約束:
ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
給表增加一個(gè)(多字段)唯一約束:
ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
給一個(gè)表增加一個(gè)自動(dòng)命名的主鍵約束,要注意的是一個(gè)表只能有一個(gè)主鍵:
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);