Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Renaming primary key

Tags:

mysql

While creating table in mysql I've set wrong name to primary key column.

ALTER TABLE `table_name` CHANGE COLUMN `old_id_name` `new_id_name` integer auto_increment primary key

After that I'm getting

Error message: Multiple primary key defined

So the question is how can I rename the column. Note that I don't want to change primary key to another but just rename it.

like image 964
Eugeny89 Avatar asked Aug 09 '26 02:08

Eugeny89


2 Answers

use query like this

ALTER TABLE `table_name` CHANGE `old_id_name` `new_id_name` INT(11) NOT NULL AUTO_INCREMENT;

it will change only the column name of primary key

like image 96
PravinS Avatar answered Aug 10 '26 16:08

PravinS


Try this:

ALTER TABLE table_name CHANGE column old_name new_name int;

fiddle

like image 43
G one Avatar answered Aug 10 '26 17:08

G one



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!