tags:

views:

42

answers:

1

So, the situation is this:

  • Table Foo has a foreign key to table Bar.
  • There are currently many rows in Foo where this foreign key is null.
  • All Foos need a Bar.

I want to:

  • For each Foo where bar_fk is null, insert a new row into Bar.
  • Update Foo with the newly added id for Bar.

How would you do this in MySQL? Can it be done using straight SQL? Using stored procedures?

+1  A: 

The query SELECT LAST_INSERT_ID() should help you achieve this.

Jamie Lewis