📄 alter_table.sgml
字号:
<!--$Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.62 2003/09/22 00:16:57 petere Exp $PostgreSQL documentation--><refentry id="SQL-ALTERTABLE"> <refmeta> <refentrytitle id="sql-altertable-title">ALTER TABLE</refentrytitle> <refmiscinfo>SQL - Language Statements</refmiscinfo> </refmeta> <refnamediv> <refname>ALTER TABLE</refname> <refpurpose>change the definition of a table</refpurpose> </refnamediv> <indexterm zone="sql-altertable"> <primary>ALTER TABLE</primary> </indexterm> <refsynopsisdiv><synopsis>ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ RESTRICT | CASCADE ]ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET DEFAULT <replaceable class="PARAMETER">expression</replaceable> | DROP DEFAULT }ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET | DROP } NOT NULLALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] SET WITHOUT OIDSALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable class="PARAMETER">new_column</replaceable>ALTER TABLE <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] ADD <replaceable class="PARAMETER">table_constraint</replaceable>ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ] DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]ALTER TABLE <replaceable class="PARAMETER">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>ALTER TABLE <replaceable class="PARAMETER">name</replaceable> CLUSTER ON <replaceable class="PARAMETER">index_name</replaceable></synopsis> </refsynopsisdiv> <refsect1> <title>Description</title> <para> <command>ALTER TABLE</command> changes the definition of an existing table. There are several subforms: <variablelist> <varlistentry> <term><literal>ADD COLUMN</literal></term> <listitem> <para> This form adds a new column to the table using the same syntax as <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">. </para> </listitem> </varlistentry> <varlistentry> <term><literal>DROP COLUMN</literal></term> <listitem> <para> 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 <literal>CASCADE</> if anything outside the table depends on the column, for example, foreign key references or views. </para> </listitem> </varlistentry> <varlistentry> <term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term> <listitem> <para> These forms set or remove the default value for a column. The default values only apply to subsequent <command>INSERT</command> 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 <command>INSERT</> statements on the view before the view's <literal>ON INSERT</literal> rule is applied. </para> </listitem> </varlistentry> <varlistentry> <term><literal>SET</literal>/<literal>DROP NOT NULL</literal></term> <listitem> <para> These forms change whether a column is marked to allow null values or to reject null values. You can only use <literal>SET NOT NULL</> when the column contains no null values. </para> </listitem> </varlistentry> <varlistentry> <term><literal>SET STATISTICS</literal></term> <listitem> <para> This form sets the per-column statistics-gathering target for subsequent <xref linkend="sql-analyze" endterm="sql-analyze-title"> 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. </para> </listitem> </varlistentry> <varlistentry> <term><literal>SET STORAGE</literal></term> <listitem> <para> 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. <literal>PLAIN</literal> must be used for fixed-length values such as <type>integer</type> and is inline, uncompressed. <literal>MAIN</literal> is for inline, compressible data. <literal>EXTERNAL</literal> is for external, uncompressed data, and <literal>EXTENDED</literal> is for external, compressed data. <literal>EXTENDED</literal> is the default for all data types that support it. The use of <literal>EXTERNAL</literal> will, for example, make substring operations on a <type>text</type> column faster, at the penalty of increased storage space. </para> </listitem> </varlistentry> <varlistentry> <term><literal>SET WITHOUT OIDS</literal></term> <listitem> <para> This form removes the <literal>oid</literal> 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 <literal>DROP COLUMN</literal> process. </para> </listitem> </varlistentry> <varlistentry> <term><literal>RENAME</literal></term> <listitem> <para> The <literal>RENAME</literal> 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. </para> </listitem> </varlistentry> <varlistentry> <term><literal>ADD <replaceable class="PARAMETER">table_constraint</replaceable></literal></term> <listitem> <para> This form adds a new constraint to a table using the same syntax as <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">. </para> </listitem> </varlistentry> <varlistentry> <term><literal>DROP CONSTRAINT</literal></term> <listitem> <para> 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. </para> </listitem> </varlistentry> <varlistentry> <term><literal>OWNER</literal></term> <listitem> <para> This form changes the owner of the table, index, sequence, or view to the specified user. </para> </listitem> </varlistentry> <varlistentry> <term><literal>CLUSTER</literal></term> <listitem> <para> This form marks a table for future <xref linkend="SQL-CLUSTER" endterm="sql-cluster-title"> operations. </para> </listitem> </varlistentry> </variablelist> </para> <para> You must own the table to use <command>ALTER TABLE</>; except for <command>ALTER TABLE OWNER</>, which may only be executed by a superuser. </para> </refsect1> <refsect1> <title>Parameters</title> <variablelist> <varlistentry> <term><replaceable class="PARAMETER">name</replaceable></term> <listitem> <para> The name (possibly schema-qualified) of an existing table to alter. If <literal>ONLY</> is specified, only that table is altered. If <literal>ONLY</> is not specified, the table and all its descendant tables (if any) are updated. <literal>*</> 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, <literal>ONLY</> was the default behavior. The default can be altered by changing the configuration parameter <varname>sql_inheritance</varname>.) </para> </listitem> </varlistentry> <varlistentry> <term><replaceable class="PARAMETER">column</replaceable></term> <listitem> <para> Name of a new or existing column. </para> </listitem> </varlistentry> <varlistentry> <term><replaceable class="PARAMETER">type</replaceable></term> <listitem> <para> Data type of the new column. </para> </listitem> </varlistentry> <varlistentry> <term><replaceable class="PARAMETER">new_column</replaceable></term> <listitem>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -