Performing a Related-Table Update by Writing a Program

The result ing tmp t able is like t he original one, but has t w o new colum ns, capital and largest. You can exam ine it t o see t his. Aft er verifying t hat youre sat isfied wit h t he tmp t able, use it t o replace t he original states t able: DROP TABLE states; ALTER TABLE tmp RENAME TO states; I f you want t o m ake sure t here is no t im e, however brief, during which t he states t able is unavailable, perform t he replacem ent like t his inst ead: RENAME TABLE states TO states_old, tmp TO states; DROP TABLE states_old;

12.19.5 Performing a Related-Table Update by Writing a Program

The t able- replacem ent t echnique is efficient because it let s t he server do all t he work. On t he ot her hand, it is m ost appropriat e when youre updat ing all or m ost of t he rows in t he t able. I f youre updat ing j ust a few rows, it m ay be less work t o updat e t he t able in place for j ust t hose rows t hat need it . Also, t able replacem ent requires m ore t han t wice t he space of t he original states t able while youre carrying out t he updat e procedure. I f you have a huge t able t o updat e, you m ay not want t o use all t hat space. A second t echnique for updat ing a t able based on a relat ed t able is t o read t he inform at ion from t he relat ed t able and use it t o generat e UPDATE st at em ent s. For exam ple, t o updat e states wit h t he inform at ion st ored in t he city t able, r ead t he cit y nam es and use t hem t o creat e and issue a series of queries like t his: UPDATE states SET capital = Montgomery, largest = Birmingham WHERE name = Alabama; UPDATE states SET capital = Juneau, largest = Anchorage WHERE name = Alaska; UPDATE states SET capital = Phoenix, largest = Phoenix WHERE name = Arizona; UPDATE states SET capital = Little Rock, largest = Little Rock WHERE name = Arkansas; ... To carry out t his procedure, first alt er t he states t able so t hat it includes t he new colum ns: [3] [3] If youve already modified states using the table-replacement procedure, first restore the table to its original structure by dropping the capital and largest columns: ALTER TABLE states ADD capital VARCHAR30, ADD largest VARCHAR30; Next , w rit e a program t hat reads t he city t able and uses it s cont ent s t o produce UPDATE st at em ent s t hat m odify t he states t able. Her e is an exam ple script , updat e_cit ies.pl, t hat does so: usrbinperl -w update_cities.pl - update states table capital and largest city columns, using contents of city table. This assumes that the states table has been modified to include columns named capital and largest. use strict; use lib qwusrlocalapachelibperl; use Cookbook; my dbh = Cookbook::connect ; my sth = dbh-prepare SELECT state, capital, largest FROM city; sth-execute ; while my state, capital, largest = sth-fetchrow_array { dbh-do UPDATE states SET capital = ?, largest = ? WHERE name = ?, undef, capital, largest, state; } dbh-disconnect ; exit 0; The script has all t he t able and colum n nam es built in t o it , which m akes it very special purpose. You could generalize t his procedure by writ ing a funct ion t hat accept s param et ers indicat ing t he t able nam es, t he colum ns t o use for m at ching records in t he t wo t ables, and t he colum ns t o use for updat ing t he rows. The updat e_relat ed.pl script in t he j oins direct ory of t he recipes dist ribut ion shows one way t o do t his.

12.19.6 Performing a Related-Table Update Using mysql