Home:ALL Converter>How do I rename a primary key column in MySQL?

How do I rename a primary key column in MySQL?

Ask Time:2010-04-24T11:39:08         Author:Vinicius Rocha

Json Formatter

How do I rename a primary key column in MySQL?

Author:Vinicius Rocha,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2703126/how-do-i-rename-a-primary-key-column-in-mysql
Eric Clack :

If you are working with InnoDB then I think you cannot rename primary keys, at least you can't if they are referenced by foreign keys. You need to dump the database, rename the columns and referencing keys in the dump file, then reload the database.",
2011-05-19T15:53:52
Igor Serebryany :

it's no different than altering any other column -- \n\nALTER TABLE `pkey` CHANGE `keyfield` `keyfield2` INT( 11 ) NOT NULL AUTO_INCREMENT \n\n\nthis changes the column keyfield in table pkey to be called keyfield2 -- you have to supply the definition afterwards, as usual.",
2010-04-24T05:09:37
Dirigible :

Leave off the PRIMARY KEY part of the alter statement. The primary key will be updated automatically.",
2015-07-17T03:13:26
Alex :

Maybe you have a foreign key constraint in place. You can disable those by SET foreign_key_constraints=0 but you have to remember to update the database afterwards.",
2011-05-19T15:57:42
Samuel DR :

Possible a bad practice work around. But you could export your entire db to an sql text file. Find and replace the PK you want to rename, and then restore the database over sql.",
2012-02-07T16:15:00
yy