为什么drop table cascade没有在postgresql中删除子表?

时间:2018-03-22 07:55:56

标签: 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.

但是当我使用cascade选项删除时,表被删除,但它不会删除该表中的其他表或其外键列,它只删除外键约束。

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)

这是预期的行为吗?如果是on delete cascade,删除父项中的行,则在子表中删除,但删除表不会发生相同的事情吗?

我错过了什么吗?这种行为是否特定于postgres?

提前致谢。

2 个答案:

答案 0 :(得分:1)

  

(...)删除由视图引用的表或另一个表的外键约束,必须指定CASCADE。 (CASCADE将完全删除从属视图,但在外键情况下,它只会删除外键约束,而不是完全删除其他表。)

https://www.postgresql.org/docs/current/static/sql-droptable.html(强调我的)

答案 1 :(得分:1)

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

Quote from the manual

  

但在外键的情况下,它只会删除外键约束,而不是其他表

(强调我的)

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