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

本小妞迷上赌 提交于 2019-12-25 02:28:47

问题


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

This code works fine as for loop. After that it shows error :------

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

I have tables employee[eid (primary key) ,ename] ,address[aid (primary key),address],and many to many relation tableemployee_add[eid,aid]. Please help! Thank in Advance :)


回答1:


You can use only one loop statement and variables( v_address and v_aid ) to make a comparison between the rows as in the block below :

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;


来源:https://stackoverflow.com/questions/54500413/many-to-many-relationship-table-not-working-cannot-delete-value-from-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!