Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename a primary key column in MySQL?

Tags:

How do I rename a primary key column in MySQL?

like image 751
Vinicius Rocha Avatar asked Apr 24 '10 03:04

Vinicius Rocha


People also ask

Can we rename primary key column in MySQL?

Show activity on this post. drop foreign keys from others tables pointing to the primary key you want to rename. rename the primary key. add the foreign column to other tables.

How do I rename a column in MySQL?

Rename MySQL Column with the CHANGE Statement Enter the following command in your MySQL client shell to change the name of the column and its definition: ALTER TABLE table_name CHANGE old_column_name new_col_name Data Type; You can change the data type of the column or keep the existing one.

Can we rename primary key column in SQL?

In SQL Server, you can use the sp_rename stored procedure to rename a user created object in the current database, including a primary key. This can be handy if you've got a primary key that had its name automatically assigned, and you now want to give it a more readable name.


2 Answers

it's no different than altering any other column --

ALTER TABLE `pkey` CHANGE `keyfield` `keyfield2` INT( 11 ) NOT NULL AUTO_INCREMENT  

this changes the column keyfield in table pkey to be called keyfield2 -- you have to supply the definition afterwards, as usual.

like image 166
Igor Serebryany Avatar answered Nov 17 '22 20:11

Igor Serebryany


Leave off the PRIMARY KEY part of the alter statement. The primary key will be updated automatically.

like image 35
Dirigible Avatar answered Nov 17 '22 22:11

Dirigible