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