<!--
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/inherit.sgml,v 1.7 2000/05/02 20:01:51 thomas Exp $
-->

 <chapter id="inherit">
  <title>Inheritance</title>

  <para>
   Let's create two classes. The capitals  class  contains
   state  capitals  which  are also cities. Naturally, the
   capitals class should inherit from cities.

<programlisting>
CREATE TABLE cities (
    name            text,
    population      float,
    altitude        int     -- (in ft)
);

CREATE TABLE capitals (
    state           char(2)
) INHERITS (cities);
</programlisting>

   In this case, an  instance  of  capitals  <firstterm>inherits</firstterm>  all
   attributes  (name,  population,  and altitude) from its
   parent, cities.  The type  of  the  attribute  name  is
   <type>text</type>,  a  native  <productname>Postgres</productname>  type  for variable length
   ASCII strings.  The type of the attribute population is
   <type>float</type>,  a  native <productname>Postgres</productname> type for double precision
   floating point numbers.  State capitals have  an  extra
   attribute, state, that shows their state.  In <productname>Postgres</productname>,
   a  class  can inherit from zero or more other classes,
   and a query can reference either  all  instances  of  a
   class  or  all  instances  of  a  class plus all of its
   descendants. 

   <note>
    <para>
     The inheritance hierarchy is a actually a directed acyclic graph.
    </para>
   </note>

   For example, the  following  query  finds
   all  the cities that are situated at an attitude of 500ft or higher:

<programlisting>
SELECT name, altitude
    FROM cities
    WHERE altitude &gt; 500;

   name    | altitude
-----------+----------
 Las Vegas |     2174
 Mariposa  |     1953
(2 rows)
</programlisting>         
  </para>

  <para>
   On the other hand, to find the  names  of  all  cities,
   including  state capitals, that are located at an altitude 
   over 500ft, the query is:

<programlisting>
SELECT c.name, c.altitude
    FROM cities* c
    WHERE c.altitude > 500;
</programlisting>

   which returns:

<programlisting>
   name    | altitude
-----------+----------
 Las Vegas |     2174
 Mariposa  |     1953
 Madison   |      845
</programlisting>

   Here the "*" after cities indicates that the query should
   be  run over cities and all classes below cities in the
   inheritance hierarchy.  Many of the  commands  that  we
   have  already discussed -- <command>SELECT</command>,
   <command>UPDATE</command> and <command>DELETE</command> --
   support this "*" notation, as do others, like
   <command>ALTER TABLE</command>.
  </para>
 </chapter>

<!-- Keep this comment at the end of the file
Local variables:
mode:sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"./reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:("/usr/lib/sgml/catalog")
sgml-local-ecat-files:nil
End:
-->
