Home Explore Blog CI



postgresql

22th chunk of `doc/src/sgml/ddl.sgml`
452e1d58a720d44a18f6daaaac39ac94a0e6699dc3841b5e0000000100000fa2
 product_no SET NOT NULL;
</programlisting>
    This command silently does nothing if the column already has a
    not-null constraint.
   </para>

   <para>
    The constraint will be checked immediately, so the table data must
    satisfy the constraint before it can be added.
   </para>
  </sect2>

  <sect2 id="ddl-alter-removing-a-constraint">
   <title>Removing a Constraint</title>

   <indexterm>
    <primary>constraint</primary>
    <secondary>removing</secondary>
   </indexterm>

   <para>
    To remove a constraint you need to know its name.  If you gave it
    a name then that's easy.  Otherwise the system assigned a
    generated name, which you need to find out.  The
    <application>psql</application> command <literal>\d
    <replaceable>tablename</replaceable></literal> can be helpful
    here; other interfaces might also provide a way to inspect table
    details.  Then the command is:
<programlisting>
ALTER TABLE products DROP CONSTRAINT some_name;
</programlisting>
   </para>

   <para>
    As with dropping a column, you need to add <literal>CASCADE</literal> if you
    want to drop a constraint that something else depends on.  An example
    is that a foreign key constraint depends on a unique or primary key
    constraint on the referenced column(s).
   </para>

   <para>
    Simplified syntax is available to drop a not-null constraint:
<programlisting>
ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;
</programlisting>
    This mirrors the <literal>SET NOT NULL</literal> syntax for adding a
    not-null constraint.  This command will silently do nothing if the column
    does not have a not-null constraint.  (Recall that a column can have at
    most one not-null constraint, so it is never ambiguous which constraint
    this command acts on.)
   </para>
  </sect2>

  <sect2 id="ddl-alter-column-default">
   <title>Changing a Column's Default Value</title>

   <indexterm>
    <primary>default value</primary>
    <secondary>changing</secondary>
   </indexterm>

   <para>
    To set a new default for a column, use a command like:
<programlisting>
ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77;
</programlisting>
    Note that this doesn't affect any existing rows in the table, it
    just changes the default for future <command>INSERT</command> commands.
   </para>

   <para>
    To remove any default value, use:
<programlisting>
ALTER TABLE products ALTER COLUMN price DROP DEFAULT;
</programlisting>
    This is effectively the same as setting the default to null.
    As a consequence, it is not an error
    to drop a default where one hadn't been defined, because the
    default is implicitly the null value.
   </para>
  </sect2>

  <sect2 id="ddl-alter-column-type">
   <title>Changing a Column's Data Type</title>

   <indexterm>
    <primary>column data type</primary>
    <secondary>changing</secondary>
   </indexterm>

   <para>
    To convert a column to a different data type, use a command like:
<programlisting>
ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);
</programlisting>
    This will succeed only if each existing entry in the column can be
    converted to the new type by an implicit cast.  If a more complex
    conversion is needed, you can add a <literal>USING</literal> clause that
    specifies how to compute the new values from the old.
   </para>

   <para>
    <productname>PostgreSQL</productname> will attempt to convert the column's
    default value (if any) to the new type, as well as any constraints
    that involve the column.  But these conversions might fail, or might
    produce surprising results.  It's often best to drop any constraints
    on the column before altering its type, and then add back suitably
    modified constraints afterwards.
   </para>
  </sect2>

  <sect2 id="ddl-alter-renaming-column">
   <title>Renaming a Column</title>

   <indexterm>
    <primary>column</primary>
    <secondary>renaming</secondary>
   </indexterm>

   <para>
    To

Title: Modifying Tables: Removing Constraints, Changing Defaults, and Changing Data Types
Summary
This section details how to remove constraints, change column default values, and alter column data types in PostgreSQL using ALTER TABLE. To remove a constraint, its name must be known, or you can use \d tablename to find it. Use CASCADE to drop constraints other objects depend on. Simplified syntax exists for dropping NOT NULL constraints. To set or remove a default value for a column, use ALTER COLUMN SET DEFAULT or DROP DEFAULT. To change a column's data type, use ALTER COLUMN TYPE. Conversions are attempted for existing data and default values, but it's often best to drop and recreate constraints before altering the type.