0

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 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.