views:

29

answers:

1

Is there a mysql query to find and replace parts of field names?

I know you can do that within the actual data using

update [table_name] 
   set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');

... but how do you do that for the actual field names?

I have some fields with a - in the name, and I'd like to replace them all with a _

+1  A: 

To change the column names, you'll need to use the ALTER TABLE command.

Here's a solution in pseudocode:

Get all table names
Foreach t in table names
  Foreach columnname in t containing '-'
    newname = old name with "-" replaced with "_"
    "ALTER TABLE <tablename> CHANGE <columnname> <newname>"
  End
End
RyanHennig