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 變更一個現存表的定義。它有好幾種子形式:
- 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實現里還不支持新列/字段的缺?。ㄖ担┖?nbsp;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);