↑↓ 选择 ↵ 打开 ⌫ 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
测试与开发版本: 19 / devel
不受支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3
历史版本PostgreSQL 7.3 已于 2007 年 11 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本。

2.10. 依赖跟踪 #

当我们创建一个涉及到很多具有外键约束、视图、触发器、函数等的表的复杂数据库结构时,我们隐式地创建了一张对象之间的依赖关系网。例如,具有一个外键约束的表依赖于它所引用的表。

为了确保整个数据库结构的完整性, PostgreSQL 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 第 2.4.5 节, with the orders table depending on it, would result in an error message such as this:

DROP TABLE products;
NOTICE:  constraint $1 on table orders depends on table products
ERROR:  Cannot drop table products because other objects depend on it
        Use DROP ... CASCADE to drop the dependent objects too

The error message contains a useful hint: If you don't want to bother deleting all the dependent objects individually, you can run

DROP TABLE products CASCADE;

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 DROP ... CASCADE will do, run DROP without CASCADE and read the NOTICE messages.)

PostgreSQL 的所有删除命令都 支持指定 CASCADE。当然, 可能的依赖的性质随对象的 类型而不同。你也可以写 RESTRICT 来代替 CASCADE,以获得默认行为,即 限制删除其他对象所依赖的对象。

注意

根据 SQL 标准,必须指定 RESTRICT 或 CASCADE 之一。实际上没有数据库系统按这种方式实现,但 默认行为是 RESTRICT 还是 CASCADE 因系统而异。

注意

PostgreSQL 7.3 之前版本的外键 约束依赖和 serial 列依赖在升级 过程中不会被维护或创建。所有其他 依赖类型在升级期间都会被正确 创建。

提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。