2 <title>Inheritance</title>
5 Let's create two classes. The capitals class contains
6 state capitals which are also cities. Naturally, the
7 capitals class should inherit from cities.
13 altitude int -- (in ft)
16 CREATE TABLE capitals (
21 In this case, an instance of capitals <firstterm>inherits</firstterm> all
22 attributes (name, population, and altitude) from its
23 parent, cities. The type of the attribute name is
24 <type>text</type>, a native <productname>Postgres</productname> type for variable length
25 ASCII strings. The type of the attribute population is
26 <type>float</type>, a native <productname>Postgres</productname> type for double precision
27 floating point numbers. State capitals have an extra
28 attribute, state, that shows their state. In <productname>Postgres</productname>,
29 a class can inherit from zero or more other classes,
30 and a query can reference either all instances of a
31 class or all instances of a class plus all of its
36 The inheritance hierarchy is a actually a directed acyclic graph.
40 For example, the following query finds
41 all the cities that are situated at an attitude of 500ft or higher:
46 WHERE altitude > 500;
49 -----------+----------
57 On the other hand, to find the names of all cities,
58 including state capitals, that are located at an altitude
59 over 500ft, the query is:
62 SELECT c.name, c.altitude
64 WHERE c.altitude > 500;
71 -----------+----------
77 Here the <quote>*</quote> after cities indicates that the query should
78 be run over cities and all classes below cities in the
79 inheritance hierarchy. Many of the commands that we
80 have already discussed -- <command>SELECT</command>,
81 <command>UPDATE</command> and <command>DELETE</command> --
82 support this <quote>*</quote> notation, as do others, like
83 <command>ALTER TABLE</command>.
87 <!-- Keep this comment at the end of the file
92 sgml-minimize-attributes:nil
93 sgml-always-quote-attributes:t
96 sgml-parent-document:nil
97 sgml-default-dtd-file:"./reference.ced"
99 sgml-local-catalogs:"/usr/lib/sgml/catalog"
100 sgml-local-ecat-files:nil