alter_table.7
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 529 行 · 第 1/2 页
7
529 行
.TP\fB\fIconstraint_name\fB\fRName of an existing constraint to drop..TP\fBCASCADE\fRAutomatically drop objects that depend on the dropped columnor constraint (for example, views referencing the column)..TP\fBRESTRICT\fRRefuse to drop the column or constraint if there are any dependentobjects. This is the default behavior..TP\fB\fItrigger_name\fB\fRName of a single trigger to disable or enable..TP\fBALL\fRDisable or enable all triggers belonging to the table.(This requires superuser privilege if any of the triggers are forforeign key constraints.).TP\fBUSER\fRDisable or enable all triggers belonging to the table except forforeign key constraint triggers..TP\fB\fIindex_name\fB\fRThe index name on which the table should be marked for clustering..TP\fB\fIstorage_parameter\fB\fRThe name of a table storage parameter..TP\fB\fIvalue\fB\fRThe new value for a table storage parameter.This might be a number or a word depending on the parameter..TP\fB\fIparent_table\fB\fRA parent table to associate or de-associate with this table..TP\fB\fInew_owner\fB\fRThe user name of the new owner of the table..TP\fB\fInew_tablespace\fB\fRThe name of the tablespace to which the table will be moved..TP\fB\fInew_schema\fB\fRThe name of the schema to which the table will be moved..SH "NOTES".PPThe key word COLUMN is noise and can be omitted..PPWhen a column is added with ADD COLUMN, all existingrows in the table are initialized with the column's default value(NULL if no DEFAULT clause is specified)..PPAdding a column with a non-null default or changing the type of anexisting column will require the entire table to be rewritten. Thismay take a significant amount of time for a large table; and it willtemporarily require double the disk space..PPAdding a CHECK or NOT NULL constraint requiresscanning the table to verify that existing rows meet the constraint..PPThe main reason for providing the option to specify multiple changesin a single \fBALTER TABLE\fR is that multiple table scans orrewrites can thereby be combined into a single pass over the table..PPThe DROP COLUMN form does not physically removethe column, but simply makes it invisible to SQL operations. Subsequentinsert and update operations in the table will store a null value for thecolumn. Thus, dropping a column is quick but it will not immediatelyreduce the on-disk size of your table, as the space occupied by the dropped column is not reclaimed. The space will bereclaimed over time as existing rows are updated..PPThe fact that ALTER TYPE requires rewriting the whole tableis sometimes an advantage, because the rewriting process eliminatesany dead space in the table. For example, to reclaim the space occupiedby a dropped column immediately, the fastest way is.sp.nfALTER TABLE table ALTER COLUMN anycol TYPE anytype;.sp.fiwhere anycol is any remaining table column andanytype is the same type that column already has.This results in no semantically-visible change in the table,but the command forces rewriting, which gets rid of no-longer-usefuldata..PPThe USING option of ALTER TYPE can actuallyspecify any expression involving the old values of the row; that is, itcan refer to other columns as well as the one being converted. This allowsvery general conversions to be done with the ALTER TYPEsyntax. Because of this flexibility, the USINGexpression is not applied to the column's default value (if any); theresult might not be a constant expression as required for a default.This means that when there is no implicit or assignment cast from old tonew type, ALTER TYPE may fail to convert the default eventhough a USING clause is supplied. In such cases,drop the default with DROP DEFAULT, perform the ALTERTYPE, and then use SET DEFAULT to add a suitable newdefault. Similar considerations apply to indexes and constraints involvingthe column..PPIf a table has any descendant tables, it is not permitted to add,rename, or change the type of a column in the parent table without doingthe same to the descendants. That is, \fBALTER TABLE ONLY\fRwill be rejected. This ensures that the descendants always havecolumns matching the parent..PPA recursive DROP COLUMN operation will remove adescendant table's column only if the descendant does not inheritthat column from any other parents and never had an independentdefinition of the column. A nonrecursive DROPCOLUMN (i.e., \fBALTER TABLE ONLY ... DROPCOLUMN\fR) never removes any descendant columns, butinstead marks them as independently defined rather than inherited..PPThe TRIGGER, CLUSTER, OWNER,and TABLESPACE actions never recurse to descendant tables;that is, they always act as though ONLY were specified.Adding a constraint can recurse only for CHECK constraints..PPChanging any part of a system catalog table is not permitted..PPRefer to CREATE TABLE [\fBcreate_table\fR(7)] for a further description of validparameters. in the documentation has further information oninheritance..SH "EXAMPLES".PPTo add a column of type \fBvarchar\fR to a table:.sp.nfALTER TABLE distributors ADD COLUMN address varchar(30);.sp.fi.PPTo drop a column from a table:.sp.nfALTER TABLE distributors DROP COLUMN address RESTRICT;.sp.fi.PPTo change the types of two existing columns in one operation:.sp.nfALTER TABLE distributors ALTER COLUMN address TYPE varchar(80), ALTER COLUMN name TYPE varchar(100);.sp.fi.PPTo change an integer column containing UNIX timestamps to \fBtimestampwith time zone\fR via a USING clause:.sp.nfALTER TABLE foo ALTER COLUMN foo_timestamp TYPE timestamp with time zone USING timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';.sp.fi.PPThe same, when the column has a default expression that won't automaticallycast to the new data type:.sp.nfALTER TABLE foo ALTER COLUMN foo_timestamp DROP DEFAULT, ALTER COLUMN foo_timestamp TYPE timestamp with time zone USING timestamp with time zone 'epoch' + foo_timestamp * interval '1 second', ALTER COLUMN foo_timestamp SET DEFAULT now();.sp.fi.PPTo rename an existing column:.sp.nfALTER TABLE distributors RENAME COLUMN address TO city;.sp.fi.PPTo rename an existing table:.sp.nfALTER TABLE distributors RENAME TO suppliers;.sp.fi.PPTo add a not-null constraint to a column:.sp.nfALTER TABLE distributors ALTER COLUMN street SET NOT NULL;.sp.fiTo remove a not-null constraint from a column:.sp.nfALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;.sp.fi.PPTo add a check constraint to a table:.sp.nfALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);.sp.fi.PPTo remove a check constraint from a table and all its children:.sp.nfALTER TABLE distributors DROP CONSTRAINT zipchk;.sp.fi.PPTo add a foreign key constraint to a table:.sp.nfALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;.sp.fi.PPTo add a (multicolumn) unique constraint to a table:.sp.nfALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);.sp.fi.PPTo add an automatically named primary key constraint to a table, notingthat a table can only ever have one primary key:.sp.nfALTER TABLE distributors ADD PRIMARY KEY (dist_id);.sp.fi.PPTo move a table to a different tablespace:.sp.nfALTER TABLE distributors SET TABLESPACE fasttablespace;.sp.fi.PPTo move a table to a different schema:.sp.nfALTER TABLE myschema.distributors SET SCHEMA yourschema;.sp.fi.SH "COMPATIBILITY".PPThe ADD, DROP, and SET DEFAULTforms conform with the SQL standard. The other forms arePostgreSQL extensions of the SQL standard.Also, the ability to specify more than one manipulation in a single\fBALTER TABLE\fR command is an extension..PP\fBALTER TABLE DROP COLUMN\fR can be used to drop the onlycolumn of a table, leaving a zero-column table. This is anextension of SQL, which disallows zero-column tables.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?