Foreign keys update automation
I think this will do it
I changed this answer as I may have read the question incorrectly
Look at edit history if you are looking for something different

as for the fk  

with CTE as 
(  select id, mail 
        , row_number() over (partition by mail order by id) as rn
   from table 
) 
update fk1  
   set fk1.fkID = cte1.ID 
  from fk as fk1 
  join cte as CTE2 
    on CTE2.ID = fk1.fkID
   and CTE2.rn &gt; 1 
  join cte as CTE1 
    on CTE1.mail = CTE2.mail 
   and CTE1.rn = 1;


run above for addition fk  

delete *  
from  
(  select id, mail 
        , row_number() over (partition by mail order by id) as rn
   from table 
) as t2 
where rn &gt; 1;