File: dml-update.html

package info (click to toggle)
pgadmin3 1.4.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 29,796 kB
  • ctags: 10,758
  • sloc: cpp: 55,356; sh: 6,164; ansic: 1,520; makefile: 576; sql: 482; xml: 100; perl: 18
file content (81 lines) | stat: -rw-r--r-- 4,324 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>6.2.Updating Data</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="dml.html" title="Chapter6.Data Manipulation">
<link rel="prev" href="dml.html" title="Chapter6.Data Manipulation">
<link rel="next" href="dml-delete.html" title="6.3.Deleting Data">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="dml-update"></a>6.2.Updating Data</h2></div></div></div>
<a name="id579701"></a><a name="id579712"></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 perform an update, you need three pieces of information:
   </p>
<div class="orderedlist"><ol type="1" compact>
<li><p>The name of the table and column to update,</p></li>
<li><p>The new value of the column,</p></li>
<li><p>Which row(s) to update.</p></li>
</ol></div>
<p>
  </p>
<p>   Recall from <a href="ddl.html" title="Chapter5.Data Definition">Chapter5, <i>Data Definition</i></a> that SQL does not, in general,
   provide a unique identifier for rows.  Therefore it is not
   necessarily 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 (no matter
   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 may 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 may 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 equals 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 href="functions.html" title="Chapter9.Functions and Operators">Chapter9, <i>Functions and Operators</i></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></body>
</html>