Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 9b6cc37ce608401d44f6535a0c7cb777 > files > 1016

postgresql11-docs-11.5-1.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>3.6. Inheritance</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="tutorial-window.html" title="3.5. Window Functions" /><link rel="next" href="tutorial-conclusion.html" title="3.7. Conclusion" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">3.6. Inheritance</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-window.html" title="3.5. Window Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tutorial-advanced.html" title="Chapter 3. Advanced Features">Up</a></td><th width="60%" align="center">Chapter 3. Advanced Features</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 11.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="tutorial-conclusion.html" title="3.7. Conclusion">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TUTORIAL-INHERITANCE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">3.6. Inheritance</h2></div></div></div><a id="id-1.4.5.7.2" class="indexterm"></a><p>
    Inheritance is a concept from object-oriented databases.  It opens
    up interesting new possibilities of database design.
   </p><p>
    Let's create two tables:  A table <code class="classname">cities</code>
    and a table <code class="classname">capitals</code>.  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:

</p><pre class="programlisting">
CREATE TABLE capitals (
  name       text,
  population real,
  altitude   int,    -- (in ft)
  state      char(2)
);

CREATE TABLE non_capitals (
  name       text,
  population real,
  altitude   int     -- (in ft)
);

CREATE VIEW cities AS
  SELECT name, population, altitude FROM capitals
    UNION
  SELECT name, population, altitude FROM non_capitals;
</pre><p>

    This works OK as far as querying goes, but it gets ugly when you
    need to update several rows, for one thing.
   </p><p>
    A better solution is this:

</p><pre class="programlisting">
CREATE TABLE cities (
  name       text,
  population real,
  altitude   int     -- (in ft)
);

CREATE TABLE capitals (
  state      char(2)
) INHERITS (cities);
</pre><p>
   </p><p>
    In this case, a row of <code class="classname">capitals</code>
    <em class="firstterm">inherits</em> all columns (<code class="structfield">name</code>,
    <code class="structfield">population</code>, and <code class="structfield">altitude</code>) from its
    <em class="firstterm">parent</em>, <code class="classname">cities</code>.  The
    type of the column <code class="structfield">name</code> is
    <code class="type">text</code>, a native <span class="productname">PostgreSQL</span>
    type for variable length character strings.  State capitals have
    an extra column, <code class="structfield">state</code>, that shows their state.  In
    <span class="productname">PostgreSQL</span>, a table can inherit from
    zero or more other tables.
   </p><p>
    For example, the  following  query finds the  names  of  all  cities,
    including  state capitals, that are located at an altitude
    over 500 feet:

</p><pre class="programlisting">
SELECT name, altitude
  FROM cities
  WHERE altitude &gt; 500;
</pre><p>

    which returns:

</p><pre class="screen">
   name    | altitude
-----------+----------
 Las Vegas |     2174
 Mariposa  |     1953
 Madison   |      845
(3 rows)
</pre><p>
   </p><p>
    On the other hand, the  following  query  finds
    all  the cities that are not state capitals and
    are situated at an altitude over 500 feet:

</p><pre class="programlisting">
SELECT name, altitude
    FROM ONLY cities
    WHERE altitude &gt; 500;
</pre><p>

</p><pre class="screen">
   name    | altitude
-----------+----------
 Las Vegas |     2174
 Mariposa  |     1953
(2 rows)
</pre><p>
   </p><p>
    Here the <code class="literal">ONLY</code> before <code class="literal">cities</code>
    indicates that the query should be run over only the
    <code class="classname">cities</code> table, and not tables below
    <code class="classname">cities</code> in the inheritance hierarchy.  Many
    of the commands that we have already discussed —
    <code class="command">SELECT</code>, <code class="command">UPDATE</code>, and
    <code class="command">DELETE</code> — support this <code class="literal">ONLY</code>
    notation.
   </p><div class="note"><h3 class="title">Note</h3><p>
     Although inheritance is frequently useful, it has not been integrated
     with unique constraints or foreign keys, which limits its usefulness.
     See <a class="xref" href="ddl-inherit.html" title="5.9. Inheritance">Section 5.9</a> for more detail.
    </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-window.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-advanced.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-conclusion.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3.5. Window Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 3.7. Conclusion</td></tr></table></div></body></html>