2000-03-31 05:27:42 +02:00
|
|
|
<!--
|
2000-06-09 03:44:34 +02:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/inherit.sgml,v 1.8 2000/06/09 01:43:56 momjian Exp $
|
2000-03-31 05:27:42 +02:00
|
|
|
-->
|
|
|
|
|
1999-08-08 06:21:33 +02:00
|
|
|
<chapter id="inherit">
|
|
|
|
<title>Inheritance</title>
|
1998-03-01 09:16:16 +01:00
|
|
|
|
1999-08-08 06:21:33 +02:00
|
|
|
<para>
|
|
|
|
Let's create two classes. The capitals class contains
|
|
|
|
state capitals which are also cities. Naturally, the
|
|
|
|
capitals class should inherit from cities.
|
|
|
|
|
2000-03-26 20:32:30 +02:00
|
|
|
<programlisting>
|
1998-03-01 09:16:16 +01:00
|
|
|
CREATE TABLE cities (
|
|
|
|
name text,
|
|
|
|
population float,
|
1999-08-08 06:21:33 +02:00
|
|
|
altitude int -- (in ft)
|
1998-03-01 09:16:16 +01:00
|
|
|
);
|
|
|
|
|
2000-06-09 03:44:34 +02:00
|
|
|
CREATE TABLE capitals UNDER cities (
|
1999-08-08 06:21:33 +02:00
|
|
|
state char(2)
|
2000-06-09 03:44:34 +02:00
|
|
|
);
|
2000-03-26 20:32:30 +02:00
|
|
|
</programlisting>
|
1999-08-08 06:21:33 +02:00
|
|
|
|
|
|
|
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>
|
1998-03-01 09:16:16 +01:00
|
|
|
|
1999-08-08 06:21:33 +02:00
|
|
|
<para>
|
2000-06-09 03:44:34 +02:00
|
|
|
For example, the following query finds the names of all cities,
|
1999-08-08 06:21:33 +02:00
|
|
|
including state capitals, that are located at an altitude
|
|
|
|
over 500ft, the query is:
|
1998-03-01 09:16:16 +01:00
|
|
|
|
2000-06-09 03:44:34 +02:00
|
|
|
<programlisting>
|
|
|
|
SELECT c.name, c.altitude
|
|
|
|
FROM cities c
|
1998-03-01 09:16:16 +01:00
|
|
|
WHERE c.altitude > 500;
|
2000-03-26 20:32:30 +02:00
|
|
|
</programlisting>
|
1999-08-08 06:21:33 +02:00
|
|
|
|
|
|
|
which returns:
|
1998-03-01 09:16:16 +01:00
|
|
|
|
2000-06-09 03:44:34 +02:00
|
|
|
<programlisting>
|
|
|
|
+----------+----------+
|
|
|
|
|name | altitude |
|
|
|
|
+----------+----------+
|
|
|
|
|Las Vegas | 2174 |
|
|
|
|
+----------+----------+
|
|
|
|
|Mariposa | 1953 |
|
|
|
|
+----------+----------+
|
|
|
|
|Madison | 845 |
|
|
|
|
+----------+----------+
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
On the other hand, the following query finds
|
|
|
|
all the cities, but not capital cities
|
|
|
|
that are situated at an attitude of 500ft or higher:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
SELECT name, altitude
|
|
|
|
FROM ONLY cities
|
|
|
|
WHERE altitude > 500;
|
1998-03-01 09:16:16 +01:00
|
|
|
|
2000-06-09 03:44:34 +02:00
|
|
|
+----------+----------+
|
|
|
|
|name | altitude |
|
|
|
|
+----------+----------+
|
|
|
|
|Las Vegas | 2174 |
|
|
|
|
+----------+----------+
|
|
|
|
|Mariposa | 1953 |
|
|
|
|
+----------+----------+
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
Here the <quote>ONLY</quote> before cities indicates that the query should
|
|
|
|
be run over only cities and not classes below cities in the
|
1999-08-08 06:21:33 +02:00
|
|
|
inheritance hierarchy. Many of the commands that we
|
|
|
|
have already discussed -- <command>SELECT</command>,
|
|
|
|
<command>UPDATE</command> and <command>DELETE</command> --
|
2000-06-09 03:44:34 +02:00
|
|
|
support this <quote>ONLY</quote> notation.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Deprecated: In previous versions of postgres, the default was not to
|
|
|
|
get access to child classes. By experience this was found to be error
|
|
|
|
prone. Under the old syntax, to get the sub-classes you append "*"
|
|
|
|
to the table name. For example
|
|
|
|
<programlisting>
|
|
|
|
SELECT * from cities*;
|
|
|
|
</programlisting>
|
|
|
|
This old behaviour is still available by using a SET command...
|
|
|
|
<programlisting>
|
|
|
|
SET EXAMINE_SUBCLASS TO on;
|
|
|
|
</programlisting>
|
1999-08-08 06:21:33 +02:00
|
|
|
</para>
|
|
|
|
</chapter>
|
1998-03-01 09:16:16 +01:00
|
|
|
|
1999-08-08 06:21:33 +02:00
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
2000-03-31 05:27:42 +02:00
|
|
|
mode:sgml
|
1999-08-08 06:21:33 +02:00
|
|
|
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
|
2000-03-31 05:27:42 +02:00
|
|
|
sgml-local-catalogs:("/usr/lib/sgml/catalog")
|
1999-08-08 06:21:33 +02:00
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|