Problem Solution Discussion Changing a Column Definition or Name

Column Repositioning and TIMESTAMP Columns Be careful if you reposit ion colum ns in a t able t hat cont ains m ore t han one TIMESTAMP colum n. The first TIMESTAMP colum n has special propert ies not shared by t he ot hers. See Recipe 5.32 , which describes t he differences. I f you change t he order of t he TIMESTAMP colum ns, youll change t he way your t able behaves.

8.3 Changing a Column Definition or Name

8.3.1 Problem

You want t o change how a colum n is defined.

8.3.2 Solution

Use MODIFY or CHANGE . MODIFY is sim pler, but cannot change t he colum n nam e. CHANGE is m ore confusing t o use, but can change bot h t he nam e and t he definit ion.

8.3.3 Discussion

To change a colum ns definit ion, use MODIFY or CHANGE . [ 1] Of t he t w o, MODIFY has t he sim pler synt ax: nam e t he colum n, t hen specify it s new definit ion. For exam ple, t o change colum n c from CHAR1 t o CHAR10 , do t his: [ 1] MODIFY requires MySQL 3.22.16 or lat er. ALTER TABLE mytbl MODIFY c CHAR10; Wit h CHANGE , t he synt ax is a bit different . Aft er t he CHANGE keyword, you nam e t he colum n you want t o change, t hen specify t he new definit ion, which includes t he new nam e. The second colum n nam e is required, because CHANGE also allows you t o renam e t he colum n, not j ust change it s definit ion. For exam ple, t o change i from INT t o BIGINT and renam e it t o j at t he sam e t im e, t he st at em ent looks like t his: ALTER TABLE mytbl CHANGE i j BIGINT; I f you now use CHANGE t o convert j from BIGINT back t o INT wit hout changing t he colum n nam e, t he st at em ent m ay look a bit odd: ALTER TABLE mytbl CHANGE j j INT; At first glance, t he st at em ent seem s incorrect —t he colum n nam e appears t o be given one t oo m any t im es. However, it s correct as writ t en. The fact t hat t he CHANGE synt ax requires t wo colum n nam es even if t heyre bot h t he sam e is sim ply som et hing you have t o get used t o. This is especially im port ant t o rem em ber if your version of MySQL is old enough t hat you cant use MODIFY . Any ALTER TABLE st at em ent t hat uses MODIFY col_name can be replaced by one t hat uses CHANGE col_name col_name . That is, t he following t wo st at em ent s are equivalent : ALTER TABLE tbl_name MODIFY col_name ... ; ALTER TABLE tbl_name CHANGE col_name col_name ... ; I t would be nice t o have a form of t he ALTER TABLE st at em ent t hat renam ed a colum n wit hout t he need t o repeat t he definit ion, especially for working wit h ENUM and SET colum ns t hat have m any m em ber values. Unfort unat ely, t here is no such st at em ent , which m akes t hese colum n t ypes som ewhat difficult t o work wit h when using ALTER TABLE . Suppose you add t o mytbl an ENUM colum n e t hat has several m em bers: ALTER TABLE mytbl ADD e ENUMhardware,software,books,office supplies, telecommunications,furniture,utilities, shipping,tax; I f you w ant t o renam e t he colum n from e t o e2 , you use CHANGE t o indicat e t he new nam e. But you m ust also repeat t he colum n definit ion as well: ALTER TABLE mytbl CHANGE e e2 ENUMhardware,software,books,office supplies, telecommunications,furniture,utilities, shipping,tax; Ugh. That s t oo m uch t yping. Manually ent ering t he proper ALTER TABLE st at em ent for t his kind of operat ion is quit e a lot of work, not t o m ent ion error-prone. One way t o avoid ret yping t he definit ion is t o capt ure t he current definit ion in a file and edit t he file t o produce t he proper ALTER TABLE st at em ent : • Run m ysqldum p t o get t he CREATE TABLE st at em ent t hat cont ains t he colum n definit ion: mysqldump --no-data cookbook mytbl test.txt The result ing file, t est .t xt , should cont ain t his st at em ent : CREATE TABLE mytbl c char10 default NULL, j bigint20 NOT NULL default 100, e enumhardware,software,books,office supplies,telecommunications, furniture,utilities,shipping,tax default NULL TYPE=MyISAM; The - - no-dat a opt ion t ells m ysqldum p not t o dum p t he dat a from t he t able; it s used here because only t he t able creat ion st at em ent is needed. • Edit t he t est .t xt file t o rem ove everyt hing but t he definit ion for t he e colum n: • e enumhardware,software,books,office supplies,telecommunications, furniture,utilities,shipping,tax default NULL • Modify t he definit ion t o produce an ALTER TABLE st at em ent wit h a sem icolon at t he end: • ALTER TABLE mytbl CHANGE e e2 • enumhardware,software,books,office supplies,telecommunications, furniture,utilities,shipping,tax default NULL; • Writ e t est .t xt back out t o save it , t hen get out of t he edit or and feed t est .t xt as a bat ch file t o m ysql: mysql cookbook test.txt For sim ple colum ns, t his procedure is m ore work t han j ust t yping t he ALTER TABLE st at em ent m anually, of course. But for ENUM and SET colum ns wit h long and ungainly definit ions, using an edit or t o creat e a m ysql bat ch file from m ysqldum p out put m akes a lot of sense. You can also use t his t echnique t o m ake it easier t o reorder t he it em s in an ENUM or SET colum n, or t o add or delet e m em bers from t he colum n definit ion. For anot her approach t o colum n m anipulat ion, see Recipe 9.9 , which develops a ut ilit y script t hat m akes it t rivial t o add m em ber values. The script exam ines t he t able st ruct ure and uses t hat inform at ion t o figure out t he proper ALTER TABLE st at em ent for m odifying an ENUM or SET colum n.

8.4 The Effect of ALTER TABLE on Null and Default Value Attributes