首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何为postgres编写删除级联?

如何为postgres编写删除级联?
EN

Stack Overflow用户
提问于 2010-09-15 01:59:48
回答 5查看 72K关注 0票数 28

我正在为postgres手动构造一个DELETE CASCADE语句。

我有一个'transaction‘和一个'slice’表,相关如下所示:

代码语言:javascript
运行
复制
    Table "public.slice"
  Column  | Type | Modifiers 
----------+------+-----------
 id       | text | not null
 name     | text | 
Referenced by:
    TABLE "transaction" CONSTRAINT "transaction_slice_id_fkey" FOREIGN KEY (slice_id) REFERENCES slice(id)
 Table "public.transaction"
  Column  | Type | Modifiers 
----------+------+-----------
 id       | text | not null
 slice_id | text | 
Referenced by:
    TABLE "classification_item" CONSTRAINT "classification_item_transaction_id_fkey" FOREIGN KEY (transaction_id) REFERENCES transaction(id)
Table "public.classification_item"
     Column     | Type | Modifiers 
----------------+------+-----------
 id             | text | not null
 transaction_id | text | 
Foreign-key constraints:
    "classification_item_transaction_id_fkey" FOREIGN KEY (transaction_id) REFERENCES transaction(id)

假设我想删除由名为‘my_ classification_items’的切片引用的所有事务和切片。我需要写些什么?

代码语言:javascript
运行
复制
=# delete from classification_item where transaction_id= #...? 
=# delete from transaction where slice_id= #...? 
=# delete from slice where name='my_slice';
EN

Stack Overflow用户

发布于 2010-09-15 03:58:01

Postgres外键支持级联删除:

代码语言:javascript
运行
复制
slice_id integer REFERENCES slice(id) ON DELETE CASCADE

票数 52
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3711580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档