Home Explore Blog CI



postgresql

11th chunk of `doc/src/sgml/ref/alter_table.sgml`
15a713134f80b88233d9a93d7be3a0b0172e0c7d804a026e0000000100000fa8
   command.
     </para>

     <para>
      If <literal>PRIMARY KEY</literal> is specified, and the index's columns are not
      already marked <literal>NOT NULL</literal>, then this command will attempt to
      do <literal>ALTER COLUMN SET NOT NULL</literal> against each such column.
      That requires a full table scan to verify the column(s) contain no
      nulls.  In all other cases, this is a fast operation.
     </para>

     <para>
      If a constraint name is provided then the index will be renamed to match
      the constraint name.  Otherwise the constraint will be named the same as
      the index.
     </para>

     <para>
      After this command is executed, the index is <quote>owned</quote> by the
      constraint, in the same way as if the index had been built by
      a regular <literal>ADD PRIMARY KEY</literal> or <literal>ADD UNIQUE</literal>
      command.  In particular, dropping the constraint will make the index
      disappear too.
     </para>

     <para>
      This form is not currently supported on partitioned tables.
     </para>

     <note>
      <para>
       Adding a constraint using an existing index can be helpful in
       situations where a new constraint needs to be added without blocking
       table updates for a long time.  To do that, create the index using
       <command>CREATE UNIQUE INDEX CONCURRENTLY</command>, and then convert it to a
       constraint using this syntax.  See the example below.
      </para>
     </note>
    </listitem>
   </varlistentry>

   <varlistentry id="sql-altertable-desc-alter-constraint">
    <term><literal>ALTER CONSTRAINT</literal></term>
    <listitem>
     <para>
      This form alters the attributes of a constraint that was previously
      created. Currently only foreign key constraints may be altered in
      this fashion, but see below.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="sql-altertable-desc-alter-constraint-inherit">
    <term><literal>ALTER CONSTRAINT ... INHERIT</literal></term>
    <term><literal>ALTER CONSTRAINT ... NO INHERIT</literal></term>
    <listitem>
     <para>
      These forms modify a inheritable constraint so that it becomes not
      inheritable, or vice-versa. Only not-null constraints may be altered
      in this fashion at present.
      In addition to changing the inheritability status of the constraint,
      in the case where a non-inheritable constraint is being marked
      inheritable, if the table has children, an equivalent constraint
      will be added to them. If marking an inheritable constraint as
      non-inheritable on a table with children, then the corresponding
      constraint on children will be marked as no longer inherited,
      but not removed.
      </para>
     </listitem>
    </varlistentry>

   <varlistentry id="sql-altertable-desc-validate-constraint">
    <term><literal>VALIDATE CONSTRAINT</literal></term>
    <listitem>
     <para>
      This form validates a foreign key, check, or not-null constraint that was
      previously created as <literal>NOT VALID</literal>, by scanning the
      table to ensure there are no rows for which the constraint is not
      satisfied.  If the constraint is not enforced, an error is thrown.
      Nothing happens if the constraint is already marked valid.
      (See <xref linkend="sql-altertable-notes"/> below for an explanation
      of the usefulness of this command.)
     </para>
     <para>
      This command acquires a <literal>SHARE UPDATE EXCLUSIVE</literal> lock.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="sql-altertable-desc-drop-constraint">
    <term><literal>DROP CONSTRAINT [ IF EXISTS ]</literal></term>
    <listitem>
     <para>
      This form drops the specified constraint on a table, along with
      any index underlying the constraint.
      If <literal>IF EXISTS</literal> is specified and the constraint
      does not exist, no error is thrown. In this case a notice is issued instead.

Title: ALTER TABLE: Constraint Modifications - Using Index, Altering, Inheritance, Validation, and Dropping
Summary
This section details various ALTER TABLE operations related to constraints. It covers adding constraints based on existing indexes (ADD CONSTRAINT USING INDEX), highlighting restrictions on index types and behavior, especially regarding PRIMARY KEY and NOT NULL constraints. It also describes altering existing constraints (ALTER CONSTRAINT), modifying inheritance properties of constraints (ALTER CONSTRAINT ... INHERIT/NO INHERIT) specifically for not-null constraints and their impact on child tables, validating constraints created with NOT VALID (VALIDATE CONSTRAINT), and removing constraints (DROP CONSTRAINT) including the IF EXISTS option.