Problem Solution Discussion Changing a Table Type

8.5 Changing a Columns Default Value

8.5.1 Problem

You want t o leave a colum n definit ion alone except for t he default value.

8.5.2 Solution

Use SET DEFAULT t o specify t he default value explicit ly, or DROP DEFAULT t o rem ove t he current default and allow MySQL t o assign t he default default .

8.5.3 Discussion

A colum ns default value is part of it s definit ion, but can be m odified separat ely from ot her aspect s of t he definit ion. To change a default value, use ALTER col_name SET DEFAULT : ALTER TABLE mytbl ALTER j SET DEFAULT 1000; Default values m ust be const ant s. For exam ple, you cannot set t he default for a dat e- valued colum n t o NOW , alt hough t hat would be very useful. To drop a default value, use ALTER col_name DROP DEFAULT : ALTER TABLE mytbl ALTER j DROP DEFAULT; I n t his case, t he colum ns default value revert s t o t he st andard default for t he colum n t ype. For colum ns t hat can cont ain NULL values, t his will be NULL . Ot herwise, t he general default s are 0, t he em pt y st ring, or t he zero dat e or t im e for num eric, st ring, or dat e or t im e colum ns, respect ively. The except ions are for AUTO_INCREMENT , ENUM , and TIMESTAMP colum ns, for which t he default s are t he next sequence num ber, t he first enum erat ion m em ber, and t he current dat e and t im e.

8.6 Changing a Table Type

8.6.1 Problem

A t able has one t ype, and now you realize t hat anot her t able t ype has propert ies t hat are m ore desirable for t he way you use t he t able.

8.6.2 Solution

Use ALTER TABLE t o change it s t ype wit h a TYPE clause.

8.6.3 Discussion

MySQL support s several t ables t ypes, each of which have differing charact erist ics. Som et im es it s necessary or desirable t o convert a t able from one t ype t o anot her. Som e sit uat ions where a change of t able t ype can be useful are as follows: • Table conversions som et im es are done t o gain access t o feat ures t hat are support ed by one t able t ype but not anot her. For exam ple, I SAM t ables do not allow NULL values in indexed colum ns. Also, AUTO_INCREMENT behavior in I SAM t ables is such t hat sequence values m ay be non- m onot onic under cert ain condit ions. See Chapt er 11 , for inform at ion about t his. You can convert an I SAM t able t o t he MyI SAM t ype, which does not suffer from t hese problem s. Or you m ight find t hat you need t o perform t ransact ions on a t able creat ed using a t ype t hat doesnt provide t ransact ional capabilit ies. To handle t his problem , you can alt er t he t able t o a t ype such as I nnoDB or BDB t hat does support t ransact ions. • The oldest t able t ype support ed by MySQL is I SAM, but I SAM t ables are deprecat ed and at som e point no longer will be support ed. I f you have I SAM t ables, you should convert t hem at som e point t o anot her t able t ype. Ot herwise, aft er I SAM support is dropped, youll be unable t o upgrade t o new releases of MySQL. Changing a t able t ype is easy; use ALTER TABLE wit h a TYPE specifier. For exam ple, t o convert a t able t o t he MyI SAM t ype, use t his st at em ent : ALTER TABLE tbl_name TYPE = MYISAM; To find out t he current t ype of a t able, use t he SHOW TABLE STATUS st at em ent int roduced in MySQL 3.23.0 or SHOW CREATE TABLE int roduced in MySQL 3.23.20 : mysql SHOW TABLE STATUS LIKE mytbl\G 1. row Name: mytbl Type: MyISAM Row_format: Fixed Rows: 0 Avg_row_length: 0 Data_length: 0 Max_data_length: 85899345919 Index_length: 1024 Data_free: 0 Auto_increment: NULL Create_time: 2002-07-15 21:28:34 Update_time: 2002-07-15 21:28:34 Check_time: NULL Create_options: Comment: mysql SHOW CREATE TABLE mytbl\G 1. row Table: mytbl Create Table: CREATE TABLE `mytbl` `c` char10 default NULL, `j` bigint20 default NULL, `e2` enumhardware,software,books,office supplies, telecommunications,furniture,utilities,shipping,tax default NULL TYPE=MyISAM Alt ernat ively, use t he m ysqldum p com m and- line ut ilit y: mysqldump --no-data cookbook mytbl CREATE TABLE mytbl c char10 default NULL, j bigint20 default NULL, e2 enumhardware,software,books,office supplies, telecommunications,furniture,utilities,shipping,tax default NULL TYPE=MyISAM;

8.7 Renaming a Table