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

« back to all changes in this revision

Viewing changes to doc/src/sgml/dml.sgml

  • 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
<!-- doc/src/sgml/dml.sgml -->
 
2
 
 
3
<chapter id="dml">
 
4
 <title>Data Manipulation</title>
 
5
 
 
6
 <remark>
 
7
  This chapter is still quite incomplete.
 
8
 </remark>
 
9
 
 
10
 <para>
 
11
  The previous chapter discussed how to create tables and other
 
12
  structures to hold your data.  Now it is time to fill the tables
 
13
  with data.  This chapter covers how to insert, update, and delete
 
14
  table data.  The chapter
 
15
  after this will finally explain how to extract your long-lost data
 
16
  from the database.
 
17
 </para>
 
18
 
 
19
 <sect1 id="dml-insert">
 
20
  <title>Inserting Data</title>
 
21
 
 
22
  <indexterm zone="dml-insert">
 
23
   <primary>inserting</primary>
 
24
  </indexterm>
 
25
 
 
26
  <indexterm zone="dml-insert">
 
27
   <primary>INSERT</primary>
 
28
  </indexterm>
 
29
 
 
30
  <para>
 
31
   When a table is created, it contains no data.  The first thing to
 
32
   do before a database can be of much use is to insert data.  Data is
 
33
   conceptually inserted one row at a time.  Of course you can also
 
34
   insert more than one row, but there is no way to insert less than
 
35
   one row.  Even if you know only some column values, a
 
36
   complete row must be created.
 
37
  </para>
 
38
 
 
39
  <para>
 
40
   To create a new row, use the <xref linkend="sql-insert">
 
41
   command.  The command requires the
 
42
   table name and column values.  For
 
43
   example, consider the products table from <xref linkend="ddl">:
 
44
<programlisting>
 
45
CREATE TABLE products (
 
46
    product_no integer,
 
47
    name text,
 
48
    price numeric
 
49
);
 
50
</programlisting>
 
51
   An example command to insert a row would be:
 
52
<programlisting>
 
53
INSERT INTO products VALUES (1, 'Cheese', 9.99);
 
54
</programlisting>
 
55
   The data values are listed in the order in which the columns appear
 
56
   in the table, separated by commas.  Usually, the data values will
 
57
   be literals (constants), but scalar expressions are also allowed.
 
58
  </para>
 
59
 
 
60
  <para>
 
61
   The above syntax has the drawback that you need to know the order
 
62
   of the columns in the table.  To avoid this you can also list the
 
63
   columns explicitly.  For example, both of the following commands
 
64
   have the same effect as the one above:
 
65
<programlisting>
 
66
INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', 9.99);
 
67
INSERT INTO products (name, price, product_no) VALUES ('Cheese', 9.99, 1);
 
68
</programlisting>
 
69
   Many users consider it good practice to always list the column
 
70
   names.
 
71
  </para>
 
72
 
 
73
  <para>
 
74
   If you don't have values for all the columns, you can omit some of
 
75
   them.  In that case, the columns will be filled with their default
 
76
   values.  For example:
 
77
<programlisting>
 
78
INSERT INTO products (product_no, name) VALUES (1, 'Cheese');
 
79
INSERT INTO products VALUES (1, 'Cheese');
 
80
</programlisting>
 
81
   The second form is a <productname>PostgreSQL</productname>
 
82
   extension.  It fills the columns from the left with as many values
 
83
   as are given, and the rest will be defaulted.
 
84
  </para>
 
85
 
 
86
  <para>
 
87
   For clarity, you can also request default values explicitly, for
 
88
   individual columns or for the entire row:
 
89
<programlisting>
 
90
INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', DEFAULT);
 
91
INSERT INTO products DEFAULT VALUES;
 
92
</programlisting>
 
93
  </para>
 
94
 
 
95
  <para>
 
96
   You can insert multiple rows in a single command:
 
97
<programlisting>
 
98
INSERT INTO products (product_no, name, price) VALUES
 
99
    (1, 'Cheese', 9.99),
 
100
    (2, 'Bread', 1.99),
 
101
    (3, 'Milk', 2.99);
 
102
</programlisting>
 
103
  </para>
 
104
 
 
105
  <tip>
 
106
   <para>
 
107
    When inserting a lot of data at the same time, considering using
 
108
    the <xref linkend="sql-copy"> command.
 
109
    It is not as flexible as the <xref linkend="sql-insert">
 
110
    command, but is more efficient. Refer
 
111
    to <xref linkend="populate"> for more information on improving
 
112
    bulk loading performance.
 
113
   </para>
 
114
  </tip>
 
115
 </sect1>
 
116
 
 
117
 <sect1 id="dml-update">
 
118
  <title>Updating Data</title>
 
119
 
 
120
  <indexterm zone="dml-update">
 
121
   <primary>updating</primary>
 
122
  </indexterm>
 
123
 
 
124
  <indexterm zone="dml-update">
 
125
   <primary>UPDATE</primary>
 
126
  </indexterm>
 
127
 
 
128
  <para>
 
129
   The modification of data that is already in the database is
 
130
   referred to as updating.  You can update individual rows, all the
 
131
   rows in a table, or a subset of all rows.  Each column can be
 
132
   updated separately; the other columns are not affected.
 
133
  </para>
 
134
 
 
135
  <para>
 
136
   To update existing rows, use the <xref linkend="sql-update">
 
137
   command.  This requires
 
138
   three pieces of information:
 
139
   <orderedlist spacing="compact">
 
140
    <listitem>
 
141
     <para>The name of the table and column to update</para>
 
142
    </listitem>
 
143
 
 
144
    <listitem>
 
145
     <para>The new value of the column</para>
 
146
    </listitem>
 
147
 
 
148
    <listitem>
 
149
     <para>Which row(s) to update</para>
 
150
    </listitem>
 
151
   </orderedlist>
 
152
  </para>
 
153
 
 
154
  <para>
 
155
   Recall from <xref linkend="ddl"> that SQL does not, in general,
 
156
   provide a unique identifier for rows.  Therefore it is not
 
157
   always possible to directly specify which row to update.
 
158
   Instead, you specify which conditions a row must meet in order to
 
159
   be updated.  Only if you have a primary key in the table (independent of
 
160
   whether you declared it or not) can you reliably address individual rows
 
161
   by choosing a condition that matches the primary key.
 
162
   Graphical database access tools rely on this fact to allow you to
 
163
   update rows individually.
 
164
  </para>
 
165
 
 
166
  <para>
 
167
   For example, this command updates all products that have a price of
 
168
   5 to have a price of 10:
 
169
<programlisting>
 
170
UPDATE products SET price = 10 WHERE price = 5;
 
171
</programlisting>
 
172
    This might cause zero, one, or many rows to be updated.  It is not
 
173
    an error to attempt an update that does not match any rows.
 
174
  </para>
 
175
 
 
176
  <para>
 
177
   Let's look at that command in detail. First is the key word
 
178
   <literal>UPDATE</literal> followed by the table name.  As usual,
 
179
   the table name can be schema-qualified, otherwise it is looked up
 
180
   in the path.  Next is the key word <literal>SET</literal> followed
 
181
   by the column name, an equal sign, and the new column value.  The
 
182
   new column value can be any scalar expression, not just a constant.
 
183
   For example, if you want to raise the price of all products by 10%
 
184
   you could use:
 
185
<programlisting>
 
186
UPDATE products SET price = price * 1.10;
 
187
</programlisting>
 
188
   As you see, the expression for the new value can refer to the existing
 
189
   value(s) in the row.  We also left out the <literal>WHERE</literal> clause.
 
190
   If it is omitted, it means that all rows in the table are updated.
 
191
   If it is present, only those rows that match the
 
192
   <literal>WHERE</literal> condition are updated.  Note that the equals
 
193
   sign in the <literal>SET</literal> clause is an assignment while
 
194
   the one in the <literal>WHERE</literal> clause is a comparison, but
 
195
   this does not create any ambiguity.  Of course, the
 
196
   <literal>WHERE</literal> condition does
 
197
   not have to be an equality test.  Many other operators are
 
198
   available (see <xref linkend="functions">).  But the expression
 
199
   needs to evaluate to a Boolean result.
 
200
  </para>
 
201
 
 
202
  <para>
 
203
   You can update more than one column in an
 
204
   <command>UPDATE</command> command by listing more than one
 
205
   assignment in the <literal>SET</literal> clause.  For example:
 
206
<programlisting>
 
207
UPDATE mytable SET a = 5, b = 3, c = 1 WHERE a &gt; 0;
 
208
</programlisting>
 
209
  </para>
 
210
 </sect1>
 
211
 
 
212
 <sect1 id="dml-delete">
 
213
  <title>Deleting Data</title>
 
214
 
 
215
  <indexterm zone="dml-delete">
 
216
   <primary>deleting</primary>
 
217
  </indexterm>
 
218
 
 
219
  <indexterm zone="dml-delete">
 
220
   <primary>DELETE</primary>
 
221
  </indexterm>
 
222
 
 
223
  <para>
 
224
   So far we have explained how to add data to tables and how to
 
225
   change data.  What remains is to discuss how to remove data that is
 
226
   no longer needed.  Just as adding data is only possible in whole
 
227
   rows, you can only remove entire rows from a table.  In the
 
228
   previous section we explained that SQL does not provide a way to
 
229
   directly address individual rows.  Therefore, removing rows can
 
230
   only be done by specifying conditions that the rows to be removed
 
231
   have to match.  If you have a primary key in the table then you can
 
232
   specify the exact row.  But you can also remove groups of rows
 
233
   matching a condition, or you can remove all rows in the table at
 
234
   once.
 
235
  </para>
 
236
 
 
237
  <para>
 
238
   You use the <xref linkend="sql-delete">
 
239
   command to remove rows; the syntax is very similar to the
 
240
   <command>UPDATE</command> command.  For instance, to remove all
 
241
   rows from the products table that have a price of 10, use:
 
242
<programlisting>
 
243
DELETE FROM products WHERE price = 10;
 
244
</programlisting>
 
245
  </para>
 
246
 
 
247
  <para>
 
248
   If you simply write:
 
249
<programlisting>
 
250
DELETE FROM products;
 
251
</programlisting>
 
252
   then all rows in the table will be deleted!  Caveat programmer.
 
253
  </para>
 
254
 </sect1>
 
255
</chapter>