【问题标题】:How to delete from table then delete what deleted rows referenced? (postgresql)如何从表中删除然后删除引用的已删除行? (postgresql)
【发布时间】:2016-12-13 12:11:41
【问题描述】:

我想做的是从table 1 中删除行,然后从table 2 中删除那些从table 1 中删除的行使用外键引用的行。我想知道是否可以在不必删除表 2 上的外键并使用 on delete cascade 重新创建它的情况下执行此操作。

我不擅长使用 SQL,尝试了 3 种不同的方法,但均未成功。

1:尝试一次从多个表中删除

delete from table1 a, table2 b where a.table2_id = b.id and a.column < 0;

2:删除并返回

delete from table2 where id in
   (delete from table1 where column < 0 returning table2_id as id);

3:从select创建一个数组并用它从两个表中删除

DECLARE
   arr integer[] :=  array(select table2_id from table1 where column < 0);
BEGIN
   FOREACH m SLICE 1 IN ARRAY arr
   LOOP
      delete from table1 where table2_id = m;
      delete from table2 where id = m;
   END LOOP;
END

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    您可以使用可写 CTE 在单个语句中执行此操作

    with t1_deleted as (
       delete from table1 where column < 0 
       returning table2_id as id
    )
    delete from table2 
    where id in (select id from t1_deleted);
    

    【讨论】:

      【解决方案2】:

      如果你可以重新安排你的删除,我认为你可以做到这一点。

      请尝试以下更新的功能。

      create table schema_name.table_name1 (id serial primary key, name text)
      create table schema_name.table_name2(id bigint references schema_name.table_name1(id), mno bigint)
      
      create or replace function schema_name.fn_test_f_k() returns void as $$
      DECLARE
         c1 cursor for select id from schema_name.table_name1 where id = 1;
      BEGIN
         for r IN c1
         LOOP
            delete from schema_name.table_name2 where id = r.id;
            delete from schema_name.table_name1 where id = r.id;
         END LOOP;
      END
      $$
      LANGUAGE PLPGSQL SECURITY DEFINER;
      
      select schema_name.fn_test_f_k();
      

      【讨论】:

      • 您可以使用没有功能的删除查询,只需在 where 条件下提及要删除的行。
      • 只需要先从外键表(table2)中删除行
      • 但我不能先删除 table2 因为 table1 引用了它。
      • 只是你需要先从外键表中删除,然后再从主键表中删除行。
      • table1.table2_id = table2.id 表一引用表二。
      猜你喜欢
      • 2022-07-22
      • 1970-01-01
      • 2020-05-13
      • 2021-09-20
      • 2014-01-24
      • 2014-04-29
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      相关资源
      最近更新 更多