1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-13 07:41:39 +03:00

Add ALTER TYPE ... ADD/DROP/ALTER/RENAME ATTRIBUTE

Like with tables, this also requires allowing the existence of
composite types with zero attributes.

reviewed by KaiGai Kohei
This commit is contained in:
Peter Eisentraut
2010-09-26 14:41:03 +03:00
parent 899beb7894
commit e440e12c56
15 changed files with 636 additions and 148 deletions

View File

@ -23,9 +23,17 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> <replaceable class="PARAMETER">action</replaceable> [, ... ]
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> RENAME ATTRIBUTE <replaceable class="PARAMETER">attribute_name</replaceable> TO <replaceable class="PARAMETER">new_attribute_name</replaceable>
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
<phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
ADD ATTRIBUTE <replaceable class="PARAMETER">attribute_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable>
DROP ATTRIBUTE [ IF EXISTS ] <replaceable class="PARAMETER">attribute_name</replaceable>
ALTER ATTRIBUTE <replaceable class="PARAMETER">attribute_name</replaceable> [ SET DATA ] TYPE <replaceable class="PARAMETER">data_type</replaceable>
</synopsis>
</refsynopsisdiv>
@ -34,6 +42,76 @@ ALTER TYPE <replaceable class="PARAMETER">name</replaceable> SET SCHEMA <replace
<para>
<command>ALTER TYPE</command> changes the definition of an existing type.
There are several subforms:
<variablelist>
<varlistentry>
<term><literal>ADD ATTRIBUTE</literal></term>
<listitem>
<para>
This form adds a new attribute to a composite type, using the same syntax as
<xref linkend="SQL-CREATETYPE">.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>DROP ATTRIBUTE [ IF EXISTS ]</literal></term>
<listitem>
<para>
This form drops an attribute from a composite type.
If <literal>IF EXISTS</literal> is specified and the attribute
does not exist, no error is thrown. In this case a notice
is issued instead.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>SET DATA TYPE</literal></term>
<listitem>
<para>
This form changes the type of an attribute of a composite type.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>OWNER</literal></term>
<listitem>
<para>
This form changes the owner of the type.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>RENAME</literal></term>
<listitem>
<para>
This form changes the name of the type or the name of an
individual attribute of a composite type.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>SET SCHEMA</literal></term>
<listitem>
<para>
This form moves the type into another schema.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
The <literal>ADD ATTRIBUTE</literal>, <literal>DROP
ATTRIBUTE</literal>, and <literal>ALTER ATTRIBUTE</literal> actions
can be combined into a list of multiple alterations to apply in
parallel. For example, it is possible to add several attributes
and/or alter the type of several attributes in a single command.
</para>
<para>
@ -90,6 +168,34 @@ ALTER TYPE <replaceable class="PARAMETER">name</replaceable> SET SCHEMA <replace
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">attribute_name</replaceable></term>
<listitem>
<para>
The name of the attribute to add, alter, or drop.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">new_attribute_name</replaceable></term>
<listitem>
<para>
The new name of the attribute begin renamed.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">data_type</replaceable></term>
<listitem>
<para>
The data type of the attribute to add, or the new type of the
attribute to alter.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
@ -117,16 +223,32 @@ ALTER TYPE email OWNER TO joe;
to <literal>customers</literal>:
<programlisting>
ALTER TYPE email SET SCHEMA customers;
</programlisting>
</para>
<para>
To add a new attribute to a type:
<programlisting>
ALTER TYPE compfoo ADD ATTRIBUTE f3 int;
</programlisting>
</para>
</refsect1>
<refsect1>
<title>Compatibility</title>
<para>
There is no <command>ALTER TYPE</command> statement in the SQL
standard.
The variants to add and drop attributes are part of the SQL
standard; the other variants are PostgreSQL extensions.
</para>
</refsect1>
<refsect1 id="SQL-ALTERTYPE-see-also">
<title>See Also</title>
<simplelist type="inline">
<member><xref linkend="sql-createtype"></member>
<member><xref linkend="sql-droptype"></member>
</simplelist>
</refsect1>
</refentry>

View File

@ -22,7 +22,7 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
CREATE TYPE <replaceable class="parameter">name</replaceable> AS
( <replaceable class="PARAMETER">attribute_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [, ... ] )
( [ <replaceable class="PARAMETER">attribute_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [, ... ] ] )
CREATE TYPE <replaceable class="parameter">name</replaceable> AS ENUM
( [ '<replaceable class="parameter">label</replaceable>' [, ... ] ] )
@ -768,10 +768,18 @@ CREATE TABLE big_objs (
<title>Compatibility</title>
<para>
This <command>CREATE TYPE</command> command is a
<productname>PostgreSQL</productname> extension. There is a
<command>CREATE TYPE</command> statement in the <acronym>SQL</> standard
that is rather different in detail.
The first form of the <command>CREATE TYPE</command> command, which
creates a composite type, conforms to the <acronym>SQL</> standard.
The other forms are <productname>PostgreSQL</productname>
extensions. The <command>CREATE TYPE</command> statement in
the <acronym>SQL</> standard also defines other forms that are not
implemented in <productname>PostgreSQL</>.
</para>
<para>
The ability to create a composite type with zero attributes is
a <productname>PostgreSQL</productname>-specific deviation from the
standard (analogous to <command>CREATE TABLE</command>).
</para>
</refsect1>
@ -779,10 +787,10 @@ CREATE TABLE big_objs (
<title>See Also</title>
<simplelist type="inline">
<member><xref linkend="sql-createfunction"></member>
<member><xref linkend="sql-droptype"></member>
<member><xref linkend="sql-altertype"></member>
<member><xref linkend="sql-createdomain"></member>
<member><xref linkend="sql-createfunction"></member>
<member><xref linkend="sql-droptype"></member>
</simplelist>
</refsect1>

View File

@ -97,7 +97,7 @@ DROP TYPE box;
This command is similar to the corresponding command in the SQL
standard, apart from the <literal>IF EXISTS</>
option, which is a <productname>PostgreSQL</> extension.
But note that the <command>CREATE TYPE</command> command
But note that much of the <command>CREATE TYPE</command> command
and the data type extension mechanisms in
<productname>PostgreSQL</productname> differ from the SQL standard.
</para>
@ -107,8 +107,8 @@ DROP TYPE box;
<title>See Also</title>
<simplelist type="inline">
<member><xref linkend="sql-createtype"></member>
<member><xref linkend="sql-altertype"></member>
<member><xref linkend="sql-createtype"></member>
</simplelist>
</refsect1>