1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>5.11.Dependency Tracking</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="ddl.html" title="Chapter5.Data Definition">
<link rel="prev" href="ddl-others.html" title="5.10.Other Database Objects">
<link rel="next" href="dml.html" title="Chapter6.Data Manipulation">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="ddl-depend"></a>5.11.Dependency Tracking</h2></div></div></div>
<a name="id579310"></a><a name="id579325"></a><p> When you create complex database structures involving many tables
with foreign key constraints, views, triggers, functions, etc. you
will implicitly create a net of dependencies between the objects.
For instance, a table with a foreign key constraint depends on the
table it references.
</p>
<p> To ensure the integrity of the entire database structure,
<span class="productname">PostgreSQL</span> makes sure that you cannot
drop objects that other objects still depend on. For example,
attempting to drop the products table we had considered in <a href="ddl-constraints.html#ddl-constraints-fk" title="5.3.5.Foreign Keys">Section5.3.5, “Foreign Keys”</a>, with the orders table depending on
it, would result in an error message such as this:
</p>
<pre class="screen">DROP TABLE products;
NOTICE: constraint orders_product_no_fkey on table orders depends on table products
ERROR: cannot drop table products because other objects depend on it
HINT: Use DROP ... CASCADE to drop the dependent objects too.</pre>
<p>
The error message contains a useful hint: if you do not want to
bother deleting all the dependent objects individually, you can run
</p>
<pre class="screen">DROP TABLE products CASCADE;</pre>
<p>
and all the dependent objects will be removed. In this case, it
doesn't remove the orders table, it only removes the foreign key
constraint. (If you want to check what <code class="command">DROP ... CASCADE</code> will do,
run <code class="command">DROP</code> without <code class="literal">CASCADE</code> and read the <code class="literal">NOTICE</code> messages.)
</p>
<p> All drop commands in <span class="productname">PostgreSQL</span> support
specifying <code class="literal">CASCADE</code>. Of course, the nature of
the possible dependencies varies with the type of the object. You
can also write <code class="literal">RESTRICT</code> instead of
<code class="literal">CASCADE</code> to get the default behavior, which is to
prevent drops of objects that other objects depend on.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p> According to the SQL standard, specifying either
<code class="literal">RESTRICT</code> or <code class="literal">CASCADE</code> is
required. No database system actually enforces that rule, but
whether the default behavior is <code class="literal">RESTRICT</code> or
<code class="literal">CASCADE</code> varies across systems.
</p>
</div>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p> Foreign key constraint dependencies and serial column dependencies
from <span class="productname">PostgreSQL</span> versions prior to 7.3
are <span class="emphasis"><em>not</em></span> maintained or created during the
upgrade process. All other dependency types will be properly
created during an upgrade from a pre-7.3 database.
</p>
</div>
</div></body>
</html>
|