]> granicus.if.org Git - postgresql/blob - doc/src/sgml/query.sgml
6f1f20f9d7e5d0c1711b655083ee23e8bea89f74
[postgresql] / doc / src / sgml / query.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/query.sgml,v 1.22 2001/11/23 21:08:51 tgl Exp $
3 -->
4
5  <chapter id="tutorial-sql">
6   <title>The <acronym>SQL</acronym> Language</title>
7
8   <sect1 id="tutorial-sql-intro">
9    <title>Introduction</title>
10
11    <para>
12     This chapter provides an overview of how to use
13     <acronym>SQL</acronym> to perform simple operations.  This
14     tutorial is only intended to give you an introduction and is in no
15     way a complete tutorial on <acronym>SQL</acronym>.  Numerous books
16     have been written on <acronym>SQL92</acronym>, including <xref
17     linkend="MELT93"> and <xref linkend="DATE97">.
18     You should be aware that some <productname>PostgreSQL</productname>
19     language features are extensions to the standard.
20    </para>
21
22    <para>
23     In the examples that follow, we assume that you have created a
24     database named <quote>mydb</quote>, as described in the previous
25     chapter, and have started <application>psql</application>.
26    </para>
27
28    <para>
29     Examples in this manual can also be found in the
30     <productname>PostgreSQL</productname> source distribution
31     in the directory <filename>src/tutorial/</filename>.  Refer to the
32     <filename>README</filename> file in that directory for how to use
33     them.  To start the tutorial, do the following:
34
35 <screen>
36 <prompt>$</prompt> <userinput>cd <replaceable>....</replaceable>/src/tutorial</userinput>
37 <prompt>$</prompt> <userinput>psql -s mydb</userinput>
38 <computeroutput>
39 ...
40 </computeroutput>
41
42 <prompt>mydb=&gt;</prompt> <userinput>\i basics.sql</userinput>
43 </screen>
44
45     The <literal>\i</literal> command reads in commands from the
46     specified file. The <literal>-s</literal> option puts you in
47     single step mode which pauses before sending each query to the
48     server.  The commands used in this section are in the file
49     <filename>basics.sql</filename>.
50    </para>
51   </sect1>
52
53
54   <sect1 id="tutorial-concepts">
55    <title>Concepts</title>
56
57    <para>
58     <indexterm><primary>relational database</primary></indexterm>
59     <indexterm><primary>hierarchical database</primary></indexterm>
60     <indexterm><primary>object-oriented database</primary></indexterm>
61     <indexterm><primary>relation</primary></indexterm>
62     <indexterm><primary>table</primary></indexterm>
63
64     <productname>PostgreSQL</productname> is a <firstterm>relational
65     database management system</firstterm> (<acronym>RDBMS</acronym>).
66     That means it is a system for managing data stored in
67     <firstterm>relations</firstterm>.  Relation is essentially a
68     mathematical term for <firstterm>table</firstterm>.  The notion of
69     storing data in tables is so commonplace today that it might
70     seem inherently obvious, but there are a number of other ways of
71     organizing databases.  Files and directories on Unix-like
72     operating systems form an example of a hierarchical database.  A
73     more modern development is the object-oriented database.
74    </para>
75
76    <para>
77     <indexterm><primary>row</primary></indexterm>
78     <indexterm><primary>column</primary></indexterm>
79
80     Each table is a named collection of <firstterm>rows</firstterm>.
81     Each row of a given table has the same set of named
82     <firstterm>columns</firstterm>,
83     and each column is of a specific data type.  Whereas columns have
84     a fixed order in each row, it is important to remember that SQL
85     does not guarantee the order of the rows within the table in any
86     way (although they can be explicitly sorted for display).
87    </para>
88
89    <para>
90     <indexterm><primary>cluster</primary></indexterm>
91
92     Tables are grouped into databases, and a collection of databases
93     managed by a single <productname>PostgreSQL</productname> server
94     instance constitutes a database <firstterm>cluster</firstterm>.
95    </para>
96   </sect1>
97
98
99   <sect1 id="tutorial-table">
100    <title>Creating a New Table</title>
101
102    <indexterm zone="tutorial-table">
103     <primary>CREATE TABLE</primary>
104    </indexterm>
105
106    <para>
107     You  can  create  a  new  table by specifying the table
108     name, along with all column names and their types:
109
110 <programlisting>
111 CREATE TABLE weather (
112     city            varchar(80),
113     temp_lo         int,           -- low temperature
114     temp_hi         int,           -- high temperature
115     prcp            real,          -- precipitation
116     date            date
117 );
118 </programlisting>
119
120     You can enter this into <command>psql</command> with the line
121     breaks.  <command>psql</command> will recognize that the command
122     is not terminated until the semicolon.
123    </para>
124
125    <para>
126     White space (i.e., spaces, tabs, and newlines) may be used freely
127     in SQL commands.  That means you can type the command aligned
128     differently than above, or even all on one line.  Two dashes
129     (<quote><literal>--</literal></quote>) introduce comments.
130     Whatever follows them is ignored up to the end of the line.  SQL
131     is case insensitive about key words and identifiers, except
132     when identifiers are double-quoted to preserve the case (not done
133     above).
134    </para>
135
136    <para>
137     <type>varchar(80)</type> specifies a data type that can store
138     arbitrary character strings up to 80 characters in length.
139     <type>int</type> is the normal integer type.  <type>real</type> is
140     a type for storing single precision floating point numbers.
141     <type>date</type> should be self-explanatory.  (Yes, the column of
142     type <type>date</type> is also named <literal>date</literal>.
143     This may be convenient or confusing -- you choose.)
144    </para>
145
146    <para>
147     <productname>PostgreSQL</productname> supports the usual
148     <acronym>SQL</acronym> types <type>int</type>,
149     <type>smallint</type>, <type>real</type>, <type>double
150     precision</type>, <type>char(<replaceable>N</>)</type>,
151     <type>varchar(<replaceable>N</>)</type>, <type>date</type>,
152     <type>time</type>, <type>timestamp</type>, and
153     <type>interval</type>, as well as other types of general utility
154     and a rich set of geometric types.
155     <productname>PostgreSQL</productname> can be customized with an
156     arbitrary number of user-defined data types.  Consequently, type
157     names are not syntactical keywords, except where required to
158     support special cases in the <acronym>SQL</acronym> standard.
159    </para>
160
161    <para>
162     The second example will store cities and their associated
163     geographical location:
164 <programlisting>
165 CREATE TABLE cities (
166     name            varchar(80),
167     location        point
168 );
169 </programlisting>
170     The <type>point</type> type is an example of a
171     <productname>PostgreSQL</productname>-specific data type.
172    </para>
173
174    <para>
175     <indexterm>
176      <primary>DROP TABLE</primary>
177     </indexterm>
178
179     Finally, it should be mentioned that if you don't need a table any
180     longer or want to recreate it differently you can remove it using
181     the following command:
182 <synopsis>
183 DROP TABLE <replaceable>tablename</replaceable>;
184 </synopsis>
185    </para>
186   </sect1>
187
188
189   <sect1 id="tutorial-populate">
190    <title>Populating a Table With Rows</title>
191
192    <indexterm zone="tutorial-populate">
193     <primary>INSERT</primary>
194    </indexterm>
195
196    <para>
197     The <command>INSERT</command> statement is used to populate a table  with
198     rows:
199
200 <programlisting>
201 INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
202 </programlisting>
203
204     Note that all data types use rather obvious input formats.
205     Constants that are not simple numeric values usually must be
206     surrounded by single quotes (<literal>'</>), as in the example.
207     The
208     <type>date</type> column is actually quite flexible in what it
209     accepts, but for this tutorial we will stick to the unambiguous
210     format shown here.
211    </para>
212
213    <para>
214     The <type>point</type> type requires a coordinate pair as input,
215     as shown here:
216 <programlisting>
217 INSERT INTO cities  VALUES ('San Francisco', '(-194.0, 53.0)');
218 </programlisting>
219    </para>
220
221    <para>
222     The syntax used so far requires you to remember the order of the
223     columns.  An alternative syntax allows you to list the columns
224     explicitly:
225 <programlisting>
226 INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
227     VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');
228 </programlisting>
229     You can list the columns in a different order if you wish or
230     even omit some columns, e.g., if the precipitation is unknown:
231 <programlisting>
232 INSERT INTO weather (date, city, temp_hi, temp_lo)
233     VALUES ('1994-11-29', 'Hayward', 54, 37);
234 </programlisting>
235     Many developers consider explicitly listing the columns better
236     style than relying on the order implicitly.
237    </para>
238
239    <para>
240     Please enter all the commands shown above so you have some data to
241     work with in the following sections.
242    </para>
243
244    <para>
245     <indexterm>
246      <primary>COPY</primary>
247     </indexterm>
248
249     You could also have used <command>COPY</command> to load large
250     amounts of data from flat text files.  This is usually faster
251     because the <command>COPY</command> is optimized for this
252     application while allowing less flexibility than
253     <command>INSERT</command>.  An example would be:
254
255 <programlisting>
256 COPY weather FROM '/home/user/weather.txt';
257 </programlisting>
258
259     where the path name for the source file must be available to the
260     backend server machine, not the client, since the backend server
261     reads the file directly.  You can read more about the
262     <command>COPY</command> command in the <citetitle>Reference
263     Manual</citetitle>.
264    </para>
265   </sect1>
266
267
268   <sect1 id="tutorial-select">
269    <title>Querying a Table</title>
270
271    <para>
272     <indexterm><primary>query</primary></indexterm>
273     <indexterm><primary>SELECT</primary></indexterm>
274
275     To retrieve data from a table it is
276     <firstterm>queried</firstterm>.  An <acronym>SQL</acronym>
277     <command>SELECT</command> statement is used to do this.  The
278     statement is divided into a select list (the part that lists the
279     columns to be returned), a table list (the part that lists the
280     tables from which to retrieve the data), and an optional
281     qualification (the part that specifies any restrictions).  For
282     example, to retrieve all the rows of
283     <classname>weather</classname>, type:
284 <programlisting>
285 SELECT * FROM weather;
286 </programlisting>
287     (here <literal>*</literal> means <quote>all columns</quote>) and
288     the output should be:
289 <screen>
290      city      | temp_lo | temp_hi | prcp |    date
291 ---------------+---------+---------+------+------------
292  San Francisco |      46 |      50 | 0.25 | 1994-11-27
293  San Francisco |      43 |      57 |    0 | 1994-11-29
294  Hayward       |      37 |      54 |      | 1994-11-29
295 (3 rows)
296 </screen>
297    </para>
298
299    <para>
300     You may specify any arbitrary expressions in the target list.  For 
301     example, you can do:
302 <programlisting>
303 SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
304 </programlisting>
305     This should give:
306 <screen>
307      city      | temp_avg |    date
308 ---------------+----------+------------
309  San Francisco |       48 | 1994-11-27
310  San Francisco |       50 | 1994-11-29
311  Hayward       |       45 | 1994-11-29
312 (3 rows)
313 </screen>
314     Notice how the <literal>AS</literal> clause is used to relabel the
315     output column.  (It is optional.)
316    </para>
317
318    <para>
319     Arbitrary Boolean operators (<literal>AND</literal>,
320     <literal>OR</literal>, and <literal>NOT</literal>) are allowed in
321     the qualification of a query.  For example, the following
322     retrieves the weather of San Francisco on rainy days:
323
324 <programlisting>
325 SELECT * FROM weather
326     WHERE city = 'San Francisco'
327     AND prcp > 0.0;
328 </programlisting>
329     Result:
330 <screen>
331      city      | temp_lo | temp_hi | prcp |    date
332 ---------------+---------+---------+------+------------
333  San Francisco |      46 |      50 | 0.25 | 1994-11-27
334 (1 row)
335 </screen>
336    </para>
337
338    <para>
339     <indexterm><primary>ORDER BY</primary></indexterm>
340     <indexterm><primary>DISTINCT</primary></indexterm>
341     <indexterm><primary>duplicate</primary></indexterm>
342
343     As a final note, you can request that the results of a select can
344     be returned in sorted order or with duplicate rows removed.  (Just
345     to make sure the following won't confuse you,
346     <literal>DISTINCT</literal> and <literal>ORDER BY</literal> can be
347     used separately.)
348
349 <programlisting>
350 SELECT DISTINCT city
351     FROM weather
352     ORDER BY city;
353 </programlisting>
354
355 <screen>
356      city
357 ---------------
358  Hayward
359  San Francisco
360 (2 rows)
361 </screen>
362    </para>
363   </sect1>
364
365
366   <sect1 id="tutorial-join">
367    <title>Joins Between Tables</title>
368
369    <indexterm zone="tutorial-join">
370     <primary>join</primary>
371    </indexterm>
372
373    <para>
374     Thus far, our queries have only accessed one table at a time.
375     Queries can access multiple tables at once, or access the same
376     table in such a way that multiple rows of the table are being
377     processed at the same time.  A query that accesses multiple rows
378     of the same or different tables at one time is called a
379     <firstterm>join</firstterm> query.  As an example, say you wish to
380     list all the weather records together with the location of the
381     associated city.  To do that, we need to compare the city column of
382     each row of the weather table with the name column of all rows in
383     the cities table, and select the pairs of rows where these values match.
384     <note>
385      <para>
386       This  is only a conceptual model.  The actual join may
387       be performed in a more efficient manner, but this is invisible
388       to the user.
389      </para>
390     </note>
391     This would be accomplished by the following query:
392
393 <programlisting>
394 SELECT *
395     FROM weather, cities
396     WHERE city = name;
397 </programlisting>
398
399 <screen>
400      city      | temp_lo | temp_hi | prcp |    date    |     name      | location
401 ---------------+---------+---------+------+------------+---------------+-----------
402  San Francisco |      46 |      50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
403  San Francisco |      43 |      57 |    0 | 1994-11-29 | San Francisco | (-194,53)
404 (2 rows)
405 </screen>
406
407    </para>
408
409    <para>
410     Observe two things about the result set:
411     <itemizedlist>
412      <listitem>
413       <para>
414        There is no result row for the city of Hayward.  This is
415        because there is no matching entry in the
416        <classname>cities</classname> table for Hayward, so the join
417        ignores the unmatched rows in the weather table.  We will see
418        shortly how this can be fixed.
419       </para>
420      </listitem>
421
422      <listitem>
423       <para>
424        There are two columns containing the city name.  This is
425        correct because the lists of columns of the
426        <classname>weather</classname> and the
427        <classname>cities</classname> tables are concatenated.  In
428        practice this is undesirable, though, so you will probably want
429        to list the output columns explicitly rather than using
430        <literal>*</literal>:
431 <programlisting>
432 SELECT city, temp_lo, temp_hi, prcp, date, location
433     FROM weather, cities
434     WHERE city = name;
435 </programlisting>
436       </para>
437      </listitem>
438     </itemizedlist>
439    </para>
440
441    <formalpara>
442     <title>Exercise:</title>
443
444     <para>
445      Attempt to find out the semantics of this query when the
446      <literal>WHERE</literal> clause is omitted.
447     </para>
448    </formalpara>
449
450    <para>
451     Since the columns all had different names, the parser
452     automatically found out which table they belong to, but it is good
453     style to fully qualify column names in join queries:
454
455 <programlisting>
456 SELECT weather.city, weather.temp_lo, weather.temp_hi,
457        weather.prcp, weather.date, cities.location
458     FROM weather, cities
459     WHERE cities.name = weather.city;
460 </programlisting>
461    </para>
462
463    <para>
464     Join queries of the kind seen thus far can also be written in this
465     alternative form:
466
467 <programlisting>
468 SELECT *
469     FROM weather INNER JOIN cities ON (weather.city = cities.name);
470 </programlisting>
471
472     This syntax is not as commonly used as the one above, but we show
473     it here to help you understand the following topics.
474    </para>
475
476    <para>
477     <indexterm><primary>join</primary><secondary>outer</secondary></indexterm>
478
479     Now we will figure out how we can get the Hayward records back in.
480     What we want the query to do is to scan the
481     <classname>weather</classname> table and for each row to find the
482     matching <classname>cities</classname> row.  If no matching row is
483     found we want some <quote>empty values</quote> to be substituted
484     for the <classname>cities</classname> table's columns.  This kind
485     of query is called an <firstterm>outer join</firstterm>.  (The
486     joins we have seen so far are inner joins.)  The command looks
487     like this:
488
489 <programlisting>
490 SELECT *
491     FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name);
492
493      city      | temp_lo | temp_hi | prcp |    date    |     name      | location
494 ---------------+---------+---------+------+------------+---------------+-----------
495  Hayward       |      37 |      54 |      | 1994-11-29 |               |
496  San Francisco |      46 |      50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
497  San Francisco |      43 |      57 |    0 | 1994-11-29 | San Francisco | (-194,53)
498 (3 rows)
499 </programlisting>
500
501     This query is called a <firstterm>left outer
502     join</firstterm> because the table mentioned on the left of the
503     join operator will have each of its rows in the output at least
504     once, whereas the table on the right will only have those rows
505     output that match some row of the left table.  When outputting a
506     left-table row for which there is no right-table match, empty (NULL)
507     values are substituted for the right-table columns.
508    </para>
509
510    <formalpara>
511     <title>Exercise:</title>
512
513     <para>
514      There are also right outer joins and full outer joins.  Try to
515      find out what those do.
516     </para>
517    </formalpara>
518
519    <para>
520     <indexterm><primary>join</primary><secondary>self</secondary></indexterm>
521     <indexterm><primary>alias</primary><secondary>for table name in query</secondary></indexterm>
522
523     We can also join a table against itself.  This is called a
524     <firstterm>self join</firstterm>.  As an example, suppose we wish
525     to find all the weather records that are in the temperature range
526     of other weather records.  So we need to compare the
527     <structfield>temp_lo</> and <structfield>temp_hi</> columns of
528     each <classname>weather</classname> row to the
529     <structfield>temp_lo</structfield> and
530     <structfield>temp_hi</structfield> columns of all other
531     <classname>weather</classname> rows.  We can do this with the
532     following query:
533
534 <programlisting>
535 SELECT W1.city, W1.temp_lo AS low, W1.temp_hi AS high,
536     W2.city, W2.temp_lo AS low, W2.temp_hi AS high
537     FROM weather W1, weather W2
538     WHERE W1.temp_lo < W2.temp_lo
539     AND W1.temp_hi > W2.temp_hi;
540
541      city      | low | high |     city      | low | high
542 ---------------+-----+------+---------------+-----+------
543  San Francisco |  43 |   57 | San Francisco |  46 |   50
544  Hayward       |  37 |   54 | San Francisco |  46 |   50
545 (2 rows)
546 </programlisting>     
547
548     Here we have relabeled the weather table as <literal>W1</> and
549     <literal>W2</> to be able to distinguish the left and right side
550     of the join.  You can also use these kinds of aliases in other
551     queries to save some typing, e.g.:
552 <programlisting>
553 SELECT *
554     FROM weather w, cities c
555     WHERE w.city = c.name;
556 </programlisting>
557     You will encounter this style of abbreviating quite frequently.
558    </para>
559   </sect1>
560
561
562   <sect1 id="tutorial-agg">
563    <title>Aggregate Functions</title>
564
565    <indexterm zone="tutorial-agg">
566     <primary>aggregate</primary>
567    </indexterm>
568
569    <para>
570     <indexterm><primary>average</primary></indexterm>
571     <indexterm><primary>count</primary></indexterm>
572     <indexterm><primary>max</primary></indexterm>
573     <indexterm><primary>min</primary></indexterm>
574     <indexterm><primary>sum</primary></indexterm>
575
576     Like  most  other relational database products, 
577     <productname>PostgreSQL</productname> supports
578     aggregate functions.
579     An aggregate function computes a single result from multiple input rows.
580     For example, there are aggregates to compute the
581     <function>count</function>, <function>sum</function>,
582     <function>avg</function> (average), <function>max</function> (maximum) and
583     <function>min</function> (minimum) over a set of rows.
584    </para>
585
586    <para>
587     As an example, we can find the highest low-temperature reading anywhere
588     with
589
590 <programlisting>
591 SELECT max(temp_lo) FROM weather;
592 </programlisting>
593
594 <screen>
595  max
596 -----
597   46
598 (1 row)
599 </screen>
600    </para>
601
602    <para>
603     <indexterm><primary>subquery</primary></indexterm>
604
605     If we want to know what city (or cities) that reading occurred in,
606     we might try
607
608 <programlisting>
609 SELECT city FROM weather WHERE temp_lo = max(temp_lo);     <lineannotation>WRONG</lineannotation>
610 </programlisting>
611
612     but this will not work since the aggregate
613     <function>max</function> cannot be used in the
614     <literal>WHERE</literal> clause.  (This restriction exists because
615     the <literal>WHERE</literal> clause determines the rows that will
616     go into the aggregation stage; so it has to be evaluated before
617     aggregate functions are computed.)
618     However, as is often the case
619     the query can be restated to accomplish the intended result; here
620     by using a <firstterm>subquery</firstterm>:
621
622 <programlisting>
623 SELECT city FROM weather
624     WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
625 </programlisting>
626
627 <screen>
628      city
629 ---------------
630  San Francisco
631 (1 row)
632 </screen>
633
634     This is OK because the sub-select is an independent computation
635     that computes its own aggregate separately from what is happening
636     in the outer select.
637    </para>
638
639    <para>
640     <indexterm><primary>GROUP BY</primary></indexterm>
641     <indexterm><primary>HAVING</primary></indexterm>
642
643     Aggregates are also very useful in combination with <literal>GROUP
644     BY</literal> clauses.  For example, we can get the maximum low
645     temperature observed in each city with
646
647 <programlisting>
648 SELECT city, max(temp_lo)
649     FROM weather
650     GROUP BY city;
651 </programlisting>
652
653 <screen>
654      city      | max
655 ---------------+-----
656  Hayward       |  37
657  San Francisco |  46
658 (2 rows)
659 </screen>
660
661     which gives us one output row per city.  Each aggregate result is
662     computed over the table rows matching that city.
663     We can filter these grouped
664     rows using <literal>HAVING</literal>:
665
666 <programlisting>
667 SELECT city, max(temp_lo)
668     FROM weather
669     GROUP BY city
670     HAVING max(temp_lo) < 40;
671 </programlisting>
672
673 <screen>
674   city   | max
675 ---------+-----
676  Hayward |  37
677 (1 row)
678 </screen>
679
680     which gives us the same results for only the cities that have all
681     <literal>temp_lo</> values below forty.  Finally, if we only care about
682     cities whose
683     names begin with <quote><literal>S</literal></quote>, we might do
684
685 <programlisting>
686 SELECT city, max(temp_lo)
687     FROM weather
688     WHERE city LIKE 'S%'
689     GROUP BY city
690     HAVING max(temp_lo) < 40;
691 </programlisting>
692    </para>
693
694    <para>
695     It is important to understand the interaction between aggregates and
696     SQL's <literal>WHERE</literal> and <literal>HAVING</literal> clauses.
697     The fundamental difference between <literal>WHERE</literal> and
698     <literal>HAVING</literal> is this: <literal>WHERE</literal> selects
699     input rows before groups and aggregates are computed (thus, it controls
700     which rows go into the aggregate computation), whereas
701     <literal>HAVING</literal> selects group rows after groups and
702     aggregates are computed.  Thus, the
703     <literal>WHERE</literal> clause must not contain aggregate functions;
704     it makes no sense to try to use an aggregate to determine which rows
705     will be inputs to the aggregates.  On the other hand,
706     <literal>HAVING</literal> clauses always contain aggregate functions.
707     (Strictly speaking, you are allowed to write a <literal>HAVING</literal>
708     clause that doesn't use aggregates, but it's wasteful; the same condition
709     could be used more efficiently at the <literal>WHERE</literal> stage.)
710    </para>
711
712    <para>
713     Observe that we can apply the city name restriction in
714     <literal>WHERE</literal>, since it needs no aggregate.  This is
715     more efficient than adding the restriction to <literal>HAVING</literal>,
716     because we avoid doing the grouping and aggregate calculations
717     for all rows that fail the <literal>WHERE</literal> check.
718    </para>
719   </sect1>
720
721
722   <sect1 id="tutorial-update">
723    <title>Updates</title>
724
725    <indexterm zone="tutorial-update">
726     <primary>UPDATE</primary>
727    </indexterm>
728
729    <para>
730     You can update existing rows using the
731     <command>UPDATE</command> command. 
732     Suppose you discover the temperature readings are
733     all  off  by 2 degrees as of November 28, you may update the
734     data as follows:
735
736 <programlisting>
737 UPDATE weather
738     SET temp_hi = temp_hi - 2,  temp_lo = temp_lo - 2
739     WHERE date > '1994-11-28';
740 </programlisting>
741    </para>
742
743    <para>
744     Look at the new state of the data:
745 <programlisting>
746 SELECT * FROM weather;
747
748      city      | temp_lo | temp_hi | prcp |    date
749 ---------------+---------+---------+------+------------
750  San Francisco |      46 |      50 | 0.25 | 1994-11-27
751  San Francisco |      41 |      55 |    0 | 1994-11-29
752  Hayward       |      35 |      52 |      | 1994-11-29
753 (3 rows)
754 </programlisting>
755    </para>
756   </sect1>
757
758   <sect1 id="tutorial-delete">
759    <title>Deletions</title>
760
761    <indexterm zone="tutorial-delete">
762     <primary>DELETE</primary>
763    </indexterm>
764
765    <para>
766     Suppose you are no longer interested in the weather of Hayward,
767     then you can do the following to delete those rows from the table.
768     Deletions are performed using the <command>DELETE</command>
769     command:
770 <programlisting>
771 DELETE FROM weather WHERE city = 'Hayward';
772 </programlisting>
773
774     All weather records belonging to Hayward are removed.
775
776 <programlisting>
777 SELECT * FROM weather;
778 </programlisting>
779
780 <screen>
781      city      | temp_lo | temp_hi | prcp |    date
782 ---------------+---------+---------+------+------------
783  San Francisco |      46 |      50 | 0.25 | 1994-11-27
784  San Francisco |      41 |      55 |    0 | 1994-11-29
785 (2 rows)
786 </screen>
787    </para>
788
789    <para>
790     One should be wary of queries of the form
791 <synopsis>
792 DELETE FROM <replaceable>tablename</replaceable>;
793 </synopsis>
794
795     Without a qualification, <command>DELETE</command> will
796     remove  <emphasis>all</>  rows from the given table, leaving it
797     empty.  The system will not request confirmation before
798     doing this!
799    </para>
800   </sect1>
801
802  </chapter>
803
804 <!-- Keep this comment at the end of the file
805 Local variables:
806 mode:sgml
807 sgml-omittag:nil
808 sgml-shorttag:t
809 sgml-minimize-attributes:nil
810 sgml-always-quote-attributes:t
811 sgml-indent-step:1
812 sgml-indent-data:t
813 sgml-parent-document:nil
814 sgml-default-dtd-file:"./reference.ced"
815 sgml-exposed-tags:nil
816 sgml-local-catalogs:("/usr/lib/sgml/catalog")
817 sgml-local-ecat-files:nil
818 End:
819 -->