]> granicus.if.org Git - postgresql/blob - doc/src/sgml/inherit.sgml
Updated user's guide to match new psql's output format
[postgresql] / doc / src / sgml / inherit.sgml
1  <chapter id="inherit">
2   <title>Inheritance</title>
3
4   <para>
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.
8
9 <programlisting>
10 CREATE TABLE cities (
11     name            text,
12     population      float,
13     altitude        int     -- (in ft)
14 );
15
16 CREATE TABLE capitals (
17     state           char(2)
18 ) INHERITS (cities);
19 </programlisting>
20
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
32    descendants. 
33
34    <note>
35     <para>
36      The inheritance hierarchy is a actually a directed acyclic graph.
37     </para>
38    </note>
39
40    For example, the  following  query  finds
41    all  the cities that are situated at an attitude of 500ft or higher:
42
43 <programlisting>
44 SELECT name, altitude
45     FROM cities
46     WHERE altitude &gt; 500;
47
48    name    | altitude
49 -----------+----------
50  Las Vegas |     2174
51  Mariposa  |     1953
52 (2 rows)
53 </programlisting>         
54   </para>
55
56   <para>
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:
60
61 <programlisting>
62 SELECT c.name, c.altitude
63     FROM cities* c
64     WHERE c.altitude > 500;
65 </programlisting>
66
67    which returns:
68
69 <programlisting>
70    name    | altitude
71 -----------+----------
72  Las Vegas |     2174
73  Mariposa  |     1953
74  Madison   |      845
75 </programlisting>
76
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>.
84   </para>
85  </chapter>
86
87 <!-- Keep this comment at the end of the file
88 Local variables:
89 mode: sgml
90 sgml-omittag:nil
91 sgml-shorttag:t
92 sgml-minimize-attributes:nil
93 sgml-always-quote-attributes:t
94 sgml-indent-step:1
95 sgml-indent-data:t
96 sgml-parent-document:nil
97 sgml-default-dtd-file:"./reference.ced"
98 sgml-exposed-tags:nil
99 sgml-local-catalogs:"/usr/lib/sgml/catalog"
100 sgml-local-ecat-files:nil
101 End:
102 -->