<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >ALTER TABLE</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 7.4.1 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="ALTER SEQUENCE" HREF="sql-altersequence.html"><LINK REL="NEXT" TITLE="ALTER TRIGGER" HREF="sql-altertrigger.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META NAME="creation" CONTENT="2003-12-22T03:48:47"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 7.4.1 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-altersequence.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-altersequence.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-altertrigger.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-altertrigger.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-ALTERTABLE" ></A >ALTER TABLE</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN35231" ></A ><H2 >Name</H2 >ALTER TABLE -- change the definition of a table</DIV ><A NAME="AEN35234" ></A ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN35236" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >ALTER TABLE [ ONLY ] <VAR CLASS="REPLACEABLE" >name</VAR > [ * ] ADD [ COLUMN ] <VAR CLASS="REPLACEABLE" >column</VAR > <VAR CLASS="REPLACEABLE" >type</VAR > [ <VAR CLASS="REPLACEABLE" >column_constraint</VAR > [ ... ] ] ALTER TABLE [ ONLY ] <VAR CLASS="REPLACEABLE" >name</VAR > [ * ] DROP [ COLUMN ] <VAR CLASS="REPLACEABLE" >column</VAR > [ RESTRICT | CASCADE ] ALTER TABLE [ ONLY ] <VAR CLASS="REPLACEABLE" >name</VAR > [ * ] ALTER [ COLUMN ] <VAR CLASS="REPLACEABLE" >column</VAR > { SET DEFAULT <VAR CLASS="REPLACEABLE" >expression</VAR > | DROP DEFAULT } ALTER TABLE [ ONLY ] <VAR CLASS="REPLACEABLE" >name</VAR > [ * ] ALTER [ COLUMN ] <VAR CLASS="REPLACEABLE" >column</VAR > { SET | DROP } NOT NULL ALTER TABLE [ ONLY ] <VAR CLASS="REPLACEABLE" >name</VAR > [ * ] ALTER [ COLUMN ] <VAR CLASS="REPLACEABLE" >column</VAR > SET STATISTICS <VAR CLASS="REPLACEABLE" >integer</VAR > ALTER TABLE [ ONLY ] <VAR CLASS="REPLACEABLE" >name</VAR > [ * ] ALTER [ COLUMN ] <VAR CLASS="REPLACEABLE" >column</VAR > SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN } ALTER TABLE [ ONLY ] <VAR CLASS="REPLACEABLE" >name</VAR > [ * ] SET WITHOUT OIDS ALTER TABLE [ ONLY ] <VAR CLASS="REPLACEABLE" >name</VAR > [ * ] RENAME [ COLUMN ] <VAR CLASS="REPLACEABLE" >column</VAR > TO <VAR CLASS="REPLACEABLE" >new_column</VAR > ALTER TABLE <VAR CLASS="REPLACEABLE" >name</VAR > RENAME TO <VAR CLASS="REPLACEABLE" >new_name</VAR > ALTER TABLE [ ONLY ] <VAR CLASS="REPLACEABLE" >name</VAR > [ * ] ADD <VAR CLASS="REPLACEABLE" >table_constraint</VAR > ALTER TABLE [ ONLY ] <VAR CLASS="REPLACEABLE" >name</VAR > [ * ] DROP CONSTRAINT <VAR CLASS="REPLACEABLE" >constraint_name</VAR > [ RESTRICT | CASCADE ] ALTER TABLE <VAR CLASS="REPLACEABLE" >name</VAR > OWNER TO <VAR CLASS="REPLACEABLE" >new_owner</VAR > ALTER TABLE <VAR CLASS="REPLACEABLE" >name</VAR > CLUSTER ON <VAR CLASS="REPLACEABLE" >index_name</VAR ></PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN35268" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >ALTER TABLE</TT > changes the definition of an existing table. There are several subforms: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >ADD COLUMN</TT ></DT ><DD ><P > This form adds a new column to the table using the same syntax as <A HREF="sql-createtable.html" ><I >CREATE TABLE</I ></A >. </P ></DD ><DT ><TT CLASS="LITERAL" >DROP COLUMN</TT ></DT ><DD ><P > 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 <TT CLASS="LITERAL" >CASCADE</TT > if anything outside the table depends on the column, for example, foreign key references or views. </P ></DD ><DT ><TT CLASS="LITERAL" >SET</TT >/<TT CLASS="LITERAL" >DROP DEFAULT</TT ></DT ><DD ><P > These forms set or remove the default value for a column. The default values only apply to subsequent <TT CLASS="COMMAND" >INSERT</TT > 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 <TT CLASS="COMMAND" >INSERT</TT > statements on the view before the view's <TT CLASS="LITERAL" >ON INSERT</TT > rule is applied. </P ></DD ><DT ><TT CLASS="LITERAL" >SET</TT >/<TT CLASS="LITERAL" >DROP NOT NULL</TT ></DT ><DD ><P > These forms change whether a column is marked to allow null values or to reject null values. You can only use <TT CLASS="LITERAL" >SET NOT NULL</TT > when the column contains no null values. </P ></DD ><DT ><TT CLASS="LITERAL" >SET STATISTICS</TT ></DT ><DD ><P > This form sets the per-column statistics-gathering target for subsequent <A HREF="sql-analyze.html" ><I >ANALYZE</I ></A > 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. </P ></DD ><DT ><TT CLASS="LITERAL" >SET STORAGE</TT ></DT ><DD ><P > 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. <TT CLASS="LITERAL" >PLAIN</TT > must be used for fixed-length values such as <TT CLASS="TYPE" >integer</TT > and is inline, uncompressed. <TT CLASS="LITERAL" >MAIN</TT > is for inline, compressible data. <TT CLASS="LITERAL" >EXTERNAL</TT > is for external, uncompressed data, and <TT CLASS="LITERAL" >EXTENDED</TT > is for external, compressed data. <TT CLASS="LITERAL" >EXTENDED</TT > is the default for all data types that support it. The use of <TT CLASS="LITERAL" >EXTERNAL</TT > will, for example, make substring operations on a <TT CLASS="TYPE" >text</TT > column faster, at the penalty of increased storage space. </P ></DD ><DT ><TT CLASS="LITERAL" >SET WITHOUT OIDS</TT ></DT ><DD ><P > This form removes the <TT CLASS="LITERAL" >oid</TT > 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 <TT CLASS="LITERAL" >DROP COLUMN</TT > process. </P ></DD ><DT ><TT CLASS="LITERAL" >RENAME</TT ></DT ><DD ><P > The <TT CLASS="LITERAL" >RENAME</TT > 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. </P ></DD ><DT ><TT CLASS="LITERAL" >ADD <VAR CLASS="REPLACEABLE" >table_constraint</VAR ></TT ></DT ><DD ><P > This form adds a new constraint to a table using the same syntax as <A HREF="sql-createtable.html" ><I >CREATE TABLE</I ></A >. </P ></DD ><DT ><TT CLASS="LITERAL" >DROP CONSTRAINT</TT ></DT ><DD ><P > 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. </P ></DD ><DT ><TT CLASS="LITERAL" >OWNER</TT ></DT ><DD ><P > This form changes the owner of the table, index, sequence, or view to the specified user. </P ></DD ><DT ><TT CLASS="LITERAL" >CLUSTER</TT ></DT ><DD ><P > This form marks a table for future <A HREF="sql-cluster.html" ><I >CLUSTER</I ></A > operations. </P ></DD ></DL ></DIV ><P> </P ><P > You must own the table to use <TT CLASS="COMMAND" >ALTER TABLE</TT >; except for <TT CLASS="COMMAND" >ALTER TABLE OWNER</TT >, which may only be executed by a superuser. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN35359" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><VAR CLASS="REPLACEABLE" >name</VAR ></DT ><DD ><P > The name (possibly schema-qualified) of an existing table to alter. If <TT CLASS="LITERAL" >ONLY</TT > is specified, only that table is altered. If <TT CLASS="LITERAL" >ONLY</TT > is not specified, the table and all its descendant tables (if any) are updated. <TT CLASS="LITERAL" >*</TT > 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, <TT CLASS="LITERAL" >ONLY</TT > was the default behavior. The default can be altered by changing the configuration parameter <VAR CLASS="VARNAME" >sql_inheritance</VAR >.) </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >column</VAR ></DT ><DD ><P > Name of a new or existing column. </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >type</VAR ></DT ><DD ><P > Data type of the new column. </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >new_column</VAR ></DT ><DD ><P > New name for an existing column. </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >new_name</VAR ></DT ><DD ><P > New name for the table. </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >table_constraint</VAR ></DT ><DD ><P > New table constraint for the table. </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >constraint_name</VAR ></DT ><DD ><P > Name of an existing constraint to drop. </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >new_owner</VAR ></DT ><DD ><P > The user name of the new owner of the table. </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >index_name</VAR ></DT ><DD ><P > The index name on which the table should be marked for clustering. </P ></DD ><DT ><TT CLASS="LITERAL" >CASCADE</TT ></DT ><DD ><P > Automatically drop objects that depend on the dropped column or constraint (for example, views referencing the column). </P ></DD ><DT ><TT CLASS="LITERAL" >RESTRICT</TT ></DT ><DD ><P > Refuse to drop the column or constraint if there are any dependent objects. This is the default behavior. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN35422" ></A ><H2 >Notes</H2 ><P > The key word <TT CLASS="LITERAL" >COLUMN</TT > is noise and can be omitted. </P ><P > In the current implementation of <TT CLASS="LITERAL" >ADD COLUMN</TT >, default and <TT CLASS="LITERAL" >NOT NULL</TT > clauses for the new column are not supported. The new column always comes into being with all values null. You can use the <TT CLASS="LITERAL" >SET DEFAULT</TT > form of <TT CLASS="COMMAND" >ALTER TABLE</TT > to set the default afterward. (You may also want to update the already existing rows to the new default value, using <A HREF="sql-update.html" ><I >UPDATE</I ></A >.) If you want to mark the column non-null, use the <TT CLASS="LITERAL" >SET NOT NULL</TT > form after you've entered non-null values for the column in all rows. </P ><P > The <TT CLASS="LITERAL" >DROP COLUMN</TT > 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 <TT CLASS="COMMAND" >UPDATE</TT > of all rows and then vacuum, as in: </P><PRE CLASS="PROGRAMLISTING" >UPDATE table SET col = col; VACUUM FULL table;</PRE ><P> </P ><P > 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, <TT CLASS="COMMAND" >ALTER TABLE ONLY</TT > will be rejected. This ensures that the descendants always have columns matching the parent. </P ><P > A recursive <TT CLASS="LITERAL" >DROP COLUMN</TT > 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 <TT CLASS="LITERAL" >DROP COLUMN</TT > (i.e., <TT CLASS="COMMAND" >ALTER TABLE ONLY ... DROP COLUMN</TT >) never removes any descendant columns, but instead marks them as independently defined rather than inherited. </P ><P > Changing any part of a system catalog table is not permitted. </P ><P > Refer to <TT CLASS="COMMAND" >CREATE TABLE</TT > for a further description of valid parameters. <A HREF="ddl.html" >Chapter 5</A > has further information on inheritance. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN35447" ></A ><H2 >Examples</H2 ><P > To add a column of type <TT CLASS="TYPE" >varchar</TT > to a table: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors ADD COLUMN address varchar(30);</PRE ><P> </P ><P > To drop a column from a table: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors DROP COLUMN address RESTRICT;</PRE ><P> </P ><P > To rename an existing column: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors RENAME COLUMN address TO city;</PRE ><P> </P ><P > To rename an existing table: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors RENAME TO suppliers;</PRE ><P> </P ><P > To add a not-null constraint to a column: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;</PRE ><P> To remove a not-null constraint from a column: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;</PRE ><P> </P ><P > To add a check constraint to a table: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);</PRE ><P> </P ><P > To remove a check constraint from a table and all its children: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors DROP CONSTRAINT zipchk;</PRE ><P> </P ><P > To add a foreign key constraint to a table: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;</PRE ><P> </P ><P > To add a (multicolumn) unique constraint to a table: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);</PRE ><P> </P ><P > To add an automatically named primary key constraint to a table, noting that a table can only ever have one primary key: </P><PRE CLASS="PROGRAMLISTING" >ALTER TABLE distributors ADD PRIMARY KEY (dist_id);</PRE ><P> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN35471" ></A ><H2 >Compatibility</H2 ><P > The <TT CLASS="LITERAL" >ADD COLUMN</TT > form conforms with the SQL standard, with the exception that it does not support defaults and not-null constraints, as explained above. The <TT CLASS="LITERAL" >ALTER COLUMN</TT > form is in full conformance. </P ><P > The clauses to rename tables, columns, indexes, views, and sequences are <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > extensions of the SQL standard. </P ><P > <TT CLASS="COMMAND" >ALTER TABLE DROP COLUMN</TT > can be used to drop the only column of a table, leaving a zero-column table. This is an extension of SQL, which disallows zero-column tables. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-altersequence.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="sql-altertrigger.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >ALTER SEQUENCE</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >ALTER TRIGGER</TD ></TR ></TABLE ></DIV ></BODY ></HTML >