2000-03-31 05:27:42 +02:00
|
|
|
<!--
|
2001-01-14 00:58:55 +01:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/inherit.sgml,v 1.13 2001/01/13 23:58:55 petere 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>
|
2001-01-14 00:58:55 +01:00
|
|
|
Let's create two tables. The capitals table contains
|
1999-08-08 06:21:33 +02:00
|
|
|
state capitals which are also cities. Naturally, the
|
2001-01-14 00:58:55 +01:00
|
|
|
capitals table should inherit from cities.
|
1999-08-08 06:21:33 +02:00
|
|
|
|
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
|
|
|
);
|
|
|
|
|
2001-01-05 07:34:23 +01:00
|
|
|
CREATE TABLE capitals (
|
1999-08-08 06:21:33 +02:00
|
|
|
state char(2)
|
2001-01-05 07:34:23 +01:00
|
|
|
) INHERITS (cities);
|
2000-03-26 20:32:30 +02:00
|
|
|
</programlisting>
|
1999-08-08 06:21:33 +02:00
|
|
|
|
2001-01-14 00:58:55 +01:00
|
|
|
In this case, a row of capitals <firstterm>inherits</firstterm> all
|
1999-08-08 06:21:33 +02:00
|
|
|
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>,
|
2001-01-14 00:58:55 +01:00
|
|
|
a table can inherit from zero or more other tables,
|
|
|
|
and a query can reference either all rows of a
|
|
|
|
table or all rows of a table plus all of its
|
1999-08-08 06:21:33 +02:00
|
|
|
descendants.
|
|
|
|
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
The inheritance hierarchy is a actually a directed acyclic graph.
|
|
|
|
</para>
|
|
|
|
</note>
|
2000-06-14 15:12:52 +02:00
|
|
|
</para>
|
1998-03-01 09:16:16 +01:00
|
|
|
|
1999-08-08 06:21:33 +02:00
|
|
|
<para>
|
2001-01-05 07:34:23 +01:00
|
|
|
For example, the following query finds the names of all cities,
|
|
|
|
including state capitals, that are located at an altitude
|
|
|
|
over 500ft:
|
1998-03-01 09:16:16 +01:00
|
|
|
|
2001-01-05 07:34:23 +01:00
|
|
|
<programlisting>
|
|
|
|
SELECT name, altitude
|
|
|
|
FROM cities
|
|
|
|
WHERE altitude > 500;
|
|
|
|
</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>
|
2001-01-05 07:34:23 +01:00
|
|
|
On the other hand, the following query finds
|
|
|
|
all the cities that are not state capitals and
|
|
|
|
are situated at an altitude of 500ft or higher:
|
2000-06-09 03:44:34 +02:00
|
|
|
|
|
|
|
<programlisting>
|
2001-01-05 07:34:23 +01:00
|
|
|
SELECT name, altitude
|
2000-06-09 03:44:34 +02:00
|
|
|
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>
|
|
|
|
|
2000-06-14 15:12:52 +02:00
|
|
|
<para>
|
2000-06-09 03:44:34 +02:00
|
|
|
Here the <quote>ONLY</quote> before cities indicates that the query should
|
2001-01-14 00:58:55 +01:00
|
|
|
be run over only cities and not tables 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>
|
2000-07-03 00:01:27 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
In some cases you may wish to know which table a particular tuple
|
2001-01-14 00:58:55 +01:00
|
|
|
originated from. There is a system column called
|
2000-07-03 00:01:27 +02:00
|
|
|
<quote>TABLEOID</quote> in each table which can tell you the
|
|
|
|
originating table:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
SELECT c.tableoid, c.name, c.altitude
|
|
|
|
FROM cities c
|
2001-01-05 07:34:23 +01:00
|
|
|
WHERE c.altitude > 500;
|
2000-07-03 00:01:27 +02:00
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
which returns:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
+---------+----------+----------+
|
|
|
|
|tableoid |name | altitude |
|
|
|
|
+---------+----------+----------+
|
|
|
|
|37292 |Las Vegas | 2174 |
|
|
|
|
+---------+----------+----------+
|
|
|
|
|37280 |Mariposa | 1953 |
|
|
|
|
+---------+----------+----------+
|
|
|
|
|37280 |Madison | 845 |
|
|
|
|
+---------+----------+----------+
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
If you do a join with pg_class you can see the actual table name:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
SELECT p.relname, c.name, c.altitude
|
|
|
|
FROM cities c, pg_class p
|
2001-01-05 07:34:23 +01:00
|
|
|
WHERE c.altitude > 500 and c.tableoid = p.oid;
|
2000-07-03 00:01:27 +02:00
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
which returns:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
+---------+----------+----------+
|
|
|
|
|relname |name | altitude |
|
|
|
|
+---------+----------+----------+
|
|
|
|
|capitals |Las Vegas | 2174 |
|
|
|
|
+---------+----------+----------+
|
|
|
|
|cities |Mariposa | 1953 |
|
|
|
|
+---------+----------+----------+
|
|
|
|
|cities |Madison | 845 |
|
|
|
|
+---------+----------+----------+
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
</para>
|
|
|
|
|
2000-06-23 00:31:24 +02:00
|
|
|
<note>
|
|
|
|
<title>Deprecated</title>
|
|
|
|
<para>
|
2001-01-05 07:34:23 +01:00
|
|
|
In previous versions of <productname>Postgres</productname>, the
|
|
|
|
default was not to get access to child tables. This was found to
|
|
|
|
be error prone and is also in violation of SQL99. Under the old
|
2001-01-14 00:58:55 +01:00
|
|
|
syntax, to get the sub-tables you append "*" to the table name.
|
2001-01-05 07:34:23 +01:00
|
|
|
For example
|
2000-06-23 00:31:24 +02:00
|
|
|
<programlisting>
|
|
|
|
SELECT * from cities*;
|
|
|
|
</programlisting>
|
2001-01-05 07:34:23 +01:00
|
|
|
You can still explicitly specify scanning child tables by appending
|
|
|
|
"*", as well as explicitly specify not scanning child tables by
|
|
|
|
writing <quote>ONLY</quote>. But beginning in version 7.1, the default
|
|
|
|
behavior for an undecorated table name is to scan its child tables
|
|
|
|
too, whereas before the default was not to do so. To get the old
|
|
|
|
default behavior, set the configuration option
|
|
|
|
<literal>SQL_Inheritance</literal> to off, e.g.,
|
2000-06-23 00:31:24 +02:00
|
|
|
<programlisting>
|
|
|
|
SET SQL_Inheritance TO OFF;
|
|
|
|
</programlisting>
|
2001-01-05 07:34:23 +01:00
|
|
|
or add a line in your <filename>postgresql.conf</filename> file.
|
2000-06-23 00:31:24 +02:00
|
|
|
</para>
|
|
|
|
</note>
|
1999-08-08 06:21:33 +02:00
|
|
|
</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:
|
|
|
|
-->
|