[thelist] MYSQL - Add field to table with data for existing records

Ken Moore psm2713 at hotmail.com
Thu Oct 12 11:40:01 CDT 2006

Hi, all,

M. Seyon asked about a combined ALTER and UPDATE command. There may be some 
way. A much better plan, if you are worried about inconsistancies, is to 
copy to do this another name, make the changes, fill the fields(s) in the 
temp table, and UPDATE the real table WHERE TempTable.NewField = "whatever".

Be certain to make at least 2 backups of the original table. You may not 
need any, but one might not be enough. A painful lesson learned.


>Need some quick mysql help. I'm sure this is a fairly straightforward 
>I would like to add a field to an existing table, with the following 
>- For all existing records, the field needs to be given a value - fixed
>value, same for every record
>- But I don't want this value to become the default value for subsequent
>records (added after the new field)
>For reference, the field is a year. All existing records need to be set to
>2006. Future records will of course be set to the relevant year, which will
>not be 2006.
>I know I can use the ALTER command to add the field, but not sure how to
>add the value.
>I also know I can do this with two statements - first ALTER then UPDATE but
>wondering if it can be done in one go.

Express yourself - download free Windows Live Messenger themes! 

More information about the thelist mailing list