~ubuntu-branches/ubuntu/gutsy/libpgjava/gutsy

« back to all changes in this revision

Viewing changes to src/tutorial/advanced.source

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2005-04-21 14:25:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050421142511-wibh5vc31fkrorx7
Tags: 7.4.7-3
Built with sources...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
---------------------------------------------------------------------------
 
2
--
 
3
-- advanced.sql-
 
4
--    Tutorial on advanced more PostgreSQL features
 
5
--
 
6
--
 
7
-- Copyright (c) 1994, Regents of the University of California
 
8
--
 
9
-- $Id: advanced.source,v 1.5 2002/04/11 21:18:50 tgl Exp $
 
10
--
 
11
---------------------------------------------------------------------------
 
12
 
 
13
-----------------------------
 
14
-- Inheritance:
 
15
--      A table can inherit from zero or more tables.  A query can reference
 
16
--      either all rows of a table or all rows of a table plus all of its
 
17
--      descendants.
 
18
-----------------------------
 
19
 
 
20
-- For example, the capitals table inherits from cities table. (It inherits 
 
21
-- all data fields from cities.)
 
22
 
 
23
CREATE TABLE cities (
 
24
        name            text,
 
25
        population      float8,
 
26
        altitude        int             -- (in ft)
 
27
);
 
28
 
 
29
CREATE TABLE capitals (
 
30
        state           char(2)
 
31
) INHERITS (cities);
 
32
 
 
33
-- Now, let's populate the tables.
 
34
INSERT INTO cities VALUES ('San Francisco', 7.24E+5, 63);
 
35
INSERT INTO cities VALUES ('Las Vegas', 2.583E+5, 2174);
 
36
INSERT INTO cities VALUES ('Mariposa', 1200, 1953);
 
37
 
 
38
INSERT INTO capitals VALUES ('Sacramento', 3.694E+5, 30, 'CA');
 
39
INSERT INTO capitals VALUES ('Madison', 1.913E+5, 845, 'WI');
 
40
 
 
41
SELECT * FROM cities;
 
42
SELECT * FROM capitals;
 
43
 
 
44
-- You can find all cities, including capitals, that
 
45
-- are located at an altitude of 500 ft or higher by:
 
46
 
 
47
SELECT c.name, c.altitude
 
48
FROM cities c
 
49
WHERE c.altitude > 500;
 
50
 
 
51
-- To scan rows of the parent table only, use ONLY:
 
52
 
 
53
SELECT name, altitude
 
54
FROM ONLY cities
 
55
WHERE altitude > 500;
 
56
 
 
57
 
 
58
-- clean up (you must remove the children first)
 
59
DROP TABLE capitals;
 
60
DROP TABLE cities;