Home Explore Blog CI



postgresql

3rd chunk of `doc/src/sgml/dml.sgml`
016ca8974dd0ebace4916f41bd949ff3c9198b5b5c96e2870000000100000fa3
 <literal>UPDATE</literal> followed by the table name.  As usual,
   the table name can be schema-qualified, otherwise it is looked up
   in the path.  Next is the key word <literal>SET</literal> followed
   by the column name, an equal sign, and the new column value.  The
   new column value can be any scalar expression, not just a constant.
   For example, if you want to raise the price of all products by 10%
   you could use:
<programlisting>
UPDATE products SET price = price * 1.10;
</programlisting>
   As you see, the expression for the new value can refer to the existing
   value(s) in the row.  We also left out the <literal>WHERE</literal> clause.
   If it is omitted, it means that all rows in the table are updated.
   If it is present, only those rows that match the
   <literal>WHERE</literal> condition are updated.  Note that the equals
   sign in the <literal>SET</literal> clause is an assignment while
   the one in the <literal>WHERE</literal> clause is a comparison, but
   this does not create any ambiguity.  Of course, the
   <literal>WHERE</literal> condition does
   not have to be an equality test.  Many other operators are
   available (see <xref linkend="functions"/>).  But the expression
   needs to evaluate to a Boolean result.
  </para>

  <para>
   You can update more than one column in an
   <command>UPDATE</command> command by listing more than one
   assignment in the <literal>SET</literal> clause.  For example:
<programlisting>
UPDATE mytable SET a = 5, b = 3, c = 1 WHERE a &gt; 0;
</programlisting>
  </para>
 </sect1>

 <sect1 id="dml-delete">
  <title>Deleting Data</title>

  <indexterm zone="dml-delete">
   <primary>deleting</primary>
  </indexterm>

  <indexterm zone="dml-delete">
   <primary>DELETE</primary>
  </indexterm>

  <para>
   So far we have explained how to add data to tables and how to
   change data.  What remains is to discuss how to remove data that is
   no longer needed.  Just as adding data is only possible in whole
   rows, you can only remove entire rows from a table.  In the
   previous section we explained that SQL does not provide a way to
   directly address individual rows.  Therefore, removing rows can
   only be done by specifying conditions that the rows to be removed
   have to match.  If you have a primary key in the table then you can
   specify the exact row.  But you can also remove groups of rows
   matching a condition, or you can remove all rows in the table at
   once.
  </para>

  <para>
   You use the <xref linkend="sql-delete"/>
   command to remove rows; the syntax is very similar to the
   <xref linkend="sql-update"/> command.  For instance, to remove all
   rows from the products table that have a price of 10, use:
<programlisting>
DELETE FROM products WHERE price = 10;
</programlisting>
  </para>

  <para>
   If you simply write:
<programlisting>
DELETE FROM products;
</programlisting>
   then all rows in the table will be deleted!  Caveat programmer.
  </para>
 </sect1>

 <sect1 id="dml-returning">
  <title>Returning Data from Modified Rows</title>

  <indexterm zone="dml-returning">
   <primary>RETURNING</primary>
  </indexterm>

  <indexterm zone="dml-returning">
   <primary>INSERT</primary>
   <secondary>RETURNING</secondary>
  </indexterm>

  <indexterm zone="dml-returning">
   <primary>UPDATE</primary>
   <secondary>RETURNING</secondary>
  </indexterm>

  <indexterm zone="dml-returning">
   <primary>DELETE</primary>
   <secondary>RETURNING</secondary>
  </indexterm>

  <indexterm zone="dml-returning">
   <primary>MERGE</primary>
   <secondary>RETURNING</secondary>
  </indexterm>

  <para>
   Sometimes it is useful to obtain data from modified rows while they are
   being manipulated.  The <command>INSERT</command>, <command>UPDATE</command>,
   <command>DELETE</command>, and <command>MERGE</command> commands all have an
   optional <literal>RETURNING</literal> clause that supports this.  Use
   of <literal>RETURNING</literal> avoids performing an extra

Title: Modifying and Deleting Data
Summary
This section explains how to update and delete data in a database table using the UPDATE and DELETE commands, including updating multiple columns, removing rows based on conditions, and using the RETURNING clause to obtain data from modified rows during INSERT, UPDATE, DELETE, and MERGE operations.