1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

Inheritance overhaul by Chris Bitmead <chris@bitmead.com>

This commit is contained in:
Bruce Momjian
2000-06-09 01:44:34 +00:00
parent fb070464c1
commit 8c1d09d591
32 changed files with 484 additions and 204 deletions

View File

@ -1,5 +1,5 @@
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/advanced.sgml,v 1.12 2000/05/02 20:01:51 thomas Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/advanced.sgml,v 1.13 2000/06/09 01:43:55 momjian Exp $
-->
<chapter id="advanced">
@ -35,7 +35,7 @@ CREATE TABLE cities (
CREATE TABLE capitals (
state char(2)
) INHERITS (cities);
) UNDER cities;
</programlisting>
In this case, an instance of capitals <firstterm>inherits</firstterm> all
@ -60,38 +60,20 @@ CREATE TABLE capitals (
</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;
<para>
For example, the following query finds the names of all cities,
including state capitals, that are located at an altitude
over 500ft, the query is:
+----------+----------+
|name | altitude |
+----------+----------+
|Las Vegas | 2174 |
+----------+----------+
|Mariposa | 1953 |
+----------+----------+
</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
<programlisting>
SELECT c.name, c.altitude
FROM cities c
WHERE c.altitude > 500;
</programlisting>
</programlisting>
which returns:
<programlisting>
which returns:
<programlisting>
+----------+----------+
|name | altitude |
+----------+----------+
@ -101,16 +83,50 @@ SELECT c.name, c.altitude
+----------+----------+
|Madison | 845 |
+----------+----------+
</programlisting>
</programlisting>
</para>
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 inheritance notation using "*" as do other commands like
<command>ALTER</command>.
</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 &gt; 500;
+----------+----------+
|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
inheritance hierarchy. Many of the commands that we
have already discussed -- <command>SELECT</command>,
<command>UPDATE</command> and <command>DELETE</command> --
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>
</para>
</sect1>
<sect1>

View File

@ -1,6 +1,6 @@
.\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here....
.\" $Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.5 2000/02/17 03:39:39 tgl Exp $
.\" $Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.6 2000/06/09 01:43:56 momjian Exp $
.TH "SYSTEM CATALOGS" INTRO 03/13/94 PostgreSQL PostgreSQL
.SH "Section 7 - System Catalogs"
.de LS
@ -191,6 +191,8 @@ pg_class
2=main memory */
int2vector relkey /* - unused */
oidvector relkeyop /* - unused */
bool relhassubclass /* does the class have a subclass?
*/
aclitem relacl[1] /* access control lists */
.fi
.nf M

View File

@ -1,5 +1,5 @@
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/inherit.sgml,v 1.7 2000/05/02 20:01:51 thomas Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/inherit.sgml,v 1.8 2000/06/09 01:43:56 momjian Exp $
-->
<chapter id="inherit">
@ -17,9 +17,9 @@ CREATE TABLE cities (
altitude int -- (in ft)
);
CREATE TABLE capitals (
CREATE TABLE capitals UNDER cities (
state char(2)
) INHERITS (cities);
);
</programlisting>
In this case, an instance of capitals <firstterm>inherits</firstterm> all
@ -41,50 +41,71 @@ CREATE TABLE capitals (
</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,
For example, the following query finds 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
<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>
<programlisting>
+----------+----------+
|name | altitude |
+----------+----------+
|Las Vegas | 2174 |
+----------+----------+
|Mariposa | 1953 |
+----------+----------+
|Madison | 845 |
+----------+----------+
</programlisting>
</para>
Here the "*" after cities indicates that the query should
be run over cities and all classes below cities in the
<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 &gt; 500;
+----------+----------+
|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
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>.
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>
</para>
</chapter>

View File

@ -1,5 +1,5 @@
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.12 2000/04/11 14:43:54 momjian Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.13 2000/06/09 01:43:57 momjian Exp $
Postgres documentation
-->
@ -23,10 +23,10 @@ Postgres documentation
<date>1999-07-20</date>
</refsynopsisdivinfo>
<synopsis>
ALTER TABLE <replaceable class="PARAMETER">table</replaceable> [ * ]
ALTER TABLE [ ONLY ]<replaceable class="PARAMETER">table</replaceable> [ * ]
ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable
class="PARAMETER">type</replaceable>
ALTER TABLE <replaceable class="PARAMETER">table</replaceable> [ * ]
ALTER TABLE [ ONLY ]<replaceable class="PARAMETER">table</replaceable> [ * ]
ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET DEFAULT <replaceable
class="PARAMETER">value</replaceable> | DROP DEFAULT }
ALTER TABLE <replaceable class="PARAMETER">table</replaceable> [ * ]
@ -175,24 +175,6 @@ ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
The keyword <literal>COLUMN</literal> is noise and can be omitted.
</para>
<para>
<quote>*</quote> following a name of a table indicates that the statement
should be run over that table and all tables below it in the
inheritance hierarchy;
by default, the attribute will not be added to or renamed in any of the subclasses.
This should always be done when adding or modifying an attribute in a
superclass. If it is not, queries on the inheritance hierarchy
such as
<programlisting>
SELECT <replaceable>NewColumn</replaceable> FROM <replaceable>SuperClass</replaceable>*
</programlisting>
will not work because the subclasses will be missing an attribute
found in the superclass.
</para>
<para>
In the current implementation, default and constraint clauses for the
new column will be ignored. You can use the <literal>SET DEFAULT</literal>

View File

@ -1,5 +1,5 @@
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_table.sgml,v 1.29 2000/05/02 20:02:03 thomas Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_table.sgml,v 1.30 2000/06/09 01:43:57 momjian Exp $
Postgres documentation
-->
@ -31,7 +31,7 @@ CREATE [ TEMPORARY | TEMP ] TABLE <replaceable class="PARAMETER">table</replacea
[, PRIMARY KEY ( <replaceable class="PARAMETER">column</replaceable> [, ...] ) ]
[, CHECK ( <replaceable class="PARAMETER">condition</replaceable> ) ]
[, <replaceable>table_constraint_clause</replaceable> ]
) [ INHERITS ( <replaceable>inherited_table</replaceable> [, ...] ) ]
) [ UNDER <replaceable>inherited_table</replaceable> [, ...] ]
</synopsis>
<refsect2 id="R2-SQL-CREATETABLE-1">
@ -130,10 +130,10 @@ CREATE [ TEMPORARY | TEMP ] TABLE <replaceable class="PARAMETER">table</replacea
</varlistentry>
<varlistentry>
<term>INHERITS <replaceable class="PARAMETER">inherited_table</replaceable></term>
<term>UNDER <replaceable class="PARAMETER">inherited_table</replaceable></term>
<listitem>
<para>
The optional INHERITS clause specifies a collection of table
The optional UNDER clause specifies a collection of table
names from which this table automatically inherits all fields.
If any inherited field name appears more than once,
<productname>Postgres</productname>
@ -229,7 +229,7 @@ ERROR: DEFAULT: type mismatched
</para>
<para>
The optional INHERITS
The optional UNDER
clause specifies a collection of class names from which this class
automatically inherits all fields. If any inherited field name
appears more than once, Postgres reports an error. Postgres automatically
@ -1838,8 +1838,8 @@ CREATE TABLE distributors (
Notes
</title>
<para>
CREATE TABLE/INHERITS is a <productname>Postgres</productname>
language extension.
CREATE TABLE/UNDER is defined by SQL3. Multiple inheritance is a
<productname>Postgres</productname> language extension.
</para>
</refsect2>

View File

@ -1,5 +1,5 @@
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/delete.sgml,v 1.10 2000/03/26 18:32:27 petere Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/ref/delete.sgml,v 1.11 2000/06/09 01:44:00 momjian Exp $
Postgres documentation
-->
@ -24,7 +24,7 @@ Postgres documentation
<date>1999-07-20</date>
</refsynopsisdivinfo>
<synopsis>
DELETE FROM <replaceable class="PARAMETER">table</replaceable> [ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
DELETE FROM [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
</synopsis>
<refsect2 id="R2-SQL-DELETE-1">
@ -118,6 +118,12 @@ DELETE <replaceable class="parameter">count</replaceable>
</tip>
</para>
<para>
By default DELETE will delete tuples in the table specified
and all its sub-classes. If you wish to only update the
specific table mentioned, you should use the ONLY clause.
</para>
<para>
You must have write access to the table in order to modify
it, as well as read access to any table whose values are

View File

@ -1,5 +1,5 @@
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/select.sgml,v 1.28 2000/03/27 17:14:43 thomas Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/ref/select.sgml,v 1.29 2000/06/09 01:44:00 momjian Exp $
Postgres documentation
-->
@ -25,7 +25,7 @@ Postgres documentation
SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replaceable> [, ...] ) ] ]
<replaceable class="PARAMETER">expression</replaceable> [ AS <replaceable class="PARAMETER">name</replaceable> ] [, ...]
[ INTO [ TEMPORARY | TEMP ] [ TABLE ] <replaceable class="PARAMETER">new_table</replaceable> ]
[ FROM <replaceable class="PARAMETER">table</replaceable> [ <replaceable class="PARAMETER">alias</replaceable> ] [, ...] ]
[ FROM [ ONLY ]<replaceable class="PARAMETER">table</replaceable> [ <replaceable class="PARAMETER">alias</replaceable> ] [, ...] ]
[ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
[ GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...] ]
[ HAVING <replaceable class="PARAMETER">condition</replaceable> [, ...] ]
@ -203,6 +203,13 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replac
if WHERE is omitted, all rows are candidates.
(See <xref linkend="sql-where" endterm="sql-where-title">.)
</para>
<para>
<command>ONLY</command> will eliminate rows from subclasses of the table.
This was previously the default result, and getting subclasses was
obtained by appending <command>*</command> to the table name.
The old behaviour is available via the command
<command>SET EXAMINE_SUBCLASS TO 'on';</command>
</para>
<para>
<command>DISTINCT</command> will eliminate duplicate rows from the

View File

@ -1,5 +1,9 @@
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/set.sgml,v 1.43 2000/05/18 14:24:33 momjian Exp $
<<<<<<< set.sgml
$Header: /cvsroot/pgsql/doc/src/sgml/ref/set.sgml,v 1.44 2000/06/09 01:44:00 momjian Exp $
=======
$Header: /cvsroot/pgsql/doc/src/sgml/ref/set.sgml,v 1.44 2000/06/09 01:44:00 momjian Exp $
>>>>>>> 1.43
Postgres documentation
-->
@ -553,6 +557,39 @@ SELECT setseed(<replaceable>value</replaceable>);
</listitem>
</varlistentry>
<varlistentry>
<term>EXAMINE_SUBCLASS</term>
<listitem>
<para>
Changes the behaviour of SELECT so that it no longer automatically
examines sub-classes. (See SELECT). By default a SELECT on a table
will also return subclass tuples unless specifying ONLY tablename.
Setting this returns postgres to the traditional behaviour of
only returning subclasses when appending "*" to the tablename.
<variablelist>
<varlistentry>
<term>ON</term>
<listitem>
<para>
Returns SELECT to the behaviour of automatically returning
results from sub-classes.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>OFF</term>
<listitem>
<para>
Prevents SELECT from returning sub-classes unless the "*" follows the table name
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>ENABLE_SEQSCAN</term>
<listitem>

View File

@ -1,5 +1,5 @@
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v 1.9 2000/04/11 05:39:15 thomas Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v 1.10 2000/06/09 01:44:00 momjian Exp $
Postgres documentation
-->
@ -23,7 +23,7 @@ Postgres documentation
<date>1999-07-20</date>
</refsynopsisdivinfo>
<synopsis>
UPDATE <replaceable class="PARAMETER">table</replaceable> SET <replaceable class="PARAMETER">col</replaceable> = <replaceable class="PARAMETER">expression</replaceable> [, ...]
UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> SET <replaceable class="PARAMETER">col</replaceable> = <replaceable class="PARAMETER">expression</replaceable> [, ...]
[ FROM <replaceable class="PARAMETER">fromlist</replaceable> ]
[ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
</synopsis>
@ -140,6 +140,12 @@ UPDATE <replaceable class="parameter">#</replaceable>
it, as well as read access to any table whose values are
mentioned in the WHERE condition.
</para>
<para>
By default UPDATE will update tuples in the table specified
and all its sub-classes. If you wish to only update the
specific table mentioned, you should use the ONLY clause.
</para>
</refsect1>
<refsect1 id="R1-SQL-UPDATE-2">

View File

@ -1,5 +1,5 @@
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/syntax.sgml,v 1.20 2000/05/02 20:01:53 thomas Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/syntax.sgml,v 1.21 2000/06/09 01:43:56 momjian Exp $
-->
<chapter id="syntax">
@ -854,9 +854,10 @@ sqrt(emp.salary)
defines one or more instance variables to range over the class
indicated in <replaceable>class_reference</replaceable>.
One can also request
the instance variable to range over all classes that are beneath the
indicated class in the inheritance hierarchy by postpending the
designator asterisk ("*").
the instance variable to range over only the specific class
and not those that are beneath the
indicated class in the inheritance hierarchy by specifying ONLY before
before the classname.
</para>
</sect2>
</sect1>