]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/tutorial-inheritance.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / tutorial-inheritance.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!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 id="docContent" class="container-fluid col-10"><div 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 18.0 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 /></div><div class="sect1" id="TUTORIAL-INHERITANCE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">3.6. Inheritance <a href="#TUTORIAL-INHERITANCE" class="id_link">#</a></h2></div></div></div><a id="id-1.4.5.7.2" class="indexterm"></a><p>
3     Inheritance is a concept from object-oriented databases.  It opens
4     up interesting new possibilities of database design.
5    </p><p>
6     Let's create two tables:  A table <code class="classname">cities</code>
7     and a table <code class="classname">capitals</code>.  Naturally, capitals
8     are also cities, so you want some way to show the capitals
9     implicitly when you list all cities.  If you're really clever you
10     might invent some scheme like this:
11
12 </p><pre class="programlisting">
13 CREATE TABLE capitals (
14   name       text,
15   population real,
16   elevation  int,    -- (in ft)
17   state      char(2)
18 );
19
20 CREATE TABLE non_capitals (
21   name       text,
22   population real,
23   elevation  int     -- (in ft)
24 );
25
26 CREATE VIEW cities AS
27   SELECT name, population, elevation FROM capitals
28     UNION
29   SELECT name, population, elevation FROM non_capitals;
30 </pre><p>
31
32     This works OK as far as querying goes, but it gets ugly when you
33     need to update several rows, for one thing.
34    </p><p>
35     A better solution is this:
36
37 </p><pre class="programlisting">
38 CREATE TABLE cities (
39   name       text,
40   population real,
41   elevation  int     -- (in ft)
42 );
43
44 CREATE TABLE capitals (
45   state      char(2) UNIQUE NOT NULL
46 ) INHERITS (cities);
47 </pre><p>
48    </p><p>
49     In this case, a row of <code class="classname">capitals</code>
50     <em class="firstterm">inherits</em> all columns (<code class="structfield">name</code>,
51     <code class="structfield">population</code>, and <code class="structfield">elevation</code>) from its
52     <em class="firstterm">parent</em>, <code class="classname">cities</code>.  The
53     type of the column <code class="structfield">name</code> is
54     <code class="type">text</code>, a native <span class="productname">PostgreSQL</span>
55     type for variable length character strings.  The
56     <code class="classname">capitals</code> table has
57     an additional column, <code class="structfield">state</code>, which shows its
58     state abbreviation.  In
59     <span class="productname">PostgreSQL</span>, a table can inherit from
60     zero or more other tables.
61    </p><p>
62     For example, the  following  query finds the  names  of  all  cities,
63     including  state capitals, that are located at an elevation
64     over 500 feet:
65
66 </p><pre class="programlisting">
67 SELECT name, elevation
68   FROM cities
69   WHERE elevation &gt; 500;
70 </pre><p>
71
72     which returns:
73
74 </p><pre class="screen">
75    name    | elevation
76 -----------+-----------
77  Las Vegas |      2174
78  Mariposa  |      1953
79  Madison   |       845
80 (3 rows)
81 </pre><p>
82    </p><p>
83     On the other hand, the  following  query  finds
84     all  the cities that are not state capitals and
85     are situated at an elevation over 500 feet:
86
87 </p><pre class="programlisting">
88 SELECT name, elevation
89     FROM ONLY cities
90     WHERE elevation &gt; 500;
91 </pre><p>
92
93 </p><pre class="screen">
94    name    | elevation
95 -----------+-----------
96  Las Vegas |      2174
97  Mariposa  |      1953
98 (2 rows)
99 </pre><p>
100    </p><p>
101     Here the <code class="literal">ONLY</code> before <code class="literal">cities</code>
102     indicates that the query should be run over only the
103     <code class="classname">cities</code> table, and not tables below
104     <code class="classname">cities</code> in the inheritance hierarchy.  Many
105     of the commands that we have already discussed —
106     <code class="command">SELECT</code>, <code class="command">UPDATE</code>, and
107     <code class="command">DELETE</code> — support this <code class="literal">ONLY</code>
108     notation.
109    </p><div class="note"><h3 class="title">Note</h3><p>
110      Although inheritance is frequently useful, it has not been integrated
111      with unique constraints or foreign keys, which limits its usefulness.
112      See <a class="xref" href="ddl-inherit.html" title="5.11. Inheritance">Section 5.11</a> for more detail.
113     </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" title="3.5. Window Functions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-advanced.html" title="Chapter 3. Advanced Features">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-conclusion.html" title="3.7. Conclusion">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" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 3.7. Conclusion</td></tr></table></div></body></html>