删除连接的输出(多表中的记录)

时间:2015-06-12 13:44:00

标签: sql postgresql join cloudfoundry cascading-deletes

我用

select * 
from 
    service_instances i 
    inner join 
    service_instance_operations op on i.id = op.service_instance_id 
    inner join 
    service_bindings bind on i.id = bind.service_instance_id  
where i.guid = 'daf67426-129b-4010-832c-692bcfe98f62';

如何在所有3个表中删除此记录?存在主要和外键关键约束。

将“select *”替换为“delete”无效。

数据库架构是Cloud Foundry pyvenv-activate bug 的CCDB。

1 个答案:

答案 0 :(得分:2)

您可以使用WITH构建。

with _deleted_service_instances as (
    delete from service_instances 
    where guid = 'daf67426-129b-4010-832c-692bcfe98f62'
    returning id
)
, _deleted_service_instance_operations as (
    delete from service_instance_operations op
    using _deleted_service_instances i
        where op.service_instance_id = i.id
)
, _deleted_service_bindings as (
    delete from service_bindings b
    using _deleted_service_instances i
        where b.service_instance_id = i.id
)
select null::int as dummy;

<强>更新

  

我使用PostgreSQL 9.0.3并且因供应商而无法升级。它发布于2011-01-31。你知道我的版本查询的外观吗?

我只看到一种方法 - 使用三个查询:

    delete from service_instance_operations op
    using service_instances i
    where op.service_instance_id = i.id
            AND i.guid = 'daf67426-129b-4010-832c-692bcfe98f62';

    delete from service_bindings b
    using service_instances i
    where b.service_instance_id = i.id
            AND i.guid = 'daf67426-129b-4010-832c-692bcfe98f62';

    delete from service_instances 
    where guid = 'daf67426-129b-4010-832c-692bcfe98f62';