gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

112 行
5.9KB

  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 V1.79.1" /><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 12.4 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>
  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. </p><pre class="programlisting">
  12. CREATE TABLE capitals (
  13. name text,
  14. population real,
  15. elevation int, -- (in ft)
  16. state char(2)
  17. );
  18. CREATE TABLE non_capitals (
  19. name text,
  20. population real,
  21. elevation int -- (in ft)
  22. );
  23. CREATE VIEW cities AS
  24. SELECT name, population, elevation FROM capitals
  25. UNION
  26. SELECT name, population, elevation FROM non_capitals;
  27. </pre><p>
  28. This works OK as far as querying goes, but it gets ugly when you
  29. need to update several rows, for one thing.
  30. </p><p>
  31. A better solution is this:
  32. </p><pre class="programlisting">
  33. CREATE TABLE cities (
  34. name text,
  35. population real,
  36. elevation int -- (in ft)
  37. );
  38. CREATE TABLE capitals (
  39. state char(2)
  40. ) INHERITS (cities);
  41. </pre><p>
  42. </p><p>
  43. In this case, a row of <code class="classname">capitals</code>
  44. <em class="firstterm">inherits</em> all columns (<code class="structfield">name</code>,
  45. <code class="structfield">population</code>, and <code class="structfield">elevation</code>) from its
  46. <em class="firstterm">parent</em>, <code class="classname">cities</code>. The
  47. type of the column <code class="structfield">name</code> is
  48. <code class="type">text</code>, a native <span class="productname">PostgreSQL</span>
  49. type for variable length character strings. The
  50. <code class="classname">capitals</code> table has
  51. an extra column, <code class="structfield">state</code>, which shows their states. In
  52. <span class="productname">PostgreSQL</span>, a table can inherit from
  53. zero or more other tables.
  54. </p><p>
  55. For example, the following query finds the names of all cities,
  56. including state capitals, that are located at an elevation
  57. over 500 feet:
  58. </p><pre class="programlisting">
  59. SELECT name, elevation
  60. FROM cities
  61. WHERE elevation &gt; 500;
  62. </pre><p>
  63. which returns:
  64. </p><pre class="screen">
  65. name | elevation
  66. -----------+-----------
  67. Las Vegas | 2174
  68. Mariposa | 1953
  69. Madison | 845
  70. (3 rows)
  71. </pre><p>
  72. </p><p>
  73. On the other hand, the following query finds
  74. all the cities that are not state capitals and
  75. are situated at an elevation over 500 feet:
  76. </p><pre class="programlisting">
  77. SELECT name, elevation
  78. FROM ONLY cities
  79. WHERE elevation &gt; 500;
  80. </pre><p>
  81. </p><pre class="screen">
  82. name | elevation
  83. -----------+-----------
  84. Las Vegas | 2174
  85. Mariposa | 1953
  86. (2 rows)
  87. </pre><p>
  88. </p><p>
  89. Here the <code class="literal">ONLY</code> before <code class="literal">cities</code>
  90. indicates that the query should be run over only the
  91. <code class="classname">cities</code> table, and not tables below
  92. <code class="classname">cities</code> in the inheritance hierarchy. Many
  93. of the commands that we have already discussed —
  94. <code class="command">SELECT</code>, <code class="command">UPDATE</code>, and
  95. <code class="command">DELETE</code> — support this <code class="literal">ONLY</code>
  96. notation.
  97. </p><div class="note"><h3 class="title">Note</h3><p>
  98. Although inheritance is frequently useful, it has not been integrated
  99. with unique constraints or foreign keys, which limits its usefulness.
  100. See <a class="xref" href="ddl-inherit.html" title="5.10. Inheritance">Section 5.10</a> for more detail.
  101. </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>
上海开阖软件有限公司 沪ICP备12045867号-1