多对多关系 table 无法从中删除值

Many to Many Relationship table Not working cannot delete Value from that

declare
begin
  for i in (select aid ,address from address)
  loop
    for j in (select aid ,address from address )
    loop
      if i.address=j.address then
        if i.aid!=j.aid then
          update employee_add 
          set aid=i.aid 
          where aid=j.aid;
          delete from address 
          where aid=i.aid;
        end if;
      end if; 
    end loop;
  end loop;
end;
/

此代码与 for loop 一样工作正常。之后显示错误:------

*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.

我有 tables employee[eid (primary key) ,ename]address[aid (primary key),address] 和多对多关系 tableemployee_add[eid,aid]。 请帮忙!提前致谢:)

您只能使用一个 loop 语句和 variablesv_addressv_aid)在行之间进行比较,如下面的块:

declare
  v_address address.address%type;
  v_aid     address.aid%type;  
begin
 for i in (select aid ,address from address order by aid)
 loop  
    if nvl(v_address,'')=i.address then 
         update employee_add set aid=v_aid where aid=i.aid;  
         delete address where aid=i.aid;
    else
       v_address := i.address;
       v_aid := i.aid;   
    end if;
  end loop;
end;