Thursday, August 27, 2009

MySQL rename a table or column

SkyHi @ Thursday, August 27, 2009
Summary: As of MySQL 5.0, renaming a table is trivial with the "rename table" function. Renaming a column was a little cryptic, even looking at the documentation. Here are a couple of concrete examples.

To rename a table from "lu_wind_direction" to "lu_direction":

mysql> rename table lu_wind_direction to lu_direction;

To rename a column called "wind_direction_id" to "direction_id" in the table named "spacetime":

mysql> alter table spacetime change wind_direction_id direction_id tinyint(3) unsigned;

So, when renaming a column, it's just the original name first, then after the new name, you need to include the column specifications and constraints.