~ubuntu-branches/ubuntu/precise/postgresql-9.1/precise-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/html/dml-update.html

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
2
<HTML
 
3
><HEAD
 
4
><TITLE
 
5
>Updating Data</TITLE
 
6
><META
 
7
NAME="GENERATOR"
 
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
 
9
REV="MADE"
 
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
 
11
REL="HOME"
 
12
TITLE="PostgreSQL 9.1beta1 Documentation"
 
13
HREF="index.html"><LINK
 
14
REL="UP"
 
15
TITLE="Data Manipulation"
 
16
HREF="dml.html"><LINK
 
17
REL="PREVIOUS"
 
18
TITLE="Inserting Data"
 
19
HREF="dml-insert.html"><LINK
 
20
REL="NEXT"
 
21
TITLE="Deleting Data"
 
22
HREF="dml-delete.html"><LINK
 
23
REL="STYLESHEET"
 
24
TYPE="text/css"
 
25
HREF="stylesheet.css"><META
 
26
HTTP-EQUIV="Content-Type"
 
27
CONTENT="text/html; charset=ISO-8859-1"><META
 
28
NAME="creation"
 
29
CONTENT="2011-04-27T21:20:33"></HEAD
 
30
><BODY
 
31
CLASS="SECT1"
 
32
><DIV
 
33
CLASS="NAVHEADER"
 
34
><TABLE
 
35
SUMMARY="Header navigation table"
 
36
WIDTH="100%"
 
37
BORDER="0"
 
38
CELLPADDING="0"
 
39
CELLSPACING="0"
 
40
><TR
 
41
><TH
 
42
COLSPAN="5"
 
43
ALIGN="center"
 
44
VALIGN="bottom"
 
45
><A
 
46
HREF="index.html"
 
47
>PostgreSQL 9.1beta1 Documentation</A
 
48
></TH
 
49
></TR
 
50
><TR
 
51
><TD
 
52
WIDTH="10%"
 
53
ALIGN="left"
 
54
VALIGN="top"
 
55
><A
 
56
TITLE="Inserting Data"
 
57
HREF="dml-insert.html"
 
58
ACCESSKEY="P"
 
59
>Prev</A
 
60
></TD
 
61
><TD
 
62
WIDTH="10%"
 
63
ALIGN="left"
 
64
VALIGN="top"
 
65
><A
 
66
TITLE="Data Manipulation"
 
67
HREF="dml.html"
 
68
>Fast Backward</A
 
69
></TD
 
70
><TD
 
71
WIDTH="60%"
 
72
ALIGN="center"
 
73
VALIGN="bottom"
 
74
>Chapter 6. Data Manipulation</TD
 
75
><TD
 
76
WIDTH="10%"
 
77
ALIGN="right"
 
78
VALIGN="top"
 
79
><A
 
80
TITLE="Data Manipulation"
 
81
HREF="dml.html"
 
82
>Fast Forward</A
 
83
></TD
 
84
><TD
 
85
WIDTH="10%"
 
86
ALIGN="right"
 
87
VALIGN="top"
 
88
><A
 
89
TITLE="Deleting Data"
 
90
HREF="dml-delete.html"
 
91
ACCESSKEY="N"
 
92
>Next</A
 
93
></TD
 
94
></TR
 
95
></TABLE
 
96
><HR
 
97
ALIGN="LEFT"
 
98
WIDTH="100%"></DIV
 
99
><DIV
 
100
CLASS="SECT1"
 
101
><H1
 
102
CLASS="SECT1"
 
103
><A
 
104
NAME="DML-UPDATE"
 
105
>6.2. Updating Data</A
 
106
></H1
 
107
><P
 
108
>   The modification of data that is already in the database is
 
109
   referred to as updating.  You can update individual rows, all the
 
110
   rows in a table, or a subset of all rows.  Each column can be
 
111
   updated separately; the other columns are not affected.
 
112
  </P
 
113
><P
 
114
>   To update existing rows, use the <A
 
115
HREF="sql-update.html"
 
116
>UPDATE</A
 
117
>
 
118
   command.  This requires
 
119
   three pieces of information:
 
120
   <P
 
121
></P
 
122
></P><OL
 
123
COMPACT="COMPACT"
 
124
TYPE="1"
 
125
><LI
 
126
><P
 
127
>The name of the table and column to update</P
 
128
></LI
 
129
><LI
 
130
><P
 
131
>The new value of the column</P
 
132
></LI
 
133
><LI
 
134
><P
 
135
>Which row(s) to update</P
 
136
></LI
 
137
></OL
 
138
><P>
 
139
  </P
 
140
><P
 
141
>   Recall from <A
 
142
HREF="ddl.html"
 
143
>Chapter 5</A
 
144
> that SQL does not, in general,
 
145
   provide a unique identifier for rows.  Therefore it is not
 
146
   always possible to directly specify which row to update.
 
147
   Instead, you specify which conditions a row must meet in order to
 
148
   be updated.  Only if you have a primary key in the table (independent of
 
149
   whether you declared it or not) can you reliably address individual rows
 
150
   by choosing a condition that matches the primary key.
 
151
   Graphical database access tools rely on this fact to allow you to
 
152
   update rows individually.
 
153
  </P
 
154
><P
 
155
>   For example, this command updates all products that have a price of
 
156
   5 to have a price of 10:
 
157
</P><PRE
 
158
CLASS="PROGRAMLISTING"
 
159
>UPDATE products SET price = 10 WHERE price = 5;</PRE
 
160
><P>
 
161
    This might cause zero, one, or many rows to be updated.  It is not
 
162
    an error to attempt an update that does not match any rows.
 
163
  </P
 
164
><P
 
165
>   Let's look at that command in detail. First is the key word
 
166
   <TT
 
167
CLASS="LITERAL"
 
168
>UPDATE</TT
 
169
> followed by the table name.  As usual,
 
170
   the table name can be schema-qualified, otherwise it is looked up
 
171
   in the path.  Next is the key word <TT
 
172
CLASS="LITERAL"
 
173
>SET</TT
 
174
> followed
 
175
   by the column name, an equal sign, and the new column value.  The
 
176
   new column value can be any scalar expression, not just a constant.
 
177
   For example, if you want to raise the price of all products by 10%
 
178
   you could use:
 
179
</P><PRE
 
180
CLASS="PROGRAMLISTING"
 
181
>UPDATE products SET price = price * 1.10;</PRE
 
182
><P>
 
183
   As you see, the expression for the new value can refer to the existing
 
184
   value(s) in the row.  We also left out the <TT
 
185
CLASS="LITERAL"
 
186
>WHERE</TT
 
187
> clause.
 
188
   If it is omitted, it means that all rows in the table are updated.
 
189
   If it is present, only those rows that match the
 
190
   <TT
 
191
CLASS="LITERAL"
 
192
>WHERE</TT
 
193
> condition are updated.  Note that the equals
 
194
   sign in the <TT
 
195
CLASS="LITERAL"
 
196
>SET</TT
 
197
> clause is an assignment while
 
198
   the one in the <TT
 
199
CLASS="LITERAL"
 
200
>WHERE</TT
 
201
> clause is a comparison, but
 
202
   this does not create any ambiguity.  Of course, the
 
203
   <TT
 
204
CLASS="LITERAL"
 
205
>WHERE</TT
 
206
> condition does
 
207
   not have to be an equality test.  Many other operators are
 
208
   available (see <A
 
209
HREF="functions.html"
 
210
>Chapter 9</A
 
211
>).  But the expression
 
212
   needs to evaluate to a Boolean result.
 
213
  </P
 
214
><P
 
215
>   You can update more than one column in an
 
216
   <TT
 
217
CLASS="COMMAND"
 
218
>UPDATE</TT
 
219
> command by listing more than one
 
220
   assignment in the <TT
 
221
CLASS="LITERAL"
 
222
>SET</TT
 
223
> clause.  For example:
 
224
</P><PRE
 
225
CLASS="PROGRAMLISTING"
 
226
>UPDATE mytable SET a = 5, b = 3, c = 1 WHERE a &gt; 0;</PRE
 
227
><P>
 
228
  </P
 
229
></DIV
 
230
><DIV
 
231
CLASS="NAVFOOTER"
 
232
><HR
 
233
ALIGN="LEFT"
 
234
WIDTH="100%"><TABLE
 
235
SUMMARY="Footer navigation table"
 
236
WIDTH="100%"
 
237
BORDER="0"
 
238
CELLPADDING="0"
 
239
CELLSPACING="0"
 
240
><TR
 
241
><TD
 
242
WIDTH="33%"
 
243
ALIGN="left"
 
244
VALIGN="top"
 
245
><A
 
246
HREF="dml-insert.html"
 
247
ACCESSKEY="P"
 
248
>Prev</A
 
249
></TD
 
250
><TD
 
251
WIDTH="34%"
 
252
ALIGN="center"
 
253
VALIGN="top"
 
254
><A
 
255
HREF="index.html"
 
256
ACCESSKEY="H"
 
257
>Home</A
 
258
></TD
 
259
><TD
 
260
WIDTH="33%"
 
261
ALIGN="right"
 
262
VALIGN="top"
 
263
><A
 
264
HREF="dml-delete.html"
 
265
ACCESSKEY="N"
 
266
>Next</A
 
267
></TD
 
268
></TR
 
269
><TR
 
270
><TD
 
271
WIDTH="33%"
 
272
ALIGN="left"
 
273
VALIGN="top"
 
274
>Inserting Data</TD
 
275
><TD
 
276
WIDTH="34%"
 
277
ALIGN="center"
 
278
VALIGN="top"
 
279
><A
 
280
HREF="dml.html"
 
281
ACCESSKEY="U"
 
282
>Up</A
 
283
></TD
 
284
><TD
 
285
WIDTH="33%"
 
286
ALIGN="right"
 
287
VALIGN="top"
 
288
>Deleting Data</TD
 
289
></TR
 
290
></TABLE
 
291
></DIV
 
292
></BODY
 
293
></HTML
 
294
>
 
 
b'\\ No newline at end of file'