我的 postgreSQL 中有 3 個表
專案表:
id item_id
------------
1 1234
2 5678
帳戶表:
item_id account_id
----------------------
2 abcd
付款表:
account_id payment_status
--------------------------
abcd good
描述
- account_table中的item_id是item_table的id
- payment_table的account_id是account_table的account_id
我想從item_table中洗掉與item_id關聯的所有表中的條目
示例:如果我的 item_id 是 5678,那么我想從 ID 相互連接的所有表中洗掉條目。
如何在 PostgreSQL 中實作這一點?
uj5u.com熱心網友回復:
如果 account.item_id 被宣告為 item.id 的外鍵,您可以將該外鍵宣告為“ON DELETE CASCADE”。然后從“專案”中洗掉一行將級聯洗掉“帳戶”。
根據需要對其他表重復。
可能是我誤解了你,但它很容易讓你測驗。
uj5u.com熱心網友回復:
如果需要從三個表中洗掉,則需要運行三個 DELETE 陳述句:
delete from payment_table
where account_id in (select account_id
from account_table
where item_id in (select id
from item_table
where item_id = 5678));
delete from account_table
where item_id in (select id
from item_table
where item_id = 5678);
delete from item_table
where item_id = 5678;
uj5u.com熱心網友回復:
DELETE p,a FROM 'payment_table' p
LEFTJOIN 'account_table' a ON (p.account_id = a.account_id)
WHERE a.item_id = ?;
這樣的事情可能會讓你朝著正確的方向前進。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/463045.html
標籤:sql PostgreSQL
