summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/dml-update.html
blob: 18a7ceb7ca8254272a5bd3b22bee001c8a7245a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>6.2. Updating Data</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="dml-insert.html" title="6.1. Inserting Data" /><link rel="next" href="dml-delete.html" title="6.3. Deleting Data" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">6.2. Updating Data</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="dml-insert.html" title="6.1. Inserting Data">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="dml.html" title="Chapter 6. Data Manipulation">Up</a></td><th width="60%" align="center">Chapter 6. Data Manipulation</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.2 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="dml-delete.html" title="6.3. Deleting Data">Next</a></td></tr></table><hr /></div><div class="sect1" id="DML-UPDATE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">6.2. Updating Data <a href="#DML-UPDATE" class="id_link">#</a></h2></div></div></div><a id="id-1.5.5.4.2" class="indexterm"></a><a id="id-1.5.5.4.3" class="indexterm"></a><p>
   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.
  </p><p>
   To update existing rows, use the <a class="xref" href="sql-update.html" title="UPDATE"><span class="refentrytitle">UPDATE</span></a>
   command.  This requires
   three pieces of information:
   </p><div class="orderedlist"><ol class="orderedlist compact" type="1"><li class="listitem"><p>The name of the table and column to update</p></li><li class="listitem"><p>The new value of the column</p></li><li class="listitem"><p>Which row(s) to update</p></li></ol></div><p>
  </p><p>
   Recall from <a class="xref" href="ddl.html" title="Chapter 5. Data Definition">Chapter 5</a> 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.
  </p><p>
   For example, this command updates all products that have a price of
   5 to have a price of 10:
</p><pre class="programlisting">
UPDATE products SET price = 10 WHERE price = 5;
</pre><p>
    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.
  </p><p>
   Let's look at that command in detail. First is the key word
   <code class="literal">UPDATE</code> 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 <code class="literal">SET</code> 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:
</p><pre class="programlisting">
UPDATE products SET price = price * 1.10;
</pre><p>
   As you see, the expression for the new value can refer to the existing
   value(s) in the row.  We also left out the <code class="literal">WHERE</code> 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
   <code class="literal">WHERE</code> condition are updated.  Note that the equals
   sign in the <code class="literal">SET</code> clause is an assignment while
   the one in the <code class="literal">WHERE</code> clause is a comparison, but
   this does not create any ambiguity.  Of course, the
   <code class="literal">WHERE</code> condition does
   not have to be an equality test.  Many other operators are
   available (see <a class="xref" href="functions.html" title="Chapter 9. Functions and Operators">Chapter 9</a>).  But the expression
   needs to evaluate to a Boolean result.
  </p><p>
   You can update more than one column in an
   <code class="command">UPDATE</code> command by listing more than one
   assignment in the <code class="literal">SET</code> clause.  For example:
</p><pre class="programlisting">
UPDATE mytable SET a = 5, b = 3, c = 1 WHERE a &gt; 0;
</pre><p>
  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="dml-insert.html" title="6.1. Inserting Data">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="dml.html" title="Chapter 6. Data Manipulation">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="dml-delete.html" title="6.3. Deleting Data">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6.1. Inserting Data </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.2 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 6.3. Deleting Data</td></tr></table></div></body></html>