doc PG 17 relnotes: add FETCH_COUNT item
[pgsql.git] / src / tutorial / advanced.source
blob0c68b3344c359c38cd71cd6017ac359e9fe1bf6a
1 ---------------------------------------------------------------------------
2 --
3 -- advanced.sql-
4 --    Tutorial on advanced PostgreSQL features
5 --
6 --
7 -- Copyright (c) 1994, Regents of the University of California
8 --
9 -- src/tutorial/advanced.source
11 ---------------------------------------------------------------------------
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 -----------------------------
20 -- For example, the capitals table inherits from cities table. (It inherits
21 -- all data fields from cities.)
23 CREATE TABLE cities (
24         name            text,
25         population      float8,
26         elevation       int             -- (in ft)
29 CREATE TABLE capitals (
30         state           char(2)
31 ) INHERITS (cities);
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);
38 INSERT INTO capitals VALUES ('Sacramento', 3.694E+5, 30, 'CA');
39 INSERT INTO capitals VALUES ('Madison', 1.913E+5, 845, 'WI');
41 SELECT * FROM cities;
42 SELECT * FROM capitals;
44 -- You can find all cities, including capitals, that
45 -- are located at an elevation of 500 ft or higher by:
47 SELECT c.name, c.elevation
48 FROM cities c
49 WHERE c.elevation > 500;
51 -- To scan rows of the parent table only, use ONLY:
53 SELECT name, elevation
54 FROM ONLY cities
55 WHERE elevation > 500;
58 -- clean up (you must remove the children first)
59 DROP TABLE capitals;
60 DROP TABLE cities;