Home / Databases / MySQL / How can I manipulate a table with a reserved word as a column name?
How can I manipulate a table with a reserved word as a column name?
Last updated: 06/23/2009
It's easy to accidentally create a mysql table with a reserved word in it. Sometimes it happens when you upgrade to a newer version of mysql. In any case, it can be a catch-22 to try renaming/removing a column when it's a reserved word.
I recently ran into this with a table that had a "CONDITION" field. 'Condition' is a reserved word in Mysql 5.0. After trying several commands to rename the field, I came upon this trick.
The Trick: Remember you can refer to any field using the TABLE.COLUMN syntax. Even though it seems silly, you can do something like this:
This causes an error:
mysql> alter table machinery drop column condition; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition' at line 1
This works:
mysql> alter table machinery drop column machinery.condition;