How to alter or modify the primary key in a mysql table ?

If we have to alter or modify the primary key of a table in an existing mysql table, the following steps will help you.

For a table without primary key

For single primary key

ALTER TABLE table_name ADD PRIMARY KEY(your_primary_key);

For composite primary key

ALTER TABLE table_name ADD PRIMARY KEY(key1, key2, key3);

 

For a table with already existing primary key

For single primary key

ALTER TABLE table_name DROP PRIMARY KEY, ADD PRIMARY KEY(primary_key);

For composite primary key

ALTER TABLE table_name DROP PRIMARY KEY, ADD PRIMARY KEY(key1, key2, key2);

 

Hope this is helpful 🙂

About amalgjose
I am an Electrical Engineer by qualification, now I am working as a Software Architect. I am very much interested in Electrical, Electronics, Mechanical and now in Software fields. I like exploring things in these fields. I love travelling, long drives and music.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: