postgresql - 为什么 drop table cascade 不删除 postgresql 中的子表?

标签 postgresql foreign-keys ddl drop-table

我有以下两个表,其架构如下所示:-

postgres=# \d products1;
                               Table "public.products1"
       Column       |  Type   |                       Modifiers
--------------------+---------+--------------------------------------------------------
 id                 | integer | not null default nextval('products1_id_seq'::regclass)
 name               | text    | not null
 default_picture_id | integer |
Indexes:
    "products1_pkey" PRIMARY KEY, btree (id)
    "unique_id_default_pic_id" UNIQUE CONSTRAINT, btree (id, default_picture_id)
Foreign-key constraints:
    "fk_products_1" FOREIGN KEY (id, default_picture_id) REFERENCES product_pictures1(product_id, id) ON UPDATE RESTRICT ON DELETE RESTRICT
Referenced by:
    TABLE "product_pictures1" CONSTRAINT "fk_id_product_id" FOREIGN KEY (id, product_id) REFERENCES products1(default_picture_id, id)


postgres=# \d product_pictures1;
                           Table "public.product_pictures1"
   Column   |  Type   |                           Modifiers
------------+---------+----------------------------------------------------------------
 id         | integer | not null default nextval('product_pictures1_id_seq'::regclass)
 img_path   | text    | not null
 product_id | integer |
Indexes:
    "product_pictures1_pkey" PRIMARY KEY, btree (id)
    "unique_id_productid" UNIQUE CONSTRAINT, btree (id, product_id)
Foreign-key constraints:
    "fk_id_product_id" FOREIGN KEY (id, product_id) REFERENCES products1(default_picture_id, id)
Referenced by:
    TABLE "products1" CONSTRAINT "fk_products_1" FOREIGN KEY (id, default_picture_id) REFERENCES product_pictures1(product_id, id) ON UPDATE RESTRICT ON DELETE RESTRICT

以下两个表相互引用:-

当我尝试删除任何一张表时,它会出现以下错误:-

postgres=# drop table products1;
ERROR:  cannot drop table products1 because other objects depend on it
DETAIL:  constraint fk_id_product_id on table product_pictures1 depends on table products1
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

但是当我用级联选项删除时,表被删除了,但它并没有删除另一个表或其在该表中的外键列,它只删除了外键约束。

postgres=# drop table products1 cascade;
NOTICE:  drop cascades to constraint fk_id_product_id on table product_pictures1
DROP TABLE

postgres=# \d product_pictures1;
                           Table "public.product_pictures1"
   Column   |  Type   |                           Modifiers
------------+---------+----------------------------------------------------------------
 id         | integer | not null default nextval('product_pictures1_id_seq'::regclass)
 img_path   | text    | not null
 product_id | integer |
Indexes:
    "product_pictures1_pkey" PRIMARY KEY, btree (id)
    "unique_id_productid" UNIQUE CONSTRAINT, btree (id, product_id)

这是预期的行为吗?在 删除级联 的情况下,删除父表中的行,删除子表中的行,但删除表不会发生同样的事情吗?

我错过了什么吗?这种行为是 postgres 特有的吗?

提前致谢。

最佳答案

因为这就是 DROP ... CASCADE 的设计方式。

Quote from the manual

but in the foreign-key case it will only remove the foreign-key constraint, not the other table entirely

(强调我的)

这不是 Postgres 特有的。删除表时,Oracle 和 DB2 的工作方式相同。

关于postgresql - 为什么 drop table cascade 不删除 postgresql 中的子表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49423291/

相关文章:

postgresql - 用于可变列数查询的 Postgres CrossTab

ruby-on-rails - 使用数据结构的gem在Rails中进行Postgresql复制

postgresql - 将数据加载到新 CitusDB 实例的最快方法是什么?

mysql - 同一表中的两列和相同的外键

postgresql - 'serial' 列是否自动成为 PostgreSQL 中的主键?

database - 在这种情况下如何确定放置外键的位置?

sql - 将主键更改为复合主键

mysql - 让 SQL 像 DDL 语句一样

sqlite - SQLite:条件DDL语句

python - MySQL CREATE TABLE 在 DDL 语句中添加额外的行