バージョンごとのドキュメント一覧

3.6. 継承 #

<title>Inheritance</title>

Inheritance is a concept from object-oriented databases. It opens up interesting new possibilities of database design. 継承とはオブジェクト指向データベースの概念です。 データベース設計においてこれまでになかった興味深い可能性を広げてくれます。

Let's create two tables: A table <classname>cities</classname> and a table <classname>capitals</classname>. Naturally, capitals are also cities, so you want some way to show the capitals implicitly when you list all cities. If you're really clever you might invent some scheme like this: 2つのテーブルcities(都市)テーブルとcapitals(州都)テーブルを作ってみましょう。 州都は本来同時に都市でもありますので、全ての都市をリストする時は何もしなくても州都も表示する何らかの方法が必要です。 賢い人なら次のような案を工夫するかもしれません。

CREATE TABLE capitals (
  name       text,
  population real,

  elevation  int,    &#45;- (in ft)

  elevation  int,    -- (フィート単位)
  state      char(2)
);

CREATE TABLE non_capitals (
  name       text,
  population real,

  elevation  int     &#45;- (in ft)

  elevation  int     -- (フィート単位)
);

CREATE VIEW cities AS
  SELECT name, population, elevation FROM capitals
    UNION
  SELECT name, population, elevation FROM non_capitals;

This works OK as far as querying goes, but it gets ugly when you need to update several rows, for one thing. 問い合わせを続ける分には問題はありませんが、例えば、複数の行を更新する時に醜くなります。

A better solution is this: より良い解決策は次のような構文です。

CREATE TABLE cities (
  name       text,
  population real,

  elevation  int     &#45;- (in ft)

  elevation  int     -- (フィート単位)
);

CREATE TABLE capitals (
  state      char(2) UNIQUE NOT NULL
) INHERITS (cities);

In this case, a row of <classname>capitals</classname> <firstterm>inherits</firstterm> all columns (<structfield>name</structfield>, <structfield>population</structfield>, and <structfield>elevation</structfield>) from its <firstterm>parent</firstterm>, <classname>cities</classname>. The type of the column <structfield>name</structfield> is <type>text</type>, a native <productname>PostgreSQL</productname> type for variable length character strings. The <classname>capitals</classname> table has an additional column, <structfield>state</structfield>, which shows its state abbreviation. In <productname>PostgreSQL</productname>, a table can inherit from zero or more other tables. この例では、capitalsテーブルの行はcitiesテーブルから全ての列、すなわちname(都市名)、population(人口)そしてelevation(標高)を継承します。 name列のデータ型は、可変長文字列のためにPostgreSQLが初めから備えているtext型です。 capitalsテーブルは、これに加えて州の略称を示すstate列を持ちます。 PostgreSQLでは、テーブルは関連付けられたテーブルがあればそれぞれから属性を継承できます。

For example, the following query finds the names of all cities, including state capitals, that are located at an elevation over 500 feet: 以下の問い合わせの例は、州都も含めて、標高500フィート以上に位置する全ての都市を求めるものです。

SELECT name, elevation
  FROM cities
  WHERE elevation > 500;

which returns: これは以下を返します。

   name    | elevation
-----------+-----------
 Las Vegas |      2174
 Mariposa  |      1953
 Madison   |       845
(3 rows)

On the other hand, the following query finds all the cities that are not state capitals and are situated at an elevation over 500 feet: その一方、州都ではない標高500フィート以上に位置する都市を見つけ出したい時は次のような問い合わせになります。

SELECT name, elevation
    FROM ONLY cities
    WHERE elevation > 500;

   name    | elevation
-----------+-----------
 Las Vegas |      2174
 Mariposa  |      1953
(2 rows)

Here the <literal>ONLY</literal> before <literal>cities</literal> indicates that the query should be run over only the <classname>cities</classname> table, and not tables below <classname>cities</classname> in the inheritance hierarchy. Many of the commands that we have already discussed &mdash; <command>SELECT</command>, <command>UPDATE</command>, and <command>DELETE</command> &mdash; support this <literal>ONLY</literal> notation. ここでcitiesの前に置かれたONLYは、継承階層においてcitiesテーブルの下層にあるテーブルではなく、citiesテーブルのみを参照することを意味します。 既に説明したSELECTUPDATEおよびDELETEなど数多くのコマンドは、このONLY表記をサポートしています。

注記

Although inheritance is frequently useful, it has not been integrated with unique constraints or foreign keys, which limits its usefulness. See <xref linkend="ddl-inherit"/> for more detail. 継承は多くの場合で便利ですが、一意性制約や外部キーと統合されていないので万能ではありません。 詳細は5.10を参照してください。