]> begriffs open source - ai-pg/blob - full-docs/txt/tutorial-inheritance.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / tutorial-inheritance.txt
1
2 3.6. Inheritance #
3
4    Inheritance is a concept from object-oriented databases. It opens up
5    interesting new possibilities of database design.
6
7    Let's create two tables: A table cities and a table capitals.
8    Naturally, capitals are also cities, so you want some way to show the
9    capitals implicitly when you list all cities. If you're really clever
10    you might invent some scheme like this:
11 CREATE TABLE capitals (
12   name       text,
13   population real,
14   elevation  int,    -- (in ft)
15   state      char(2)
16 );
17
18 CREATE TABLE non_capitals (
19   name       text,
20   population real,
21   elevation  int     -- (in ft)
22 );
23
24 CREATE VIEW cities AS
25   SELECT name, population, elevation FROM capitals
26     UNION
27   SELECT name, population, elevation FROM non_capitals;
28
29    This works OK as far as querying goes, but it gets ugly when you need
30    to update several rows, for one thing.
31
32    A better solution is this:
33 CREATE TABLE cities (
34   name       text,
35   population real,
36   elevation  int     -- (in ft)
37 );
38
39 CREATE TABLE capitals (
40   state      char(2) UNIQUE NOT NULL
41 ) INHERITS (cities);
42
43    In this case, a row of capitals inherits all columns (name, population,
44    and elevation) from its parent, cities. The type of the column name is
45    text, a native PostgreSQL type for variable length character strings.
46    The capitals table has an additional column, state, which shows its
47    state abbreviation. In PostgreSQL, a table can inherit from zero or
48    more other tables.
49
50    For example, the following query finds the names of all cities,
51    including state capitals, that are located at an elevation over 500
52    feet:
53 SELECT name, elevation
54   FROM cities
55   WHERE elevation > 500;
56
57    which returns:
58    name    | elevation
59 -----------+-----------
60  Las Vegas |      2174
61  Mariposa  |      1953
62  Madison   |       845
63 (3 rows)
64
65    On the other hand, the following query finds all the cities that are
66    not state capitals and are situated at an elevation over 500 feet:
67 SELECT name, elevation
68     FROM ONLY cities
69     WHERE elevation > 500;
70
71    name    | elevation
72 -----------+-----------
73  Las Vegas |      2174
74  Mariposa  |      1953
75 (2 rows)
76
77    Here the ONLY before cities indicates that the query should be run over
78    only the cities table, and not tables below cities in the inheritance
79    hierarchy. Many of the commands that we have already discussed —
80    SELECT, UPDATE, and DELETE — support this ONLY notation.
81
82 Note
83
84    Although inheritance is frequently useful, it has not been integrated
85    with unique constraints or foreign keys, which limits its usefulness.
86    See Section 5.11 for more detail.