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

delete the output of a join (record in multipe tables)

我用

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 cloud_controller_ng 的 CCDB。

您可以使用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;

更新

I use PostgreSQL 9.0.3 and can't upgrade because of vendor. It was released 2011-01-31. Do you know how the query looks with my version?

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

    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';