tips and tricks for web developers & designers, and system & network admins

not-sexy.com

How to add a column to a mysql 5 table only if the column does not already exist

September 16, 2008 | work, mysql | 5:02 pm | Comments [0] |

The situation is this: You are running a database update script, and you need to add an additional field to a table in a way such that the script will not fail if the column already exists (from a previous update etc.). Most people would simply use a php script to get the layout of the table and then only execute the ALTER TABLE command if the column does not already exist, but sometimes we are bound by scripting restrictions, so here is the SQL code to do it:
(more…)