alter_table.7
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 529 行 · 第 1/2 页
7
529 行
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "ALTER TABLE" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMEALTER TABLE \- change the definition of a table.SH SYNOPSIS.sp.nfALTER TABLE [ ONLY ] \fIname\fR [ * ] \fIaction\fR [, ... ]ALTER TABLE [ ONLY ] \fIname\fR [ * ] RENAME [ COLUMN ] \fIcolumn\fR TO \fInew_column\fRALTER TABLE \fIname\fR RENAME TO \fInew_name\fRALTER TABLE \fIname\fR SET SCHEMA \fInew_schema\fRwhere \fIaction\fR is one of: ADD [ COLUMN ] \fIcolumn\fR \fItype\fR [ \fIcolumn_constraint\fR [ ... ] ] DROP [ COLUMN ] \fIcolumn\fR [ RESTRICT | CASCADE ] ALTER [ COLUMN ] \fIcolumn\fR TYPE \fItype\fR [ USING \fIexpression\fR ] ALTER [ COLUMN ] \fIcolumn\fR SET DEFAULT \fIexpression\fR ALTER [ COLUMN ] \fIcolumn\fR DROP DEFAULT ALTER [ COLUMN ] \fIcolumn\fR { SET | DROP } NOT NULL ALTER [ COLUMN ] \fIcolumn\fR SET STATISTICS \fIinteger\fR ALTER [ COLUMN ] \fIcolumn\fR SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN } ADD \fItable_constraint\fR DROP CONSTRAINT \fIconstraint_name\fR [ RESTRICT | CASCADE ] DISABLE TRIGGER [ \fItrigger_name\fR | ALL | USER ] ENABLE TRIGGER [ \fItrigger_name\fR | ALL | USER ] CLUSTER ON \fIindex_name\fR SET WITHOUT CLUSTER SET WITHOUT OIDS SET ( \fIstorage_parameter\fR = \fIvalue\fR [, ... ] ) RESET ( \fIstorage_parameter\fR [, ... ] ) INHERIT \fIparent_table\fR NO INHERIT \fIparent_table\fR OWNER TO \fInew_owner\fR SET TABLESPACE \fInew_tablespace\fR.sp.fi.SH "DESCRIPTION".PP\fBALTER TABLE\fR changes the definition of an existing table.There are several subforms:.TP\fBADD COLUMN\fRThis form adds a new column to the table, using the same syntax asCREATE TABLE [\fBcreate_table\fR(7)]..TP\fBDROP COLUMN\fRThis form drops a column from a table. Indexes andtable constraints involving the column will be automaticallydropped as well. You will need to say CASCADE ifanything outside the table depends on the column, for example,foreign key references or views..TP\fBALTER COLUMN TYPE\fRThis form changes the type of a column of a table. Indexes andsimple table constraints involving the column will be automaticallyconverted to use the new column type by reparsing the originallysupplied expression. The optional USINGclause specifies how to compute the new column value from the old;if omitted, the default conversion is the same as an assignmentcast from old data type to new. A USINGclause must be provided if there is no implicit or assignmentcast from old to new type..TP\fBSET/DROP DEFAULT\fRThese forms set or remove the default value for a column.The default values only apply to subsequent \fBINSERT\fRcommands; they do not cause rows already in the table to change.Defaults may also be created for views, in which case they areinserted into \fBINSERT\fR statements on the view beforethe view's ON INSERT rule is applied..TP\fBSET/DROP NOT NULL\fRThese forms change whether a column is marked to allow nullvalues or to reject null values. You can only use SETNOT NULL when the column contains no null values..TP\fBSET STATISTICS\fRThis formsets the per-column statistics-gathering target for subsequentANALYZE [\fBanalyze\fR(7)] operations.The target can be set in the range 0 to 1000; alternatively, set itto -1 to revert to using the system default statisticstarget (default_statistics_target).For more information on the use of statistics by thePostgreSQL query planner, refer toin the documentation..TP\fBSET STORAGE\fRThis form sets the storage mode for a column. This controls whether thiscolumn is held inline or in a supplementary table, and whether the datashould be compressed or not. PLAIN must be usedfor fixed-length values such as \fBinteger\fR and isinline, 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 mostdata types that support non-PLAIN storage.Use of EXTERNAL willmake substring operations on \fBtext\fR and \fBbytea\fRcolumns faster, at the penalty of increased storage space. Note thatSET STORAGE doesn't itself change anything in the table,it just sets the strategy to be pursued during future table updates.See in the documentation for more information..TP\fBADD \fItable_constraint\fB\fRThis form adds a new constraint to a table using the same syntax asCREATE TABLE [\fBcreate_table\fR(7)]. .TP\fBDROP CONSTRAINT\fRThis form drops the specified constraint on a table..TP\fBDISABLE/ENABLE TRIGGER\fRThese forms disable or enable trigger(s) belonging to the table.A disabled trigger is still known to the system, but is not executedwhen its triggering event occurs. For a deferred trigger, the enablestatus is checked when the event occurs, not when the trigger functionis actually executed. One may disable or enable a singletrigger specified by name, or all triggers on the table, or onlyuser triggers (this option excludes triggers that are used to implementforeign key constraints). Disabling or enabling constraint triggersrequires superuser privileges; it should be done with caution sinceof course the integrity of the constraint cannot be guaranteed if thetriggers are not executed..TP\fBCLUSTER\fRThis form selects the default index for future CLUSTER [\fBcluster\fR(7)]operations. It does not actually re-cluster the table..TP\fBSET WITHOUT CLUSTER\fRThis form removes the most recently usedCLUSTER [\fBcluster\fR(7)]index specification from the table. This affectsfuture cluster operations that don't specify an index..TP\fBSET WITHOUT OIDS\fRThis form removes the oid system column from thetable. This is exactly equivalent toDROP COLUMN oid RESTRICT,except that it will not complain if there is already nooid column.Note that there is no variant of \fBALTER TABLE\fRthat allows OIDs to be restored to a table once they have beenremoved..TP\fBSET ( \fIstorage_parameter\fB = \fIvalue\fB [, ... ] )\fRThis form changes one or more storage parameters for the table. SeeCREATE TABLE [\fBcreate_table\fR(7)]for details on the available parameters. Note that the table contentswill not be modified immediately by this command; depending on theparameter you may need to rewrite the table to get the desired effects.That can be done with CLUSTER [\fBcluster\fR(7)] or one of the forms of \fBALTERTABLE\fR that forces a table rewrite..sp.RS.B "Note:"While \fBCREATE TABLE\fR allows OIDS to be specifiedin the WITH (\fIstorage_parameter\fR) syntax,\fBALTER TABLE\fR does not treat OIDS as astorage parameter..RE.sp.TP\fBRESET ( \fIstorage_parameter\fB [, ... ] )\fRThis form resets one or more storage parameters to theirdefaults. As with SET, a table rewrite may beneeded to update the table entirely..TP\fBINHERIT \fIparent_table\fB\fRThis form adds the target table as a new child of the specified parenttable. Subsequently, queries against the parent will include recordsof the target table. To be added as a child, the target table mustalready contain all the same columns as the parent (it could haveadditional columns, too). The columns must have matching data types,and if they have NOT NULL constraints in the parentthen they must also have NOT NULL constraints in thechild.There must also be matching child-table constraints for allCHECK constraints of the parent. CurrentlyUNIQUE, PRIMARY KEY, andFOREIGN KEY constraints are not considered, butthis may change in the future..TP\fBNO INHERIT \fIparent_table\fB\fRThis form removes the target table from the list of children of thespecified parent table.Queries against the parent table will no longer include records drawnfrom the target table..TP\fBOWNER\fRThis form changes the owner of the table, sequence, or view to thespecified user..TP\fBSET TABLESPACE\fRThis form changes the table's tablespace to the specified tablespace andmoves the data file(s) associated with the table to the new tablespace.Indexes on the table, if any, are not moved; but they can be movedseparately with additional SET TABLESPACE commands.See also CREATE TABLESPACE [\fBcreate_tablespace\fR(7)]..TP\fBRENAME\fRThe RENAME forms change the name of a table(or an index, sequence, or view) or the name of an individual column ina table. There is no effect on the stored data..TP\fBSET SCHEMA\fRThis form moves the table into another schema. Associated indexes,constraints, and sequences owned by table columns are moved as well..PP.PPAll the actions except RENAME and SET SCHEMAcan be combined intoa list of multiple alterations to apply in parallel. For example, itis possible to add several columns and/or alter the type of severalcolumns in a single command. This is particularly useful with largetables, since only one pass over the table need be made..PPYou must own the table to use \fBALTER TABLE\fR.To change the schema of a table, you must also haveCREATE privilege on the new schema.To add the table as a new child of a parent table, you must own theparent table as well.To alter the owner, you must also be a direct or indirect member of the newowning role, and that role must have CREATE privilege onthe table's schema. (These restrictions enforce that altering the ownerdoesn't do anything you couldn't do by dropping and recreating the table.However, a superuser can alter ownership of any table anyway.).SH "PARAMETERS".TP\fB\fIname\fB\fRThe name (possibly schema-qualified) of an existing table toalter. If ONLY is specified, only that table isaltered. If ONLY is not specified, the table and allits descendant tables (if any) are updated. * can beappended to the table name to indicate that descendant tables areto be altered, but in the current version, this is the defaultbehavior. (In releases before 7.1, ONLY was thedefault behavior. The default can be altered by changing theconfiguration parameter sql_inheritance.).TP\fB\fIcolumn\fB\fRName of a new or existing column..TP\fB\fInew_column\fB\fRNew name for an existing column..TP\fB\fInew_name\fB\fRNew name for the table..TP\fB\fItype\fB\fRData type of the new column, or new data type for an existingcolumn..TP\fB\fItable_constraint\fB\fRNew table constraint for the table.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?