Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/dml.sgml`
4feb17cfaff16acb7b42b27f89e5cd3961fe62926e8ac7d10000000100000fa3
 DEFAULT VALUES;
</programlisting>
  </para>

  <para>
   You can insert multiple rows in a single command:
<programlisting>
INSERT INTO products (product_no, name, price) VALUES
    (1, 'Cheese', 9.99),
    (2, 'Bread', 1.99),
    (3, 'Milk', 2.99);
</programlisting>
  </para>

  <para>
   It is also possible to insert the result of a query (which might be no
   rows, one row, or many rows):
<programlisting>
INSERT INTO products (product_no, name, price)
  SELECT product_no, name, price FROM new_products
    WHERE release_date = 'today';
</programlisting>
   This provides the full power of the SQL query mechanism (<xref
   linkend="queries"/>) for computing the rows to be inserted.
  </para>

  <tip>
   <para>
    When inserting a lot of data at the same time, consider using
    the <xref linkend="sql-copy"/> command.
    It is not as flexible as the <xref linkend="sql-insert"/>
    command, but is more efficient. Refer
    to <xref linkend="populate"/> for more information on improving
    bulk loading performance.
   </para>
  </tip>
 </sect1>

 <sect1 id="dml-update">
  <title>Updating Data</title>

  <indexterm zone="dml-update">
   <primary>updating</primary>
  </indexterm>

  <indexterm zone="dml-update">
   <primary>UPDATE</primary>
  </indexterm>

  <para>
   The modification of data that is already in the database is
   referred to as updating.  You can update individual rows, all the
   rows in a table, or a subset of all rows.  Each column can be
   updated separately; the other columns are not affected.
  </para>

  <para>
   To update existing rows, use the <xref linkend="sql-update"/>
   command.  This requires
   three pieces of information:
   <orderedlist spacing="compact">
    <listitem>
     <para>The name of the table and column to update</para>
    </listitem>

    <listitem>
     <para>The new value of the column</para>
    </listitem>

    <listitem>
     <para>Which row(s) to update</para>
    </listitem>
   </orderedlist>
  </para>

  <para>
   Recall from <xref linkend="ddl"/> that SQL does not, in general,
   provide a unique identifier for rows.  Therefore it is not
   always possible to directly specify which row to update.
   Instead, you specify which conditions a row must meet in order to
   be updated.  Only if you have a primary key in the table (independent of
   whether you declared it or not) can you reliably address individual rows
   by choosing a condition that matches the primary key.
   Graphical database access tools rely on this fact to allow you to
   update rows individually.
  </para>

  <para>
   For example, this command updates all products that have a price of
   5 to have a price of 10:
<programlisting>
UPDATE products SET price = 10 WHERE price = 5;
</programlisting>
    This might cause zero, one, or many rows to be updated.  It is not
    an error to attempt an update that does not match any rows.
  </para>

  <para>
   Let's look at that command in detail. First is the key word
   <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

Title: Updating Data in Tables
Summary
This section explains how to update existing data in a database table using the UPDATE command, including specifying the table and column to update, the new value, and the conditions for which rows to update, with examples of updating individual rows, all rows, or a subset of rows.