Cry MySQL How to...

Rename a database table field


To rename a field in a MySQL database table use:

alter table table_name change old_field_name new_field_name field-spec

where:

table_name
is the name of the database table
old_field_name
is the name of the file to rename.
new_field_name
is the new name for the field once it has been renamed.
field-spec
is the definition for the field. This is required. This does mean that you can rename a field and change its type at the same time.

For example, if a table "accounts" has a field called "phone" which needs to be renamed to "telephone" (being a varchar 40 field) then this would be:

alter table accounts change phone telephone varchar(40)

The field-specification must always be included, as the original field definition is not carried forward. So do check that it is correct.


These notes have been tested against MySQL version 5.1, and may apply to other versions as well.



About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.