多对多关系表不起作用无法从中删除值

时间:2019-02-03 06:04:42

标签: oracle plsql plsql-package

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一样正常工作。之后,它显示错误:------

  

*原因:外键值没有匹配的主键值。
  *操作:删除外键或添加匹配的主键。

我有表employee[eid (primary key) ,ename]address[aid (primary key),address]和多对多关系表employee_add[eid,aid]。             请帮忙!在此先感谢:)

1 个答案:

答案 0 :(得分:0)

您只能使用一个loop语句和variablesv_addressv_aid)在各行之间进行比较,如下面的块所示:< / em>

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;