1
<!-- doc/src/sgml/dml.sgml -->
4
<title>Data Manipulation</title>
7
This chapter is still quite incomplete.
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
19
<sect1 id="dml-insert">
20
<title>Inserting Data</title>
22
<indexterm zone="dml-insert">
23
<primary>inserting</primary>
26
<indexterm zone="dml-insert">
27
<primary>INSERT</primary>
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.
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">:
45
CREATE TABLE products (
51
An example command to insert a row would be:
53
INSERT INTO products VALUES (1, 'Cheese', 9.99);
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.
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:
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);
69
Many users consider it good practice to always list the column
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
78
INSERT INTO products (product_no, name) VALUES (1, 'Cheese');
79
INSERT INTO products VALUES (1, 'Cheese');
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.
87
For clarity, you can also request default values explicitly, for
88
individual columns or for the entire row:
90
INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', DEFAULT);
91
INSERT INTO products DEFAULT VALUES;
96
You can insert multiple rows in a single command:
98
INSERT INTO products (product_no, name, price) VALUES
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.
117
<sect1 id="dml-update">
118
<title>Updating Data</title>
120
<indexterm zone="dml-update">
121
<primary>updating</primary>
124
<indexterm zone="dml-update">
125
<primary>UPDATE</primary>
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.
136
To update existing rows, use the <xref linkend="sql-update">
137
command. This requires
138
three pieces of information:
139
<orderedlist spacing="compact">
141
<para>The name of the table and column to update</para>
145
<para>The new value of the column</para>
149
<para>Which row(s) to update</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.
167
For example, this command updates all products that have a price of
168
5 to have a price of 10:
170
UPDATE products SET price = 10 WHERE price = 5;
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.
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%
186
UPDATE products SET price = price * 1.10;
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.
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:
207
UPDATE mytable SET a = 5, b = 3, c = 1 WHERE a > 0;
212
<sect1 id="dml-delete">
213
<title>Deleting Data</title>
215
<indexterm zone="dml-delete">
216
<primary>deleting</primary>
219
<indexterm zone="dml-delete">
220
<primary>DELETE</primary>
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
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:
243
DELETE FROM products WHERE price = 10;
250
DELETE FROM products;
252
then all rows in the table will be deleted! Caveat programmer.