Retrieving Dates in Non-ISO Format Converting Character Columns Between Fixed-Length and Variable-Length Types

• I f you have a version of MySQL older t han 3.22.16, it wont underst and t he MODIFY col_name synt ax used by add_elem ent .py. You m ay want t o edit t he script t o use CHANGE col_name synt ax inst ead. The follow ing t w o st at em ent s are equivalent : • ALTER TABLE tbl_name MODIFY col_name col_definition ; ALTER TABLE tbl_name CHANGE col_name col_name col_definition ; add_elem ent .py uses MODIFY because it s less confusing t han CHANGE .

9.9.8 Retrieving Dates in Non-ISO Format

MySQL st ores dat es in I SO 8601 form at CCYY-MM-DD , but it s oft en desirable or necessary t o rewrit e dat e values, such as when you need t o t ransfer t able dat a int o anot her program t hat expect s dat es in anot her form at . You can writ e a script t hat ret rieves and print s t able rows, using colum n m et adat a t o det ect DATE , DATETIME , and TIMESTAMP colum ns, and reform at t hem wit h DATE_FORMAT int o what ever dat e form at you want . For an exam ple, see Recipe 10.34 , which describes a short script nam ed iso_t o_us.pl t hat uses t his t echnique t o rewrit e I SO dat es int o U.S. form at .

9.9.9 Converting Character Columns Between Fixed-Length and Variable-Length Types

CHAR colum ns have a fixed lengt h, whereas VARCHAR colum ns are variable lengt h. I n general, t ables t hat use CHAR colum ns can be processed m ore quickly but t ake up m ore space t han t ables t hat use VARCHAR colum ns. To m ake it easier t o convert t ables t o use CHAR or VARCHAR colum ns, you can use t he inform at ion provided by SHOW COLUMNS t o generat e an ALTER TABLE st at em ent t hat perform s t he requisit e colum n conversions. Here is a Pyt hon funct ion alter_to_char t hat creat es a st at em ent for changing all t he VARCHAR colum ns t o CHAR : def alter_to_char conn, tbl_name: cursor = conn.cursor cursor.execute SHOW COLUMNS FROM + tbl_name rows = cursor.fetchall cursor.close str = for info in rows: col_name = info[0] type = info[1] if re.match varchar, type: its a VARCHAR column type = re.sub var, , type convert to CHAR determine whether column can contain NULL values if info[2] == YES: nullable = NULL else: nullable = NOT NULL; construct DEFAULT clause add surrounding quotes unless value is NULL default = DEFAULT + conn.literal info[4] add MODIFY clause to string if str = : str = str + ,\n\t str = str + \ MODIFY s s s s col_name, type, nullable, default cursor.close if str == : return None return ALTER TABLE + tbl_name + \n\t + str Suppose you have a t able t hat looks like t his: CREATE TABLE chartbl c1 VARCHAR10, c2 VARCHAR10 BINARY, c3 VARCHAR10 NOT NULL DEFAULT abc\def ; I f you pass t he nam e of t hat t able t o t he alter_to_varchar funct ion, t he st at em ent t hat it ret urns looks like t his: ALTER TABLE chartbl MODIFY c1 char10 NULL DEFAULT NULL, MODIFY c2 char10 binary NULL DEFAULT NULL, MODIFY c3 char10 NOT NULL DEFAULT abc\def A funct ion t o convert colum ns in t he ot her direct ion from CHAR t o VARCHAR would be sim ilar. Here is an exam ple, t his t im e in Perl: sub alter_to_varchar { my dbh, tbl_name = _; my sth, str; sth = dbh-prepare SHOW COLUMNS FROM tbl_name; sth-execute ; while my row = sth-fetchrow_array { if row[1] =~ char its a CHAR column { row[1] = var . row[1]; str .= ,\n\t if str; str .= MODIFY row[0] row[1]; str .= row[2] eq YES ? : NOT . NULL; str .= DEFAULT . dbh-quote row[4]; } } str = ALTER TABLE tbl_name\n\tstr if str; return str; } For com plet eness, t he funct ion generat es an ALTER TABLE st at em ent t hat explicit ly convert s all CHAR colum ns t o VARCHAR . I n pract ice, it s necessary t o convert only one such colum n. MySQL not ices t he change of a colum n from fixed- lengt h t o variable- lengt h form at , and aut om at ically convert s any ot her fixed- lengt h colum ns t hat have a variable- lengt h equivalent .

9.9.10 Selecting All Except Certain Columns