This is a pretty conceptual question so I don't know that code can be given in the answer. But basically you have a number of steps:
create your new tables and populate them.
add the additional fields (allow nulls at first until you get your db null-free)
Populate the additional fields (
update foo set bar_id = (select id from bar where ....)
)
Check for nulls. Then once you are done, alter the tables to have not null constraints. If you need an interrim check you can add a CHECK
constraint that id is below a certain point or the foreign key is not null.