1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/advanced.sgml,v 1.27 2002/02/12 22:25:15 momjian Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/advanced.sgml,v 1.28 2002/06/11 15:32:32 thomas Exp $
3
3
-->
4
4
5
5
<chapter id="tutorial-advanced">
@@ -46,14 +46,14 @@ $Header: /cvsroot/pgsql/doc/src/sgml/advanced.sgml,v 1.27 2002/02/12 22:25:15 mo
46
46
<firstterm>view</firstterm> over the query, which gives a name to
47
47
the query that you can refer to like an ordinary table.
48
48
49
- <programlisting>
49
+ <programlisting>
50
50
CREATE VIEW myview AS
51
51
SELECT city, temp_lo, temp_hi, prcp, date, location
52
52
FROM weather, cities
53
53
WHERE city = name;
54
54
55
55
SELECT * FROM myview;
56
- </programlisting>
56
+ </programlisting>
57
57
</para>
58
58
59
59
<para>
@@ -101,32 +101,30 @@ SELECT * FROM myview;
101
101
<para>
102
102
The new declaration of the tables would look like this:
103
103
104
- <programlisting>
104
+ <programlisting>
105
105
CREATE TABLE cities (
106
- city varchar(80) primary key,
107
- location point
106
+ city varchar(80) primary key,
107
+ location point
108
108
);
109
109
110
110
CREATE TABLE weather (
111
- city varchar(80) references cities,
112
- temp_lo int,
113
- temp_hi int,
114
- prcp real,
115
- date date
111
+ city varchar(80) references cities,
112
+ temp_lo int,
113
+ temp_hi int,
114
+ prcp real,
115
+ date date
116
116
);
117
-
118
- </programlisting>
117
+ </programlisting>
119
118
120
119
Now try inserting an invalid record:
121
120
122
- <programlisting>
121
+ <programlisting>
123
122
INSERT INTO weather VALUES ('Berkeley', 45, 53, 0.0, '1994-11-28');
124
- </programlisting>
123
+ </programlisting>
125
124
126
- <screen>
125
+ <screen>
127
126
ERROR: <unnamed> referential integrity violation - key referenced from weather not found in cities
128
- </screen>
129
-
127
+ </screen>
130
128
</para>
131
129
132
130
<para>
@@ -162,7 +160,8 @@ ERROR: <unnamed> referential integrity violation - key referenced from we
162
160
Suppose that we want to record a payment of $100.00 from Alice's account
163
161
to Bob's account. Simplifying outrageously, the SQL commands for this
164
162
might look like
165
- <programlisting>
163
+
164
+ <programlisting>
166
165
UPDATE accounts SET balance = balance - 100.00
167
166
WHERE name = 'Alice';
168
167
UPDATE branches SET balance = balance - 100.00
@@ -171,7 +170,10 @@ UPDATE accounts SET balance = balance + 100.00
171
170
WHERE name = 'Bob';
172
171
UPDATE branches SET balance = balance + 100.00
173
172
WHERE name = (SELECT branch_name FROM accounts WHERE name = 'Bob');
174
- </programlisting>
173
+ </programlisting>
174
+ </para>
175
+
176
+ <para>
175
177
The details of these commands are not important here; the important
176
178
point is that there are several separate updates involved to accomplish
177
179
this rather simple operation. Our bank's officers will want to be
@@ -219,13 +221,17 @@ UPDATE branches SET balance = balance + 100.00
219
221
the SQL commands of the transaction with
220
222
<command>BEGIN</> and <command>COMMIT</> commands. So our banking
221
223
transaction would actually look like
222
- <programlisting>
224
+
225
+ <programlisting>
223
226
BEGIN;
224
227
UPDATE accounts SET balance = balance - 100.00
225
228
WHERE name = 'Alice';
226
229
-- etc etc
227
230
COMMIT;
228
- </programlisting>
231
+ </programlisting>
232
+ </para>
233
+
234
+ <para>
229
235
If, partway through the transaction, we decide we don't want to
230
236
commit (perhaps we just noticed that Alice's balance went negative),
231
237
we can issue the command <command>ROLLBACK</> instead of
@@ -272,25 +278,25 @@ COMMIT;
272
278
implicitly when you list all cities. If you're really clever you
273
279
might invent some scheme like this:
274
280
275
- <programlisting>
281
+ <programlisting>
276
282
CREATE TABLE capitals (
277
- name text,
278
- population real,
279
- altitude int, -- (in ft)
280
- state char(2)
283
+ name text,
284
+ population real,
285
+ altitude int, -- (in ft)
286
+ state char(2)
281
287
);
282
288
283
289
CREATE TABLE non_capitals (
284
- name text,
285
- population real,
286
- altitude int -- (in ft)
290
+ name text,
291
+ population real,
292
+ altitude int -- (in ft)
287
293
);
288
294
289
295
CREATE VIEW cities AS
290
- SELECT name, population, altitude FROM capitals
291
- UNION
292
- SELECT name, population, altitude FROM non_capitals;
293
- </programlisting>
296
+ SELECT name, population, altitude FROM capitals
297
+ UNION
298
+ SELECT name, population, altitude FROM non_capitals;
299
+ </programlisting>
294
300
295
301
This works OK as far as querying goes, but it gets ugly when you
296
302
need to update several rows, to name one thing.
@@ -299,18 +305,20 @@ CREATE VIEW cities AS
299
305
<para>
300
306
A better solution is this:
301
307
302
- <programlisting>
308
+ <programlisting>
303
309
CREATE TABLE cities (
304
- name text,
305
- population real,
306
- altitude int -- (in ft)
310
+ name text,
311
+ population real,
312
+ altitude int -- (in ft)
307
313
);
308
314
309
315
CREATE TABLE capitals (
310
- state char(2)
316
+ state char(2)
311
317
) INHERITS (cities);
312
- </programlisting>
318
+ </programlisting>
319
+ </para>
313
320
321
+ <para>
314
322
In this case, a row of <classname>capitals</classname>
315
323
<firstterm>inherits</firstterm> all columns (<structfield>name</>,
316
324
<structfield>population</>, and <structfield>altitude</>) from its
@@ -328,42 +336,42 @@ CREATE TABLE capitals (
328
336
including state capitals, that are located at an altitude
329
337
over 500 ft.:
330
338
331
- <programlisting>
339
+ <programlisting>
332
340
SELECT name, altitude
333
- FROM cities
334
- WHERE altitude > 500;
335
- </programlisting>
341
+ FROM cities
342
+ WHERE altitude > 500;
343
+ </programlisting>
336
344
337
345
which returns:
338
346
339
- <screen>
347
+ <screen>
340
348
name | altitude
341
349
-----------+----------
342
350
Las Vegas | 2174
343
351
Mariposa | 1953
344
352
Madison | 845
345
353
(3 rows)
346
- </screen>
354
+ </screen>
347
355
</para>
348
356
349
357
<para>
350
358
On the other hand, the following query finds
351
359
all the cities that are not state capitals and
352
360
are situated at an altitude of 500 ft. or higher:
353
361
354
- <programlisting>
362
+ <programlisting>
355
363
SELECT name, altitude
356
364
FROM ONLY cities
357
365
WHERE altitude > 500;
358
- </programlisting>
366
+ </programlisting>
359
367
360
368
<screen>
361
369
name | altitude
362
370
-----------+----------
363
371
Las Vegas | 2174
364
372
Mariposa | 1953
365
373
(2 rows)
366
- </screen>
374
+ </screen>
367
375
</para>
368
376
369
377
<para>
@@ -397,7 +405,6 @@ SELECT name, altitude
397
405
site</ulink> for links to more resources.
398
406
</para>
399
407
</sect1>
400
-
401
408
</chapter>
402
409
403
410
<!-- Keep this comment at the end of the file