]> granicus.if.org Git - postgresql/blob - src/tutorial/advanced.source
Update copyright for 2006. Update scripts.
[postgresql] / src / tutorial / advanced.source
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 -- $PostgreSQL: pgsql/src/tutorial/advanced.source,v 1.6 2003/11/29 22:41:33 pgsql 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;