You should be able to do it, for each table, with code like the following (Note: I assume that the column description for each *_id
field is INTEGER NOT NULL
and that those same fields have AUTO_INCREMENT
set. This is pretty normal for an ID column.)
# remove auto_increment, allows us to remove primary key
ALTER TABLE user MODIFY user_id INTEGER NOT NULL;
# remove primary key, allows us to change column name
ALTER TABLE user DROP PRIMARY KEY;
# change column name, re-add auto_increment and primary key
ALTER TABLE user CHANGE user_id id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY;
Heed the other guys' warnings, though. Unless this is your creation (and you've just decided to change naming convention early on in the project or something), changing this will break any other code that expects to interface with this database.
Hope this helps,
Anthony
EDIT: the above assumes that you have no foreign key constraints, where other tables explicitly reference the user.user_id
column. If this is not the case, you will have to remove the all of the foreign key constraints before changing the name, then re-add them afterwards. If you're unsure where there are foreign key relationships in your database, you can list them all with the following code (copied from http://www.xinotes.org/notes/note/477/ to improve copy/paste-ability of answer):
SELECT CONCAT(table_name, '.', column_name, ' -> ', referenced_table_name, '.', referenced_column_name)
FROM information_schema.key_column_usage
WHERE referenced_table_schema = schema()
AND referenced_table_name is not null
ORDER BY table_name, column_name;