gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 line
7.0KB

  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>5.14. Dependency Tracking</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="ddl-others.html" title="5.13. Other Database Objects" /><link rel="next" href="dml.html" title="Chapter 6. Data Manipulation" /></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">5.14. Dependency Tracking</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ddl-others.html" title="5.13. Other Database Objects">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ddl.html" title="Chapter 5. Data Definition">Up</a></td><th width="60%" align="center">Chapter 5. Data Definition</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="dml.html" title="Chapter 6. Data Manipulation">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="DDL-DEPEND"><div class="titlepage"><div><div><h2 class="title" style="clear: both">5.14. Dependency Tracking</h2></div></div></div><a id="id-1.5.4.16.2" class="indexterm"></a><a id="id-1.5.4.16.3" class="indexterm"></a><p>
  3. When you create complex database structures involving many tables
  4. with foreign key constraints, views, triggers, functions, etc. you
  5. implicitly create a net of dependencies between the objects.
  6. For instance, a table with a foreign key constraint depends on the
  7. table it references.
  8. </p><p>
  9. To ensure the integrity of the entire database structure,
  10. <span class="productname">PostgreSQL</span> makes sure that you cannot
  11. drop objects that other objects still depend on. For example,
  12. attempting to drop the products table we considered in <a class="xref" href="ddl-constraints.html#DDL-CONSTRAINTS-FK" title="5.4.5. Foreign Keys">Section 5.4.5</a>, with the orders table depending on
  13. it, would result in an error message like this:
  14. </p><pre class="screen">
  15. DROP TABLE products;
  16. ERROR: cannot drop table products because other objects depend on it
  17. DETAIL: constraint orders_product_no_fkey on table orders depends on table products
  18. HINT: Use DROP ... CASCADE to drop the dependent objects too.
  19. </pre><p>
  20. The error message contains a useful hint: if you do not want to
  21. bother deleting all the dependent objects individually, you can run:
  22. </p><pre class="screen">
  23. DROP TABLE products CASCADE;
  24. </pre><p>
  25. and all the dependent objects will be removed, as will any objects
  26. that depend on them, recursively. In this case, it doesn't remove
  27. the orders table, it only removes the foreign key constraint.
  28. It stops there because nothing depends on the foreign key constraint.
  29. (If you want to check what <code class="command">DROP ... CASCADE</code> will do,
  30. run <code class="command">DROP</code> without <code class="literal">CASCADE</code> and read the
  31. <code class="literal">DETAIL</code> output.)
  32. </p><p>
  33. Almost all <code class="command">DROP</code> commands in <span class="productname">PostgreSQL</span> support
  34. specifying <code class="literal">CASCADE</code>. Of course, the nature of
  35. the possible dependencies varies with the type of the object. You
  36. can also write <code class="literal">RESTRICT</code> instead of
  37. <code class="literal">CASCADE</code> to get the default behavior, which is to
  38. prevent dropping objects that any other objects depend on.
  39. </p><div class="note"><h3 class="title">Note</h3><p>
  40. According to the SQL standard, specifying either
  41. <code class="literal">RESTRICT</code> or <code class="literal">CASCADE</code> is
  42. required in a <code class="command">DROP</code> command. No database system actually
  43. enforces that rule, but whether the default behavior
  44. is <code class="literal">RESTRICT</code> or <code class="literal">CASCADE</code> varies
  45. across systems.
  46. </p></div><p>
  47. If a <code class="command">DROP</code> command lists multiple
  48. objects, <code class="literal">CASCADE</code> is only required when there are
  49. dependencies outside the specified group. For example, when saying
  50. <code class="literal">DROP TABLE tab1, tab2</code> the existence of a foreign
  51. key referencing <code class="literal">tab1</code> from <code class="literal">tab2</code> would not mean
  52. that <code class="literal">CASCADE</code> is needed to succeed.
  53. </p><p>
  54. For user-defined functions, <span class="productname">PostgreSQL</span> tracks
  55. dependencies associated with a function's externally-visible properties,
  56. such as its argument and result types, but <span class="emphasis"><em>not</em></span> dependencies
  57. that could only be known by examining the function body. As an example,
  58. consider this situation:
  59. </p><pre class="programlisting">
  60. CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow',
  61. 'green', 'blue', 'purple');
  62. CREATE TABLE my_colors (color rainbow, note text);
  63. CREATE FUNCTION get_color_note (rainbow) RETURNS text AS
  64. 'SELECT note FROM my_colors WHERE color = $1'
  65. LANGUAGE SQL;
  66. </pre><p>
  67. (See <a class="xref" href="xfunc-sql.html" title="37.5. Query Language (SQL) Functions">Section 37.5</a> for an explanation of SQL-language
  68. functions.) <span class="productname">PostgreSQL</span> will be aware that
  69. the <code class="function">get_color_note</code> function depends on the <code class="type">rainbow</code>
  70. type: dropping the type would force dropping the function, because its
  71. argument type would no longer be defined. But <span class="productname">PostgreSQL</span>
  72. will not consider <code class="function">get_color_note</code> to depend on
  73. the <code class="structname">my_colors</code> table, and so will not drop the function if
  74. the table is dropped. While there are disadvantages to this approach,
  75. there are also benefits. The function is still valid in some sense if the
  76. table is missing, though executing it would cause an error; creating a new
  77. table of the same name would allow the function to work again.
  78. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ddl-others.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ddl.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="dml.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.13. Other Database Objects </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 6. Data Manipulation</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1