Home Explore Blog CI



postgresql

22th chunk of `doc/src/sgml/ref/create_table.sgml`
d82eeb04472791a93e7bcd2965e9b7121e63e478bf602a820000000100000fac
 these cases from arising.)
     </para>

     <para>
      In addition, when the data in the referenced columns is changed,
      certain actions are performed on the data in this table's
      columns.  The <literal>ON DELETE</literal> clause specifies the
      action to perform when a referenced row in the referenced table is
      being deleted.  Likewise, the <literal>ON UPDATE</literal>
      clause specifies the action to perform when a referenced column
      in the referenced table is being updated to a new value. If the
      row is updated, but the referenced column is not actually
      changed, no action is done. Referential actions are executed as part of
      the data changing command, even if the constraint is deferred.  There
      are the following possible actions for each clause:

      <variablelist>
       <varlistentry id="sql-createtable-parms-references-refact-no-action">
        <term><literal>NO ACTION</literal></term>
        <listitem>
         <para>
          Produce an error if the deletion or update
          would create a foreign key constraint violation.
          If the constraint is deferred, this
          error will be produced at constraint check time if there still
          exist any referencing rows.  This is the default action.
         </para>
        </listitem>
       </varlistentry>

       <varlistentry id="sql-createtable-parms-references-refact-restrict">
        <term><literal>RESTRICT</literal></term>
        <listitem>
         <para>
          Produce an error if a row to be deleted or updated matches a row in
          the referencing table.  This prevents the action even if the state
          after the action would not violate the foreign key constraint.  In
          particular, it prevents updates of referenced rows to values that
          are distinct but compare as equal.  (But it does not prevent
          <quote>no-op</quote> updates that update a column to the same
          value.)
         </para>

         <para>
          In a temporal foreign key, this option is not supported.
         </para>
        </listitem>
       </varlistentry>

       <varlistentry id="sql-createtable-parms-references-refact-cascade">
        <term><literal>CASCADE</literal></term>
        <listitem>
         <para>
          Delete any rows referencing the deleted row, or update the
          values of the referencing column(s) to the new values of the
          referenced columns, respectively.
         </para>

         <para>
          In a temporal foreign key, this option is not supported.
         </para>
        </listitem>
       </varlistentry>

       <varlistentry id="sql-createtable-parms-references-refact-set-null">
        <term><literal>SET NULL [ ( <replaceable>column_name</replaceable> [, ... ] ) ]</literal></term>
        <listitem>
         <para>
          Set all of the referencing columns, or a specified subset of the
          referencing columns, to null. A subset of columns can only be
          specified for <literal>ON DELETE</literal> actions.
         </para>

         <para>
          In a temporal foreign key, this option is not supported.
         </para>
        </listitem>
       </varlistentry>

       <varlistentry id="sql-createtable-parms-references-refact-set-default">
        <term><literal>SET DEFAULT [ ( <replaceable>column_name</replaceable> [, ... ] ) ]</literal></term>
        <listitem>
         <para>
          Set all of the referencing columns, or a specified subset of the
          referencing columns, to their default values. A subset of columns
          can only be specified for <literal>ON DELETE</literal> actions.
          (There must be a row in the referenced table matching the default
          values, if they are not null, or the operation will fail.)
         </para>

         <para>
          In a temporal foreign key, this option is not supported.
         </para>
        </listitem>
       </varlistentry>
      </variablelist>

Title: Referential Actions: NO ACTION, RESTRICT, CASCADE, SET NULL, and SET DEFAULT
Summary
This section details the possible actions that can be specified in the ON DELETE and ON UPDATE clauses of a foreign key constraint. It describes NO ACTION (producing an error if a violation occurs), RESTRICT (producing an error if a row to be deleted or updated matches a row in the referencing table, unsupported in temporal foreign keys), CASCADE (deleting referencing rows or updating referencing columns to new values, also unsupported in temporal foreign keys), SET NULL (setting referencing columns to null, also unsupported in temporal foreign keys), and SET DEFAULT (setting referencing columns to default values, which must have a matching row in the referenced table, and also unsupported in temporal foreign keys).